1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.

22
1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh Chris Greenhalgh School of Computer School of Computer Science Science

description

3 Wait/notify Overview Every Java object has a wait queue as well as a lockEvery Java object has a wait queue as well as a lock Only a thread holding the lock can manipulate the wait queueOnly a thread holding the lock can manipulate the wait queue The queue is manipulated using the java.lang.Object methods wait(…) and notify() / notifyAll() …The queue is manipulated using the java.lang.Object methods wait(…) and notify() / notifyAll() … –All classes extend java.lang.Object Lock Object Wait queue Method… wait notify

Transcript of 1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.

Page 1: 1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.

1

G53SRP: Java Concurrency Control (2) – wait/notify

Chris GreenhalghChris GreenhalghSchool of Computer ScienceSchool of Computer Science

Page 2: 1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.

2

Contents• Wait/notify overview Wait/notify overview • Wait()Wait()• Notify()Notify()• Infinite bufferInfinite buffer• General concurrency controlGeneral concurrency control• Reader/writer lockReader/writer lock• Timed waitTimed wait• SummarySummary • Book: Wellings 3.2Book: Wellings 3.2

Page 3: 1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.

3

Wait/notify Overview• Every Java object has a wait Every Java object has a wait

queue as well as a lockqueue as well as a lock• Only a thread holding the lock Only a thread holding the lock

can manipulate the wait queuecan manipulate the wait queue• The queue is manipulated using The queue is manipulated using

the the java.lang.Objectjava.lang.Object methods methods wait(…)wait(…) and and notify()notify()/ / notifyAll()notifyAll()……– All classes extend java.lang.ObjectAll classes extend java.lang.Object

Lock

Object

Wait queue

Method…

wait

notify

Page 4: 1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.

4

wait()• A thread calling A thread calling waitwait is added to the wait is added to the wait

queuequeue– (not necessarily the end)(not necessarily the end)

• It releases the object lock while waitingIt releases the object lock while waiting• It re-obtains the object lock before returningIt re-obtains the object lock before returning• Why use wait?Why use wait?

– Efficient task cooperation (avoids polling and Efficient task cooperation (avoids polling and avoids race conditions or lock-out with sleep)avoids race conditions or lock-out with sleep)

Page 5: 1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.

5

Wait detail• ……synchronized (this) {synchronized (this) { try { try {

this. this.waitwait();();

} } catch (InterruptedException ie) { catch (InterruptedException ie) { } }}}

Gains lock on ‘this’

Releases lock on ‘this’

Releases lock on ‘this’

Thread

haslock

haslock

Added to wait queue (time passes)Woken from wait queue (time passes)Re-gains lock on ‘this’

Page 6: 1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.

6

notify()• A thread calling A thread calling notify()notify() wakes wakes

one thread(s) from the wait queueone thread(s) from the wait queue• A thread calling A thread calling notifyAll()notifyAll() wakes all wakes all

thread(s) from the wait queuethread(s) from the wait queue• If there are no threads waiting then the calls have If there are no threads waiting then the calls have

no effectno effect– Notifies are not “queued” or “persistent”Notifies are not “queued” or “persistent”

• Waiting threads are not subdivided or classifiedWaiting threads are not subdivided or classified– Notify cannot wake a certain kind of thread or methodNotify cannot wake a certain kind of thread or method– => less efficient than some alternative=> less efficient than some alternative

Page 7: 1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.

7

Infinite bufferClass Buffer<T> {Class Buffer<T> { Vector<T> data =Vector<T> data =

new Vector<T>(); new Vector<T>(); synchronizedsynchronized void void

putput(T item) {(T item) { data.add(item);data.add(item); notify()notify();; }} … …

…… public public synchronizedsynchronized

T T getget() () {{ while (data.size()==0) {while (data.size()==0) { try {try { wait()wait();; } } catch(InterruptedException ie) {}catch(InterruptedException ie) {}

}} T item = data.get(0);T item = data.get(0); data.remove(0);data.remove(0); return item;return item; }}}}

haslock

haslock

wakes

Page 8: 1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.

8

Infinite buffer - cases• get()get(), data in buffer, data in buffer

– Removes and returns first itemRemoves and returns first item• get()get(), no data in buffer, no data in buffer

– Waits until data presentWaits until data present• put()put(), no thread waiting, no thread waiting

– Add data to buffer (notify is no-op)Add data to buffer (notify is no-op)• put()put(), thread(s) waiting, thread(s) waiting

– Add data to buffer, wake one threadAdd data to buffer, wake one thread

Page 9: 1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.

9

Infinite buffer - notes• notify()notify() may wake any waiting thread may wake any waiting thread

– But only threads calling get ever waitBut only threads calling get ever wait• Put only adds one item Put only adds one item

– only one waiting thread can now take this item, so only only one waiting thread can now take this item, so only needs to be woken upneeds to be woken up

• wait()wait() may terminate due to interrupt may terminate due to interrupt• A waiting thread may be beaten to the lock by a A waiting thread may be beaten to the lock by a

new new get()get() thread thread– E.g. if it was already waiting to obtain the lockE.g. if it was already waiting to obtain the lock– So it has to be ready to wait more than once!So it has to be ready to wait more than once!

