SlideShare a Scribd company logo
1 of 18
Download to read offline
Concurrent
Programming
   May 6, 2010
Agenda

• Motivational Examples
• Computing Model
• Race Conditions
• Deadlocks
• Resources
                    2
Sleeping Barber
      “BAU” Process
      Barber cuts hair
      When finished, he checks the waiting room
      If no one is there, he goes to sleep in the chair
      Customers arriving while the barber is asleep wake the
      barber


          What can go wrong?
          Barber finishes, walks to waiting room
          Customer arrives and goes to wake the barber
          They do not see each other, return and wait
          indefinitely



          3
Dining Philosophers
                               “BAU” Process
                               Philosophers alternate between thinking and eating
                               Two forks are required to eat spaghetti
                               When finished thinking, philosophers seek forks
                               When finished eating, philosophers put forks down


                                     What can go wrong?
                                     Process of picking up, putting down forks can
                                     result in each philosopher holding one fork -
                                     none can eat


     Dining Philosophers Demo
 http://www.doc.ic.ac.uk/~jnm/concurrency/classes/Diners/Diners.html
                                     4
Computing
                   Canonical Model
                   • Processes spawn threads to do work
                   • Threads maintain local state but can also
                       access shared state
                   •   Threads can be active, suspended, blocked
                       waiting for resources or sleeping
                   •   Threads execute concurrently - truly
                       concurrently in multi-processor
                       environments, virtually concurrently in single-
                       processor environments
                   •   Thread scheduling is managed by the parent
                       process, operating system and / or application
                   •   Sequencing of operations across different
                       threads is in general unpredictable


Shared State
                   5
Simple Computing Problem
      Bad Timing                                  Web App Hit Counter
                                                  • Multi-threaded web app wants to maintain a
                                                       count of the number of hits it gets
        Read counter
                                                  •    Shared state is used to maintain the count
                                                  •    Request processing threads update the count
                                                       each time they process a request
                                                  •    Under heavy load, the app seems to “miss”
        Read counter                                   some hits. Why?

     Assign counter+1


                                                      public void increment() {
      Assign counter+1
                                                          count = count + 1;
                                                      }

Badness results from B reading the counter while A is in process of updating it. This is an example of a
race condition.
                                                  6
Solution
     Semaphore                            Keep B out while A updates
                                      • Could make “count++” operation atomic (i.e. unable to
                                           be interrupted.) The JDK 1.5 AtomicInteger class
                                           does this.
       Close the door                 •    More general solution is to synchronize access to the
                                           shared resource
                                      •    Using a semaphore, Thread A signals exclusive access
                                           to the critical section of code that does the update
       Knock on door
                                      •    Simplest semaphore is a mutex (mutual exclusion - just
                                           one allowed in at a time) and simplest implementation
    Update, open door
                                           in Java is via the synchronized keyword


      Close door, update
                                      public synchronized void increment() {
                                          count = count + 1;
                                      }

Note that the same solution would work for the sleeping barber (close door to waiting room)
                                                 7
Synchronized Keyword in Java
   •   Acts as a mutex on blocks of code

   •   Can protect entire methods, or blocks within methods

   •   Uses the concept of a monitor on an object to represent the mutex - code entering a
       synchronized block acquires an object monitor

   •   When no object is specified, the monitor on the object executing the code is implied

   •   Synchronization locks are reentrant - i.e., if a thread owns an object’s monitor and it enters
       another method that requires the same monitor, it is allowed in

   •   When a thread leaves a synchronized block, it releases the associated monitor (even if an
       exception occurs)

                        Parent object’s monitor                                Fu’s monitor
public synchronized void foo() {       public void foo() {               public void foo() {
    fu.bar();                              synchronized (this) {             synchronized (fu) {
}                                              fu.bar();                         fu.bar();
                                           }                                 }
                                       }                                 }

                                                  8
Concurrent Object Access
                                   Method




                                   Object


                                  Threads
                        Object References
When multiple threads share references to the same object, they can concurrently call the same
method on the object, unless synchronization prevents this. A class is thread-safe if concurrently
executing threads can safely hold references to instances of the class.
                                                9
Programmer’s Challenge:
     Situational Awareness
 •    The question always needs to be asked, “will multiple threads hold references to instances
      of this class at the same time?” Unfortunately, it is sometimes hard to know.
 •    Most common use cases involving concurrent access are created by application
      containers, often in conjunction with application frameworks. Containers manage
      threads and frameworks instantiate objects. Fun begins when the Singleton Pattern is
      used by the framework.


