ssc - concurrency and multi-threading thread coordination...
TRANSCRIPT
SSC - Concurrency and Multi-threading
SSC - Concurrency and Multi-threadingThread Coordination (I)
Shan He
School for Computational ScienceUniversity of Birmingham
Module 06-19321: SSC
SSC - Concurrency and Multi-threading
Outline
Outline of Topics
Review what we learned
Thread signalling
Coordination toolsGuarded blockCountdownLatchCyclicBarrierSemaphore
SSC - Concurrency and Multi-threading
Thread signalling
Thread signalling
I Threads often have to coordinate their actions:I several threads start at the same time;I a thread waits for other threads to finish
I To coordinate, they need to signal each otherI Two types of threads signals
I Synchronous (what we are dealing):I Occur as a direct result of thread executionI Should be delivered to currently executing thread
I Asynchronous:I Occur due to an event typically unrelated to the current
instructionI Threading library must determine each signals recipient so
that asynchronous signals are delivered properly
I Each thread might receive a set of synchronous signals but itcan mask all signals except those that it wishes to receive
SSC - Concurrency and Multi-threading
Thread signalling
Thread signalling in Java
I For threads who use locks for synchronisation, the mainpurpose of signalling is communicate the lock status of aresource
I Three methods can be used:I wait() : Causes the current thread to wait (suspend and
also release its lock) until another thread invokes the notify()method or the notifyAll() method for this object.
I notify() : Wakes up a single thread that is waiting on this
object’s lock.I notifyAll() : Wakes up all threads that are waiting on this
object’s lock.
I Question: What class shall these methods belong to?
I There are idioms or tools in Java for coordinating theexecution of multiple threads
SSC - Concurrency and Multi-threading
Coordination tools
Guarded block
Guarded blockI Guarded block: The most common coordination idiomI Basic idea: wait for a particular condition to become true and
only in that case the actual execution of the thread resumes.I A simple example: a method that must not proceed until a
shared boolean variable has been set by another thread.I A naive implementation:
public void guardedWaitingTransaction() {while(!PaidFlag) {System.out.println("Pay me!!");
}System.out.println("Transaction finished!!");
}I What’s wrong with the above example?
SSC - Concurrency and Multi-threading
Coordination tools
Guarded block
Guarded block: using thread signallingI The above naive implementation is non-synchronized guarded
blockI Use a blank loop until the condition becomes true −→
wasting the precious CPU timeI We should use synchronized guarded block:
I Current thread is suspended to wait for the condition becomestrue
I It releases the acquired lock on that object −→ leaves theprocessor to be used by other threads
I We can use Java thread signalling methods to achieve thisI Steps:
I Invoke wait inside a loop that tests for the condition beingwaited for, also release the lock
I Another thread who acquires the same lock invokesnotifyAll to informing all threads waiting on that lock
that something important has happened.
SSC - Concurrency and Multi-threading
Coordination tools
Guarded block
Guarded block: example
I A more efficient example:public synchronized void guardedWaitingTransaction() {while(!PaidFlag) {try {wait();
} catch (InterruptedException e) {}}System.out.println("Transaction finished!!");
}I Q: Why used synchronized ?
I A: This ensures the current thread must own this object’s lock
SSC - Concurrency and Multi-threading
Coordination tools
Guarded block
Guarded block: example
I To notify the thread(s) who is waiting for the object’s lock:
public synchronized void paymoney(Integer value) {PaidFlag = true;
sum+= value;
notifyAll();
}
I Note 1: make sure notify() and notifyAll() should
be used with synchronized keyword
I Note 2: notify() only wakes up a single thread and alsodoes not specify which thread to be woken up.
I Rule of thumb: use notifyAll()
SSC - Concurrency and Multi-threading
Coordination tools
CountdownLatch
What is a countdown latch?
I Latch: a type of coordination tool that can be seen as a”switch” or ”trigger” in concurrent programming
I A countdown latch usually associated with a count value
I The latch is capable to count down the count value
I A thread or threads waits for the count value to reach zerobefore continuing to perform some process
I One-off process: Once the count value reaches 0, you cannotreset
SSC - Concurrency and Multi-threading
Coordination tools
CountdownLatch
How to use countdown latch?
I Steps:I Step 1: Create a CountDownLatch object with the initial
count valueI Step 2: Call countDown() to decrease the count by 1;
I Stpe 3: Use await() method in thread to wait for thecount to reach zero, or proceed immediately if the countalready reached zero.
I Usages: A CountDownLatch initialized to N can be used tomake one thread
I wait until N threads have completed some action, orI wait until some action has been completed N times.
SSC - Concurrency and Multi-threading
Coordination tools
CountdownLatch
What is a countdown latch?
3
3
2
1
0
0
Cnt=3
Ta await()
T1
T2
T3
countdown()
countdown()
countdown()
Ta awaiting...
Continue
Continue
Continue
Cnt=0
Resume Ta
Ta Resume
SSC - Concurrency and Multi-threading
Coordination tools
CyclicBarrier
What is a CyclicBarrier?
I CyclicBarrier: a coordination tool “that allows a set of threadsto all wait for each other to reach a common barrier point”,
I CyclicBarrier: a barrier that all threads must wait at, nothread can continue until all threads reach the barrier.
I Cyclic: because the barrier can be re-used after the waitingthreads are released.
I How to use CyclicBarrier :
I Step 1: Create a CyclicBarrier object with the 1).
number of parties (threads) are waiting upon it; and 2) abarrier action when all threads arrive at the barrier.
I Step 2: The threads wait for each other by calling the await()method on the CyclicBarrier .
SSC - Concurrency and Multi-threading
Coordination tools
CyclicBarrier
What is a CyclicBarrier?
Thread 1 Thread N
CyclicBarrier 1
CyclicBarrier 2
Wait Wait
Wait Wait
Continue Continue
Barrier action
SSC - Concurrency and Multi-threading
Coordination tools
Semaphore
What is a Semaphore?
I Semaphore: a variable or abstract data type that is used forcontrolling access, by multiple processes or threads, to acommon resource in concurrent programming
I Very simple idea: if the Semaphore value is 0, an attempt todecrement this value will cause the calling thread to wait untilsome other thread increments it.
I Invented by the famous Dutch computer scientist EdsgerDijkstra in 1965
I In Java, it is called counting semaphore, which maintains a setof permits (Semaphore value).
I Usage:I to restrict the number of threads than can access some
resource.I to send signals between threads.
SSC - Concurrency and Multi-threading
Coordination tools
Semaphore
How Semaphore works?
2
1
0
0
0
1
Cnt=2
T1 acquire()
T2
T3
T4
acquire()
acquire()
acquire()
continue
continue
0
1
0
1
2
awaiting...
awaiting...
T2release()continue
continueT4
T4release()continue
T3continue
T1release()continue
T3release()continue
SSC - Concurrency and Multi-threading
Coordination tools
Semaphore
How to use Semaphore?
I Steps:I Step 1: Create a Semaphore with number of permits N
Semaphore semaphore = new Semaphore(N);
I Step 2: Use acquire() and release() to define the criticalsection:semaphore.acquire();
//define your critical section
semaphore.release();
I Fairness: No guarantee that the first thread to call acquire()is also the first thread to obtain a permit.
I To enforce fairness, pass a boolean argumentboolean fair in the Semaphore class constructor:
Semaphore semaphore = new Semaphore(1, true);