










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This document from the university of maryland, college park covers advanced concurrency topics including the use of wait and notifyall in java, java concurrency utilities such as blocking buffers, exchanger, countdownlatch, semaphores, and executors, and the importance of using the swing thread for gui components. The document also includes notes on potential pitfalls and best practices for using wait and notifyall.
Typology: Study notes
1 / 18
This page cannot be seen from the preview
Don't miss anything!











Notes on wait and notifyAll
get an IllegalMonitorStateException otherwise don’t catch it; this exception indicates coding error
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
see next slide
wait idiom
Java Concurrency Utilities
don’t write your own: someone else has alreadydone it
thread pools
Exchanger
V exchange(V v) - pairs up with another thread thatwants to exchange a V
Semaphore
acquire() - acquires one permit from the semaphore.If none are available, blocks until one can bereturned release() - adds a permit to the semaphore
Executor
void execute(Runnable work)
ThreadPoolExecutor
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
Doing work
such as saving a file or spell checking a document
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();