Primitive Survival Technique: Avoid the bad neighborhood altogether

•    Keep is Stateless Stupid (KISS) - avoid maintaining state across method calls
•    Prefer immutable objects - only final fields, all set by constructors
•    Never spawn threads - let the container manage them
•    Use tested, thread-safe resource providers (e.g. databases) to maintain state (when you are
     dragged kicking and screaming into acknowledging the need to maintain state at all)
•    Encapsulate parameters needed by methods in value objects and pass these to/from methods
     instead of relying on instance fields

                                                 10
Anatomical Example
public class Foo {
    public static final int HOSIERY_THRESHOLD = 1000;               No worries
    private boolean fubar;
    ...
    public void hose(int ways) {
                                                                     Careful!
        int newWays = generateNewWays();
        if (ways + newWays > HOSIERY_THRESHOLD) {
            fubar = true;
        }
    }




•   Static final fields are initialized when the class is loaded and don’t change thereafter, so all
    threads see the same values - threadsafe
•   Method parameters are provided to each thread on the call stack and are only visible to
    that thread for the duration of the method - threadsafe
•   Local variables declared within methods are only visible to the thread activating the
    method for the duration of the method - threadsafe
•   Class member fields are visible to all threads, so if two threads can be inside a method
    that uses a member field at the same time, care must be taken to synchronize access
                                                11
Example - Struts 1.x Actions
         Servlet Container                                                          1. When the web app starts, it loads
                 ActionServlet                                                         the Struts ActionServlet (one
                                                                                       ActionServlet instance per web
                       FooAction
                                                                                       application - a singleton)

                        BarAction
                                                                                    2. When the ActionServlet is
                                                                                       initialized, it reads its
                                                                                       configuration and loads one
                                                                                       instance each of the Action
                                                                                       classes defined in its configuration
                      FubarAction                                                      (more singletons)
                                                                                    3. When browser requests arrive,
                                                                                       the container assigns processing
                                                                                       threads to requests and passes
                                                                                       requests mapped to the Struts
                                                                                       application to the Struts
                                                                                       ActionServlet instance

