SSC - Communication and Networking
SSC - Concurrency and Multi-threading
Java multithreading programming
-Synchronisation (II)
Shan He
School for Computational Science University of Birmingham
Outline of Topics
Review what we learned
More abouts Synchronized methods
Java synchronized statements
SSC - Communication and Networking Review what we learned
How to use Java synchronized methods
I Simply add the synchronized keyword to method
declaration:
I For example: for a method
public void add(long value){...} the synchronised method is:
public synchronized void add(long value){...}
I The synchronized keyword automatically acquires the
intrinsic lock for that method’s object and releases it when the method finishes.
I A synchronized method:
I prevents invocations of synchronized methods on the same object;
SSC - Communication and Networking More abouts Synchronized methods
Java synchronized methods: how they work?
I Steps:
I Step 1: Thread A executes a synchronized method
I Step 2: Thread A automatically acquires the intrinsic lock for that method’s object
I Step 3: Suspend execution of other threads that invoke synchronized methods for the same object since they don’t have the intrinsic lock
I Step 4: Thread A releases the intrinsic lock when it is finished with the object.
I Step 5: Another thread scheduled by the scheduling algorithm acquires the intrinsic lock to subsequently execute its
synchronised methods for the same object
I Note: Java guarantees that changes to the state of the object
Question: synchronised on a static method
I If you have a static method, whether synchronized methods
still work?
I 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.
I Reminder 2: Synchronized methods “prevent invocations of
SSC - Communication and Networking More abouts Synchronized methods
Answer: synchronising a class
I For a class (static) method, the monitor associated with the Class object for the method’s class is used.
I If one thread is executing a static synchronized method, any
Java synchronized methods: drawbacks
I 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,
I Thread synchronises on the whole object instead of a smaller
critical section −→ slow;
SSC - Communication and Networking Java synchronized statements
Java synchronized statements
I Also called synchronized blocks, which can solve all the drawbacks of synchronized methods.
I Use to mark a block of code as synchronized
I The block is also called critical section where one and only one thread is executing
I Usage: Use the following statement to mark a synchronized
block: synchronized(object){
synchronize operations on object }
I Example:
How to use Java synchronized statements
I Example 2:
public void add(int value) { synchronized(this){
this.count += value;} }
I The above code is equivalent to:
SSC - Communication and Networking Java synchronized statements
Java synchronized statements
I Synchronized statements must specify the object that provides
the intrinsic lock. e.g.,
I In th above example, we use synchronized(this){...}
I The object this is the instance the add method is
called on
I The object specify by the synchronized statement is called a
monitor object
I The code is said to be synchronized on the monitor object
I Only one thread can execute inside a Java code block
Liveness and Deadlock
I Liveness property in concurrent programming means
something good eventually happens. More specifically, the ability of an application to execute in a timely manner.
I Liveness problems include deadlock, starvation and livelock
I Deadlock: a situation threads are blocked forever, waiting for each other:
I Can be two threads waiting for another to release a lock I Can be more than two processes are waiting for resources in a
SSC - Communication and Networking Liveness: deadlock
4 Necessary conditions for deadlock
A deadlock situation will happen if all of the following conditions (called Coffman conditions) hold simultaneously:
I Mutual exclusion: at least one resource must be held in a
non-sharable mode:
I Only one thread at a time can use the resource
I If another thread requests that resource, the requesting thread must wait until the resource has been released
I Hold and wait: Thread holds one resource while waits for
another
I 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.
I Circular wait: A thread must be waiting for a resource which