Page 10: 1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.

10

General concurrency control• Thread 1Thread 1

– ……– Entry protocolEntry protocol– Critical sectionCritical section– Exit protocolExit protocol– ……

• EntryEntry::– Block until safe to Block until safe to

proceed (update state)proceed (update state)

• Thread 2Thread 2– ……– Entry protocolEntry protocol– Critical sectionCritical section– Exit protocolExit protocol– ……

• ExitExit::– (update state) Wake (update state) Wake

any blocked threads any blocked threads now able to proceednow able to proceed

Page 11: 1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.

11

Infinite buffer revisitedClass Buffer<T> {Class Buffer<T> { Vector<T> data =Vector<T> data =

new Vector<T>(); new Vector<T>(); synchronizedsynchronized void void

putput(T item) {(T item) { data.add(item);data.add(item); notify()notify();; }} … …

…… public public synchronizedsynchronized

T T getget() () {{ while (data.size==0) {while (data.size==0) { try {try { wait()wait();; } } catch(InterruptedException ie) {}catch(InterruptedException ie) {}

}} T item = data.get(0);T item = data.get(0); data.remove(0);data.remove(0); return item;return item; }}}}

entry

exit

Criticalsection

Page 12: 1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.

12

Infinite buffer notes• Object lock is held during critical sectionObject lock is held during critical section

– So at most one thread can be in critical sectionSo at most one thread can be in critical section• If more than one thread should be in critical If more than one thread should be in critical

section then lock must be released and section then lock must be released and regainedregained– E.g. separate into two synchronized blocksE.g. separate into two synchronized blocks

Page 13: 1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.

13

Synchronized method implementation

This:This:……synchronizedsynchronized void method() void method() {{

… …

}}……staticstatic synchronized synchronized

void method() {void method() {

… …

}}

Equals:Equals:……void method() void method() {{ synchronizedsynchronized

(this) (this) { { … … }}}}……static void method() {static void method() { synchronizedsynchronized

(ThisClass.class) (ThisClass.class) { { … … }}}}

Page 14: 1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.

14

Reader/writer lock - specification• Shared lock objectShared lock object• In critical section can be:In critical section can be:

– Any number of reader threads ORAny number of reader threads OR– Only one writer threadOnly one writer thread

Page 15: 1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.

15

Reader/writer lock - stateClass ReaderWriterLock {Class ReaderWriterLock { int writers = 0;int writers = 0; int readers = 0;int readers = 0; … …}}

Could be boolean

Page 16: 1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.

16

Reader entry protocolpublic synchronized void public synchronized void readerEntryreaderEntry() {() {

while (writers>0) {while (writers>0) { try {try { wait();wait(); } } catch (InterruptedException ie) {}catch (InterruptedException ie) {}

}} readers++;readers++;}}

N.B. mutual exclusion required(synchronized!)

Update state

Condition

Page 17: 1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.

17

Writer entry protocolpublic synchronized void public synchronized void writerEntrywriterEntry() {() {

while (writers>0 || readers>0) {while (writers>0 || readers>0) { try {try { wait();wait(); } } catch (InterruptedException ie) {}catch (InterruptedException ie) {}

}} writers++;writers++;}}

Page 18: 1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.

18

Reader exit protocolpublic synchronized void public synchronized void readerExitreaderExit() {() {

readers--;readers--; if (readers==0)if (readers==0) notify();notify();}}

Wake one thread (if a reader was running no

Reader should have been waiting)

Update state

Could use notifyAll() would besafer because ____________________but_____________________________

Page 19: 1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.

19

Writer exit protocolpublic synchronized void public synchronized void writerExitwriterExit() {() {

writers--;writers--; notifyAll();notifyAll();}} Wake all threads

(there may be several readers waiting)

Update state

Page 20: 1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.

20

Reader/writer lock notes• What happens if calls to entry/exit are not What happens if calls to entry/exit are not

matched?matched?– Enter without exit?Enter without exit?

Deadlock, as lock never releasedDeadlock, as lock never released– Exit without enter?Exit without enter?

Error or incorrect admission of concurrent Error or incorrect admission of concurrent threadsthreads

Page 21: 1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.

21

Timed wait()• wait() wait() waits indefinitelywaits indefinitely

– Or until interruptedOr until interrupted• wait(long millis)wait(long millis) waits at most waits at most millismillis ms before attempted to regain lock ms before attempted to regain lock & continue& continue– But But wait(0)wait(0) waits indefinitely waits indefinitely– Cannot tell if timed out or notified!Cannot tell if timed out or notified!

• Allows time-outAllows time-out– E.g. error or deadlock detectionE.g. error or deadlock detection

Page 22: 1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.

22

Summary• Each object has a wait queue as well as a Each object has a wait queue as well as a

locklock– = “monitor”= “monitor”

• wait()wait() blocks a thread and releases the blocks a thread and releases the lock until lock until notify()notify()//notifyAll()notifyAll()

• Supports general concurrency controlSupports general concurrency control– Can be used to avoid busy waiting or pollingCan be used to avoid busy waiting or polling

• Additional variable(s) are required to track Additional variable(s) are required to track concurrency requirements concurrency requirements