Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University...
-
Upload
brianna-singleton -
Category
Documents
-
view
213 -
download
0
Transcript of Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University...
![Page 1: Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader036.fdocuments.in/reader036/viewer/2022082816/56649f575503460f94c7b356/html5/thumbnails/1.jpg)
Advanced Concurrency Topics
Nelson Padua-Perez
Bill Pugh
Department of Computer Science
University of Maryland, College Park
![Page 2: Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader036.fdocuments.in/reader036/viewer/2022082816/56649f575503460f94c7b356/html5/thumbnails/2.jpg)
Using Wait and NotifyAll
public class Buffer { private LinkedList objects = new LinkedList(); public synchronized add( Object x ) { objects.add(x);
this.notifyAll(); } public synchronized Object remove() { while (objects.isEmpty()) {
try { this.wait(); } catch (InterruptedException e) {}
} return objects.removeFirst(); }}
![Page 3: Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader036.fdocuments.in/reader036/viewer/2022082816/56649f575503460f94c7b356/html5/thumbnails/3.jpg)
Notes on wait and notifyAll
Need to hold a lock on object on which you are going to wait/notifyAll
get an IllegalMonitorException otherwise
don’t catch it; this exception indicates coding error
wait gives up the lock on the object waited onno 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 objects and using wait: easy to get deadlock
always call wait in a loopsee next slide
![Page 4: Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader036.fdocuments.in/reader036/viewer/2022082816/56649f575503460f94c7b356/html5/thumbnails/4.jpg)
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
![Page 5: Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader036.fdocuments.in/reader036/viewer/2022082816/56649f575503460f94c7b356/html5/thumbnails/5.jpg)
What can do wrong?
public class Buffer { private LinkedList objects = new LinkedList(); public synchronized add( Object x ) { objects.add(x);
this.notifyAll(); } public synchronized Object remove() { if (objects.isEmpty()) {
try { this.wait(); } catch (InterruptedException e) {}
} return objects.removeFirst(); }}
![Page 6: Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader036.fdocuments.in/reader036/viewer/2022082816/56649f575503460f94c7b356/html5/thumbnails/6.jpg)
Sample Problem
Not easy (typical senior level quiz/midterm problem)
Bakery queue:
People show up in a bakery, takes the next available number, and receives a value in the order of their arrival
use Bakery algorithm to determine order in which consumers get items from Queue
![Page 7: Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader036.fdocuments.in/reader036/viewer/2022082816/56649f575503460f94c7b356/html5/thumbnails/7.jpg)
Java Concurrency Utilities
Lot of concurrency utilities were added in Java 1.5
Blocking buffersdon’t write your own: someone else has already done it
Exchanger
CountDownLatch
Semaphores
Executorsthread pools
![Page 8: Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader036.fdocuments.in/reader036/viewer/2022082816/56649f575503460f94c7b356/html5/thumbnails/8.jpg)
Exchanger<V>
Allows threads to pair up to exchange object
methods:V exchange(V v) - pairs up with another thread that wants to exchange a V
![Page 9: Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader036.fdocuments.in/reader036/viewer/2022082816/56649f575503460f94c7b356/html5/thumbnails/9.jpg)
CountDownLatch
Starts with some non-negative value
has several methods:getCount() - returns current value
countDown() - decrements value
await() - waits until count reaches zero
![Page 10: Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader036.fdocuments.in/reader036/viewer/2022082816/56649f575503460f94c7b356/html5/thumbnails/10.jpg)
Semaphore
Semaphore starts with some number of permits
Operations:acquire() - acquires one permit from the semaphore. If none are available, blocks until one can be returned
release() - adds a permit to the semaphore
![Page 11: Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader036.fdocuments.in/reader036/viewer/2022082816/56649f575503460f94c7b356/html5/thumbnails/11.jpg)
Executor
Very generic interface
One method:void execute(Runnable work)
Several implementations:ThreadPoolExecutor
Easy to define your own:class DirectExecutor implements Executor { public void execute(Runnable r) {
r.run(); }}
![Page 12: Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader036.fdocuments.in/reader036/viewer/2022082816/56649f575503460f94c7b356/html5/thumbnails/12.jpg)
More Executors
class ThreadPerTaskExecutor implements Executor { public void execute(Runnable r) { new Thread(r).start(); } }
![Page 13: Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader036.fdocuments.in/reader036/viewer/2022082816/56649f575503460f94c7b356/html5/thumbnails/13.jpg)
Swing and Threads
![Page 14: Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader036.fdocuments.in/reader036/viewer/2022082816/56649f575503460f94c7b356/html5/thumbnails/14.jpg)
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
}
}
![Page 15: Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader036.fdocuments.in/reader036/viewer/2022082816/56649f575503460f94c7b356/html5/thumbnails/15.jpg)
Swing and Threads
There is a Swing Thread
All callbacks in response to events occur in the Swing thread
Any modification, adjustment or reading of Swing components must be done within the Swing thread
No operations that take longer than a few milliseconds should be done in the Swing thread
![Page 16: Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader036.fdocuments.in/reader036/viewer/2022082816/56649f575503460f94c7b356/html5/thumbnails/16.jpg)
Why?
Avoiding synchronization improves performance
avoids the possibility that the Swing thread will be blocked by some other thread
![Page 17: Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader036.fdocuments.in/reader036/viewer/2022082816/56649f575503460f94c7b356/html5/thumbnails/17.jpg)
Doing work
If, in response to a GUI event, you want to perform 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
![Page 18: Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader036.fdocuments.in/reader036/viewer/2022082816/56649f575503460f94c7b356/html5/thumbnails/18.jpg)
Betterfinal 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();
![Page 19: Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.](https://reader036.fdocuments.in/reader036/viewer/2022082816/56649f575503460f94c7b356/html5/thumbnails/19.jpg)
Recommended
Action updateCursorAction = new AbstractAction() {
boolean shouldDraw = false;
public void actionPerformed(ActionEvent e) {
if (shouldDraw) drawCursor(); else eraseCursor(); shownDraw = !shouldDraw;
}
};
new Timer(300, updateCursorAction).start();