• No results found

SSC - Concurrency and Multi-threading Java multithreading programming - Synchronisation (II)

N/A
N/A
Protected

Academic year: 2021

Share "SSC - Concurrency and Multi-threading Java multithreading programming - Synchronisation (II)"

Copied!
14
0
0

Loading.... (view fulltext now)

Full text

(1)

SSC - Communication and Networking

SSC - Concurrency and Multi-threading

Java multithreading programming

-Synchronisation (II)

Shan He

School for Computational Science University of Birmingham

(2)

Outline of Topics

Review what we learned

More abouts Synchronized methods

Java synchronized statements

(3)

SSC - Communication and Networking Review what we learned

(4)

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;

(5)

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

(6)

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

(7)

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

(8)

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;

(9)

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:

(10)

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:

(11)

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

(12)

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

(13)

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

(14)

Deadlock

References

Related documents

The study can be regarded as a proof-of-concept for a new, integrated multimodel approach, integrating 3D-printing based modeling in the cyclone simulation and optimization loop..

The objective of the project is to analysis material flow for injection molding process in order to get the suitable runner and gate system and design the two-plate injection mold

ing the exits status of the terminated thread { thr join in other processes, or the current thread cannot wait for detached threads, threads. Thread Suspend

– Every synchronized method of an object has a monitor – One thread inside a synchronized method at a time.. – All other threads block until

In the end, CCGA recommended Council inform the Provost that effective immediately, CCGA has decided to invoke its delegated authority to require review of graduate programs

(E) WPS6 purified from the anoxic control assay.. 3,17-dione, androsta-1,4-diene-3,17-dione, WPS1, or WPS2) as the sole carbon source corroborates the conclusion that these

T HE CONCEPT DOES NOT PROPOSE THAT THE ARCHITECTURE ITSELF HAS THE ABILITY TO HEAL , BUT THAT THE ARCHITECTURAL MANIPULATION OF SPACE CAN PROVIDE THE PLATFORM FOR OTHER NATURAL

Not only does Motor Inn of Knoxville take care of your vehicle at an unfortunate time that the body shop is needed, we also offer complete warranty repair service on all General