Download presentation
Presentation is loading. Please wait.
Published byElaine Briggs Modified over 9 years ago
1
1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science
2
2 Contents Wait/notify overviewWait/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
3
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
4
4 wait() A thread calling wait is added to the wait queueA thread calling wait is added to the wait queue –(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 avoids race conditions or lock-out with sleep)
5
5 Wait detail … synchronized (this) { try { this.wait();… synchronized (this) { try { this.wait(); } catch (InterruptedException ie) { } } } catch (InterruptedException ie) { } } Gains lock on ‘this’ Releases lock on ‘this’ Thread has lock has lock Added to wait queue (time passes) Woken from wait queue (time passes) Re-gains lock on ‘this’
6
6 notify() A thread calling notify() wakes one thread(s) from the wait queueA thread calling notify() wakes one thread(s) from the wait queue A thread calling notifyAll() wakes all thread(s) from the wait queueA thread calling notifyAll() wakes all thread(s) from the wait queue If there are no threads waiting then the calls have no effectIf there are no threads waiting then the calls have no effect –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 method –=> less efficient than some alternative
7
7 Infinite buffer Class Buffer { Vector data = new Vector (); Vector data = new Vector (); synchronized void put(T item) { synchronized void put(T item) { data.add(item); data.add(item); notify(); notify(); } … … public synchronized T get() { while (data.size()==0) { try { wait(); } catch(InterruptedException ie) {} } T item = data.get(0); data.remove(0); return item; } has lock has lock wakes
8
8 Infinite buffer - cases get(), data in bufferget(), data in buffer –Removes and returns first item get(), no data in bufferget(), no data in buffer –Waits until data present put(), no thread waitingput(), no thread waiting –Add data to buffer (notify is no-op) put(), thread(s) waitingput(), thread(s) waiting –Add data to buffer, wake one thread
9
9 Infinite buffer - notes notify() may wake any waiting threadnotify() may wake any waiting thread –But only threads calling get ever wait Put only adds one itemPut only adds one item –only one waiting thread can now take this item, so only needs to be woken up wait() may terminate due to interruptwait() may terminate due to interrupt A waiting thread may be beaten to the lock by a new get() threadA waiting thread may be beaten to the lock by a new get() thread –E.g. if it was already waiting to obtain the lock –So it has to be ready to wait more than once!
10
10 General concurrency control Thread 1Thread 1 –…–…–…–… –Entry protocol –Critical section –Exit protocol –…–…–…–… Entry:Entry: –Block until safe to proceed (update state) Thread 2 –…–… –Entry protocol –Critical section –Exit protocol –…–… Exit: –(update state) Wake any blocked threads now able to proceed
11
11 Infinite buffer revisited Class Buffer { Vector data = new Vector (); Vector data = new Vector (); synchronized void put(T item) { synchronized void put(T item) { data.add(item); data.add(item); notify(); notify(); } … … public synchronized T get() { while (data.size==0) { try { wait(); } catch(InterruptedException ie) {} } T item = data.get(0); data.remove(0); return item; } entry exit Critical section
12
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 section If more than one thread should be in critical section then lock must be released and regainedIf more than one thread should be in critical section then lock must be released and regained –E.g. separate into two synchronized blocks
13
13 Synchronized method implementation This:… synchronized void method() { …}… static synchronized void method() { …} Equals: … void method() { synchronized (this) { … } … static void method() { synchronized (ThisClass.class) { … }
14
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 OR –Only one writer thread
15
15 Reader/writer lock - state Class ReaderWriterLock { int writers = 0; int writers = 0; int readers = 0; int readers = 0; …} Could be boolean
16
16 Reader entry protocol public synchronized void readerEntry() { 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
17
17 Writer entry protocol public synchronized void writerEntry() { while (writers>0 || readers>0) { while (writers>0 || readers>0) { try { try { wait(); wait(); } catch (InterruptedException ie) {} } catch (InterruptedException ie) {} } writers++; writers++;}
18
18 Reader exit protocol public synchronized void readerExit() { 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 be safer because ____________________ but_____________________________
19
19 Writer exit protocol public synchronized void writerExit() { writers--; writers--; notifyAll(); notifyAll();} Wake all threads (there may be several readers waiting) Update state
20
20 Reader/writer lock notes What happens if calls to entry/exit are not matched?What happens if calls to entry/exit are not matched? –Enter without exit? Deadlock, as lock never released –Exit without enter? Error or incorrect admission of concurrent threads
21
21 Timed wait() wait() waits indefinitelywait() waits indefinitely –Or until interrupted wait(long millis) waits at most millis ms before attempted to regain lock & continuewait(long millis) waits at most millis ms before attempted to regain lock & continue –But wait(0) waits indefinitely –Cannot tell if timed out or notified! Allows time-outAllows time-out –E.g. error or deadlock detection
22
22 Summary Each object has a wait queue as well as a lockEach object has a wait queue as well as a lock –= “monitor” wait() blocks a thread and releases the lock until notify() / notifyAll()wait() blocks a thread and releases the lock until notify() / notifyAll() Supports general concurrency controlSupports general concurrency control –Can be used to avoid busy waiting or polling Additional variable(s) are required to track concurrency requirementsAdditional variable(s) are required to track concurrency requirements
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.