SSC - Concurrency and Multi-threading Java multithreading programming - Synchronisation (II) Shan He School for Computational Science University of Birmingham Module 06-19321: SSC
Outline Outline of Topics Review what we have learned More about Synchronized methods Liveness: deadlock
Review what we have learned High level synchronisation in Java High level concurrency tools Executors framework Fork/Join framework
Review what we have learned Low level synchronisation in Java Low level concurrency tools Volatile Variables Locks Atomic Operations Reentrant Locks Synchronized keyword Synchronized methods Synchronized statements
More about Synchronized methods How to use Java synchronized methods Simply add the synchronized keyword to method declaration: Example (See my Java source code for details): for a method public void add(long value){...} the synchronised method is: public synchronized void add(long value){...} The synchronized keyword automatically acquires the intrinsic lock for that method s object and releases it when the method finishes. A synchronized method: prevents invocations of synchronized methods on the same object; and automatically establishes a happens-before relationship
More about Synchronized methods Java synchronized methods: how do they work? Steps: Step 1: Thread A executes a synchronized method Step 2: Thread A automatically acquires the intrinsic lock for that method s object Step 3: Suspend execution of other threads that invoke synchronized methods for the same object since they don t have the intrinsic lock Step 4: Thread A releases the intrinsic lock when it is finished with the object. Step 5: Another thread scheduled by the scheduling algorithm acquires the intrinsic lock to subsequently execute its synchronised methods for the same object Note: Java guarantees that changes to the state of the object by a thread are visible to all threads.
More about Synchronized methods Java synchronized methods: drawbacks A synchronised method synchronises on the method s object instance or the class: cannot synchronise some parts of your code coarse grained, lack of granular control over lock, Thread synchronises on the whole object instead of a smaller critical section slow; You cannot set an intrinsic lock on an external object using synchronized methods.
Also called synchronized blocks, which can solve all the drawbacks of synchronized methods. Use to mark a block of code as synchronized The block is also called critical section where one and only one thread is executing Usage: Use the following statement to mark a synchronized block: synchronized(object){ synchronize operations on the object }
How to use Example 1 (See my Java source code for details): public void add(int value) { synchronized(this){ this.count += value;} } The above code is equivalent to: public synchronized void add(int value) { this.count += value;}
Synchronized statements must specify the object that provides the intrinsic lock. e.g., synchronized(object) In Example 1, we use synchronized(this){...} The object this is the instance the add method is called on The object specify by the synchronized statement is called a monitor object The code is said to be synchronized on the monitor object Only one thread can execute inside a Java code block synchronized on the same monitor object.
Question: synchronize operations on object Is the following code correct? long r1 = 1; synchronized(r1) { r1++; }
Answer: synchronize operations on object We can only synchronize on objects but long is a primitive value that doesn t have a lock We we can use Long which is an object: Long r1 = 1; synchronized(r1) { r1++; }
Question: synchronised on a static method If you have a static method, whether synchronized methods still work? Reminder 1: a static method belongs to the class, not to the specific objects of that class, that is, a static method has no associated object. Reminder 2: Synchronized methods prevent invocations of synchronized methods on the same object
Answer: synchronising a class For a class (static) method, the monitor associated with the Class object for the method s class is used. For example, synchronized static void foo() {... } is equivalent to static void foo() { synchronized(someclass.class) {... } } If one thread is executing a static synchronized method, any other threads executing any static synchronized methods in the same class will be blocked. However, non-static synchronized methods will not be blocked since they require the lock on the instance, i.e., synchronized(this){...}
Liveness: deadlock Liveness and Deadlock Liveness property in concurrent programming means something good eventually happens. More specifically, the ability of an application to execute in a timely manner. Liveness problems include deadlock, starvation and livelock Deadlock: a situation threads are blocked forever, waiting for each other: Can be two threads waiting for another to release a lock Can be more than two threads are waiting for resources in a circular chain
Liveness: deadlock 4 Necessary conditions for deadlock A deadlock situation will happen if all of the following conditions (called Coffman conditions) hold simultaneously: Mutual exclusion: at least one resource must be held in a non-sharable mode: Only one thread at a time can use the resource If another thread requests that resource, the requesting thread must wait until the resource has been released Hold and wait: Thread holds one resource while waits for another No preemption: Once a thread is holding a resource, then that resource cannot be taken away from that thread until the thread voluntarily releases it. Circular wait: A thread must be waiting for a resource which is being held by another thread, which in turn is waiting for the first thread to release the resource.
Liveness: deadlock Deadlock A Java example