Docsity

Advanced Concurrency: Wait/NotifyAll, Java Concurrency Utilities, Swing & Threads - Prof. , Study notes of Computer Science

Partial preview of the text

Download Advanced Concurrency: Wait/NotifyAll, Java Concurrency Utilities, Swing & Threads - Prof. and more Study notes Computer Science in PDF only on Docsity!

Advanced Concurrency Topics

CMSC 132

Department of Computer Science

University of Maryland, College Park

Overview

Using Wait and NotifyAll Wait idiom Java Concurrency Utilities Swing and Threads

Notes on wait and notifyAll

Need to hold a lock on object on which you aregoing to wait/notifyAll

get an IllegalMonitorStateException otherwise don’t catch it; this exception indicates coding error

wait gives up the lock on the object waited on

no matter how many times you locked it but no other locks held by that thread are given up

be scared of holding locks on multiple objectsand using wait: easy to get deadlock

always call wait in a loop

see next slide

wait idiom

synchronized(lock) {

while (!ready) {try { lock.wait() } catch (IE e) { }} // ready must have been true// and we held the lock when ready was true// and haven’t given it upTAKE ACTION;}; // release lock

Java Concurrency Utilities

Lot of concurrency utilities were added in Java1.5 Blocking buffers

don’t write your own: someone else has alreadydone it

Exchanger CountDownLatch Semaphores Executors

thread pools

Exchanger

Allows threads to pair up to exchange object methods:

V exchange(V v) - pairs up with another thread thatwants to exchange a V

Semaphore

Semaphore starts with some number of permits Operations:

acquire() - acquires one permit from the semaphore.If none are available, blocks until one can bereturned release() - adds a permit to the semaphore

Executor

Very generic interface One method:

void execute(Runnable work)

Several implementations:

ThreadPoolExecutor

Easy to define your own:^ class DirectExecutorimplements Executor {

publ

ic

void execute(Runnable r

r.

run(

What not to do in Swing

//DON'T DO THIS!while (isCursorBlinking()) {

drawCursor();for (int i = 0; i < 300000; i++) {

Math.sqrt((double)i); // this should really chew up

// some time

} eraseCursor();for (int i = 0; i < 300000; i++) {

Math.sqrt((double)i); // likewise } }

Swing and Threads

There is a Swing Thread All callbacks in response to events occur in theSwing thread Any modification, adjustment or reading ofSwing components must be done within theSwing thread No operations that take longer than a fewmilliseconds should be done in the Swingthread

Doing work

If, in response to a GUI event, you want toperform a task that might take a while

such as saving a file or spell checking a document

Can’t do it in the event thread Ask another thread to do it

Better

final Runnable doUpdateCursor = new Runnable() {

boolean shouldDraw = false;public void run() {

if (shouldDraw) drawCursor(); else eraseCursor();shownDraw = !shouldDraw; } }; Runnable doBlinkCursor = new Runnable() {

public void run() {

while (isCursorBlinking()) {

EventQueue.invokeLater(doUpdateCursor);Thread.sleep(300); }}};

new Thread(doBlinkCursor).start();