     4. The Struts ActionServlet calls perform(ActionMapping mapping, ActionForm form, HttpServletRequest
        request, HttpServletResponse response) on the single Action instance that it uses for all requests


Consequence: Struts Action classes must be threadsafe!
rtfm://struts.apache.org/1.0.2/userGuide/building_controller.html#action_classes
                                                                               12
Struts 1.x Example (Cont.)
Inside a Struts Action Class:
private String successPath = null;
private String failedPath = null;
private String maxAttemptsPath = null;

public ActionForward perform( ActionMapping mapping, ActionForm
        form, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {

    // Set forward paths, using request information
    setForwardPaths(mapping, request );
  ...
Depending on processing outcome, forward to configured path
}

private void setForwardPaths(ActionMapping mapping, HttpServletRequest request) {
  ...
  successPath = ...
  failedPath = ...
  maxAttemptsPath = ...
}


 Problem: If two requests for this action are processed concurrently, and the
 computed forward paths are different, control can be misdirected.
                                             13
Struts 1.x Example - Fix
private String successPath = null;
private String failedPath = null;
private String maxAttemptsPath = null;

public ActionForward perform( ActionMapping mapping, ActionForm
        form, HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {

    // Set forward paths, using request information
    ForwardPaths forwardPaths = getForwardPaths(mapping,request);
    String successPath = forwardPaths.getSuccessPath();
    String failedPath = forwardPaths.getFailedPath();
    String maxAttemptsPath = forwardPaths.getMaxAttemptsPath();
  ...
Depending on processing outcome, forward to configured path
}

private ForwardPaths setForwardPaths(ActionMapping mapping, HttpServletRequest request) {
   // Compute and store local variables success, failed, maxAttempt representing paths
   ...
   return new ForwardPaths(success, failed, maxAttempts);
}


 Eliminating the member fields makes the class threadsafe. Introduction of a value object
 (ForwardPaths inner class not shown) enables data to be safely transmitted to/from the private
 helper method.
                                             14
Deadlock
                  Thread 1                              Thread 2

                   Acquire lock A

                                                        Acquire lock B

                  Block waiting to
                   acquire lock B


                                                        Block waiting to
                                                         acquire lock A



Thread 1 can’t proceed until it gets the lock that Thread 2 is holding on B. Thread 2 can’t
proceed until it gets the lock that Thread 1 is holding on A. No other threads can do anything
that requires either lock. This kind of situation can cause application servers to “hang.”

                                               15
DeadLock Example
Apache Commons Pool provides        Maintenance Thread           Client Thread
a generic object pooling API and
implementations
Apache Commons DBCP is a            Acquire pool lock
database connection pool that                                  Acquire factory
uses Commons Pool as its                                            lock
underlying object pool
                                     Block waiting to
When used together, versions         acquire factory
1.2-1.4 of Commons Pool and                lock
1.1-1.2.x of Commons DBCP
could create a deadlock (tracked                                Block waiting to
as DBCP-44 in ASF JIRA)                                        acquire pool lock
Source of the deadlock:
Contention between client
threads borrowing / returning       Resolution: Modify pool code to move all
objects and a maintenance thread    invocations of factory methods outside of
for locks on the pool and objects   synchronized scope (so lock in 0 is released before
used by the pool’s object factory   step 2 is attempted)
                                        16
Avoiding Deadlocks
Rule number 0: Leave concurrency management to external resource providers or fully
tested frameworks - try to design around the need for synchronization
Rule number 1: When holding a lock, never call any methods that require other locks
Would completely eliminate possibility of deadlock; but unfortunately not always possible
Rule number 2: When you must use multiple locks to perform actions in your application,
establish a hierarchy of locks and always acquire locks in the order determined by the
hierarchy
Rule number 3: Maintain a complete and consistent execution model of application
subsystems and make explicit (via both documentation and test cases) invariants that enforce
lock hierarchy and/or prevent contention for locks
Rule number 4: Pay special attention to exception processing, especially when using explicit
locks (i.e., objects that implement the Lock interface) - make sure locks are released on all
execution paths




                                             17
Resources
Java Threads, Scott Oaks, Henry Wong (http://oreilly.com/catalog/9780596007829/index.html?
CMP=ILL-4GV796923290)
The Little Book of Semaphores, Allen B Downey (http://greenteapress.com/semaphores/)
“Concurrent Programming with J2SE 1.5” (http://java.sun.com/developer/technicalArticles/
J2SE/concurrency/) Also read carefully the class javadoc for all of the classes in the
concurrency package
“Hierarchical Ordering of Sequential Processes”, Edsker Dijkstra, working paper (http://
userweb.cs.utexas.edu/users/EWD/ewd03xx/EWD310.PDF)
“Sequential Programming vs Concurrent Programming”, Jerry Cain (http://academicearth.org/
lectures/sequential-programming-concurrent-programming)
A little “OT”:
“On the Nature of Time - Why does Nature Abhor Deadlocks?”, Christine Cordula Dantas
(http://www.fqxi.org/data/essay-contest-files/Dantas_Nattime2_1.pdf?
phpMyAdmin=0c371ccdae9b5ff3071bae814fb4f9e9)



                                            18

More Related Content

Similar to Concurrent Programming Techniques

Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8Heartin Jacob
 
Java concurrency in practice
Java concurrency in practiceJava concurrency in practice
Java concurrency in practiceDeon Huang
 
Concurrency: Best Practices
Concurrency: Best PracticesConcurrency: Best Practices
Concurrency: Best PracticesIndicThreads
 
Concurrent Programming in Java
Concurrent Programming in JavaConcurrent Programming in Java
Concurrent Programming in JavaLakshmi Narasimhan
 
Thread model of java
Thread model of javaThread model of java
Thread model of javamyrajendra
 
Multi threading
Multi threadingMulti threading
Multi threadinggndu
 
Java multithreading
Java multithreadingJava multithreading
Java multithreadingMohammed625
 
Java Multithreading
Java MultithreadingJava Multithreading
Java MultithreadingRajkattamuri
 
Multithreading
MultithreadingMultithreading
MultithreadingF K
 
Introduction to Java Programming Language
Introduction to Java Programming LanguageIntroduction to Java Programming Language
Introduction to Java Programming LanguageJunji Zhi
 
Asynchronous Python A Gentle Introduction
Asynchronous Python A Gentle IntroductionAsynchronous Python A Gentle Introduction
Asynchronous Python A Gentle IntroductionPyData
 
Java gc
Java gcJava gc
Java gcNiit
 
Java Garbage Collection
Java Garbage CollectionJava Garbage Collection
Java Garbage CollectionVinay H G
 

Similar to Concurrent Programming Techniques (20)

Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8
 
Java concurrency in practice
Java concurrency in practiceJava concurrency in practice
Java concurrency in practice
 
Java and the JVM
Java and the JVMJava and the JVM
Java and the JVM
 
Adsa u4 ver 1.0
Adsa u4 ver 1.0Adsa u4 ver 1.0
Adsa u4 ver 1.0
 
Concurrency: Best Practices
Concurrency: Best PracticesConcurrency: Best Practices
Concurrency: Best Practices
 
Concurrency in Java
Concurrency in JavaConcurrency in Java
Concurrency in Java
 
Concurrent Programming in Java
Concurrent Programming in JavaConcurrent Programming in Java
Concurrent Programming in Java
 
Thread model of java
Thread model of javaThread model of java
Thread model of java
 
Java
JavaJava
Java
 
Multi threading
Multi threadingMulti threading
Multi threading
 
multithreading
multithreadingmultithreading
multithreading
 
Java
JavaJava
Java
 
Java threading
Java threadingJava threading
Java threading
 
Java multithreading
Java multithreadingJava multithreading
Java multithreading
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Introduction to Java Programming Language
Introduction to Java Programming LanguageIntroduction to Java Programming Language
Introduction to Java Programming Language
 
Asynchronous Python A Gentle Introduction
Asynchronous Python A Gentle IntroductionAsynchronous Python A Gentle Introduction
Asynchronous Python A Gentle Introduction
 
Java gc
Java gcJava gc
Java gc
 
Java Garbage Collection
Java Garbage CollectionJava Garbage Collection
Java Garbage Collection
 

More from Phil Steitz

Programming Math in Java - Lessons from Apache Commons Math
Programming Math in Java - Lessons from Apache Commons MathProgramming Math in Java - Lessons from Apache Commons Math
Programming Math in Java - Lessons from Apache Commons MathPhil Steitz
 
Apache Commons Pool and DBCP - Version 2 Update
Apache Commons Pool and DBCP - Version 2 UpdateApache Commons Pool and DBCP - Version 2 Update
Apache Commons Pool and DBCP - Version 2 UpdatePhil Steitz
 
Leadership model
Leadership modelLeadership model
Leadership modelPhil Steitz
 
Open Development in the Enterprise
Open Development in the EnterpriseOpen Development in the Enterprise
Open Development in the EnterprisePhil Steitz
 
Commons Pool and DBCP
Commons Pool and DBCPCommons Pool and DBCP
Commons Pool and DBCPPhil Steitz
 
Principles of Technology Leadership
Principles of Technology LeadershipPrinciples of Technology Leadership
Principles of Technology LeadershipPhil Steitz
 

More from Phil Steitz (7)

Programming Math in Java - Lessons from Apache Commons Math
Programming Math in Java - Lessons from Apache Commons MathProgramming Math in Java - Lessons from Apache Commons Math
Programming Math in Java - Lessons from Apache Commons Math
 
Apache Commons Pool and DBCP - Version 2 Update
Apache Commons Pool and DBCP - Version 2 UpdateApache Commons Pool and DBCP - Version 2 Update
Apache Commons Pool and DBCP - Version 2 Update
 
Commons Nabla
Commons NablaCommons Nabla
Commons Nabla
 
Leadership model
Leadership modelLeadership model
Leadership model
 
Open Development in the Enterprise
Open Development in the EnterpriseOpen Development in the Enterprise
Open Development in the Enterprise
 
Commons Pool and DBCP
Commons Pool and DBCPCommons Pool and DBCP
Commons Pool and DBCP
 
Principles of Technology Leadership
Principles of Technology LeadershipPrinciples of Technology Leadership
Principles of Technology Leadership
 

Recently uploaded

Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 

Recently uploaded (20)

Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 

Concurrent Programming Techniques

  • 2. Agenda • Motivational Examples • Computing Model • Race Conditions • Deadlocks • Resources 2
  • 3. Sleeping Barber “BAU” Process Barber cuts hair When finished, he checks the waiting room If no one is there, he goes to sleep in the chair Customers arriving while the barber is asleep wake the barber What can go wrong? Barber finishes, walks to waiting room Customer arrives and goes to wake the barber They do not see each other, return and wait indefinitely 3
  • 4. Dining Philosophers “BAU” Process Philosophers alternate between thinking and eating Two forks are required to eat spaghetti When finished thinking, philosophers seek forks When finished eating, philosophers put forks down What can go wrong? Process of picking up, putting down forks can result in each philosopher holding one fork - none can eat Dining Philosophers Demo http://www.doc.ic.ac.uk/~jnm/concurrency/classes/Diners/Diners.html 4
  • 5. Computing Canonical Model • Processes spawn threads to do work • Threads maintain local state but can also access shared state • Threads can be active, suspended, blocked waiting for resources or sleeping • Threads execute concurrently - truly concurrently in multi-processor environments, virtually concurrently in single- processor environments • Thread scheduling is managed by the parent process, operating system and / or application • Sequencing of operations across different threads is in general unpredictable Shared State 5
  • 6. Simple Computing Problem Bad Timing Web App Hit Counter • Multi-threaded web app wants to maintain a count of the number of hits it gets Read counter • Shared state is used to maintain the count • Request processing threads update the count each time they process a request • Under heavy load, the app seems to “miss” Read counter some hits. Why? Assign counter+1 public void increment() { Assign counter+1 count = count + 1; } Badness results from B reading the counter while A is in process of updating it. This is an example of a race condition. 6
  • 7. Solution Semaphore Keep B out while A updates • Could make “count++” operation atomic (i.e. unable to be interrupted.) The JDK 1.5 AtomicInteger class does this. Close the door • More general solution is to synchronize access to the shared resource • Using a semaphore, Thread A signals exclusive access to the critical section of code that does the update Knock on door • Simplest semaphore is a mutex (mutual exclusion - just one allowed in at a time) and simplest implementation Update, open door in Java is via the synchronized keyword Close door, update public synchronized void increment() { count = count + 1; } Note that the same solution would work for the sleeping barber (close door to waiting room) 7
  • 8. Synchronized Keyword in Java • Acts as a mutex on blocks of code • Can protect entire methods, or blocks within methods • Uses the concept of a monitor on an object to represent the mutex - code entering a synchronized block acquires an object monitor • When no object is specified, the monitor on the object executing the code is implied • Synchronization locks are reentrant - i.e., if a thread owns an object’s monitor and it enters another method that requires the same monitor, it is allowed in • When a thread leaves a synchronized block, it releases the associated monitor (even if an exception occurs) Parent object’s monitor Fu’s monitor public synchronized void foo() { public void foo() { public void foo() { fu.bar(); synchronized (this) { synchronized (fu) { } fu.bar(); fu.bar(); } } } } 8
  • 9. Concurrent Object Access Method Object Threads Object References When multiple threads share references to the same object, they can concurrently call the same method on the object, unless synchronization prevents this. A class is thread-safe if concurrently executing threads can safely hold references to instances of the class. 9
  • 10. Programmer’s Challenge: Situational Awareness • The question always needs to be asked, “will multiple threads hold references to instances of this class at the same time?” Unfortunately, it is sometimes hard to know. • Most common use cases involving concurrent access are created by application containers, often in conjunction with application frameworks. Containers manage threads and frameworks instantiate objects. Fun begins when the Singleton Pattern is used by the framework. Primitive Survival Technique: Avoid the bad neighborhood altogether • Keep is Stateless Stupid (KISS) - avoid maintaining state across method calls • Prefer immutable objects - only final fields, all set by constructors • Never spawn threads - let the container manage them • Use tested, thread-safe resource providers (e.g. databases) to maintain state (when you are dragged kicking and screaming into acknowledging the need to maintain state at all) • Encapsulate parameters needed by methods in value objects and pass these to/from methods instead of relying on instance fields 10
  • 11. Anatomical Example public class Foo { public static final int HOSIERY_THRESHOLD = 1000; No worries private boolean fubar; ... public void hose(int ways) { Careful! int newWays = generateNewWays(); if (ways + newWays > HOSIERY_THRESHOLD) { fubar = true; } } • Static final fields are initialized when the class is loaded and don’t change thereafter, so all threads see the same values - threadsafe • Method parameters are provided to each thread on the call stack and are only visible to that thread for the duration of the method - threadsafe • Local variables declared within methods are only visible to the thread activating the method for the duration of the method - threadsafe • Class member fields are visible to all threads, so if two threads can be inside a method that uses a member field at the same time, care must be taken to synchronize access 11
  • 12. Example - Struts 1.x Actions Servlet Container 1. When the web app starts, it loads ActionServlet the Struts ActionServlet (one ActionServlet instance per web FooAction application - a singleton) BarAction 2. When the ActionServlet is initialized, it reads its configuration and loads one instance each of the Action classes defined in its configuration FubarAction (more singletons) 3. When browser requests arrive, the container assigns processing threads to requests and passes requests mapped to the Struts application to the Struts ActionServlet instance 4. The Struts ActionServlet calls perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) on the single Action instance that it uses for all requests Consequence: Struts Action classes must be threadsafe! rtfm://struts.apache.org/1.0.2/userGuide/building_controller.html#action_classes 12
  • 13. Struts 1.x Example (Cont.) Inside a Struts Action Class: private String successPath = null; private String failedPath = null; private String maxAttemptsPath = null; public ActionForward perform( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // Set forward paths, using request information setForwardPaths(mapping, request ); ... Depending on processing outcome, forward to configured path } private void setForwardPaths(ActionMapping mapping, HttpServletRequest request) { ... successPath = ... failedPath = ... maxAttemptsPath = ... } Problem: If two requests for this action are processed concurrently, and the computed forward paths are different, control can be misdirected. 13
  • 14. Struts 1.x Example - Fix private String successPath = null; private String failedPath = null; private String maxAttemptsPath = null; public ActionForward perform( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // Set forward paths, using request information ForwardPaths forwardPaths = getForwardPaths(mapping,request); String successPath = forwardPaths.getSuccessPath(); String failedPath = forwardPaths.getFailedPath(); String maxAttemptsPath = forwardPaths.getMaxAttemptsPath(); ... Depending on processing outcome, forward to configured path } private ForwardPaths setForwardPaths(ActionMapping mapping, HttpServletRequest request) { // Compute and store local variables success, failed, maxAttempt representing paths ... return new ForwardPaths(success, failed, maxAttempts); } Eliminating the member fields makes the class threadsafe. Introduction of a value object (ForwardPaths inner class not shown) enables data to be safely transmitted to/from the private helper method. 14
  • 15. Deadlock Thread 1 Thread 2 Acquire lock A Acquire lock B Block waiting to acquire lock B Block waiting to acquire lock A Thread 1 can’t proceed until it gets the lock that Thread 2 is holding on B. Thread 2 can’t proceed until it gets the lock that Thread 1 is holding on A. No other threads can do anything that requires either lock. This kind of situation can cause application servers to “hang.” 15
  • 16. DeadLock Example Apache Commons Pool provides Maintenance Thread Client Thread a generic object pooling API and implementations Apache Commons DBCP is a Acquire pool lock database connection pool that Acquire factory uses Commons Pool as its lock underlying object pool Block waiting to When used together, versions acquire factory 1.2-1.4 of Commons Pool and lock 1.1-1.2.x of Commons DBCP could create a deadlock (tracked Block waiting to as DBCP-44 in ASF JIRA) acquire pool lock Source of the deadlock: Contention between client threads borrowing / returning Resolution: Modify pool code to move all objects and a maintenance thread invocations of factory methods outside of for locks on the pool and objects synchronized scope (so lock in 0 is released before used by the pool’s object factory step 2 is attempted) 16
  • 17. Avoiding Deadlocks Rule number 0: Leave concurrency management to external resource providers or fully tested frameworks - try to design around the need for synchronization Rule number 1: When holding a lock, never call any methods that require other locks Would completely eliminate possibility of deadlock; but unfortunately not always possible Rule number 2: When you must use multiple locks to perform actions in your application, establish a hierarchy of locks and always acquire locks in the order determined by the hierarchy Rule number 3: Maintain a complete and consistent execution model of application subsystems and make explicit (via both documentation and test cases) invariants that enforce lock hierarchy and/or prevent contention for locks Rule number 4: Pay special attention to exception processing, especially when using explicit locks (i.e., objects that implement the Lock interface) - make sure locks are released on all execution paths 17
  • 18. Resources Java Threads, Scott Oaks, Henry Wong (http://oreilly.com/catalog/9780596007829/index.html? CMP=ILL-4GV796923290) The Little Book of Semaphores, Allen B Downey (http://greenteapress.com/semaphores/) “Concurrent Programming with J2SE 1.5” (http://java.sun.com/developer/technicalArticles/ J2SE/concurrency/) Also read carefully the class javadoc for all of the classes in the concurrency package “Hierarchical Ordering of Sequential Processes”, Edsker Dijkstra, working paper (http:// userweb.cs.utexas.edu/users/EWD/ewd03xx/EWD310.PDF) “Sequential Programming vs Concurrent Programming”, Jerry Cain (http://academicearth.org/ lectures/sequential-programming-concurrent-programming) A little “OT”: “On the Nature of Time - Why does Nature Abhor Deadlocks?”, Christine Cordula Dantas (http://www.fqxi.org/data/essay-contest-files/Dantas_Nattime2_1.pdf? phpMyAdmin=0c371ccdae9b5ff3071bae814fb4f9e9) 18