DOC PREVIEW
UMD CMSC 132 - Advanced Concurrency Topics

This preview shows page 1-2-3-4-5-6 out of 19 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Advanced Concurrency TopicsUsing Wait and NotifyAllNotes on wait and notifyAllwait idiomWhat can do wrong?Sample ProblemJava Concurrency UtilitiesExchanger<V>CountDownLatchSemaphoreExecutorMore ExecutorsSwing and ThreadsWHAT NOT TO DO IN SWINGSlide 15Why?Doing workBetterRecommendedAdvanced Concurrency TopicsNelson Padua-PerezBill PughDepartment of Computer ScienceUniversity of Maryland, College ParkUsing Wait and NotifyAllpublic 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(); }}Notes on wait and notifyAllNeed to hold a lock on object on which you are going to wait/notifyAllget an IllegalMonitorException otherwisedon’t catch it; this exception indicates coding errorwait gives up the lock on the object waited onno matter how many times you locked itbut no other locks held by that thread are given upbe scared of holding locks on multiple objects and using wait: easy to get deadlockalways call wait in a loopsee next slidewait idiomsynchronized(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 lockWhat 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(); }}Sample ProblemNot 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 arrivaluse Bakery algorithm to determine order in which consumers get items from QueueJava Concurrency UtilitiesLot of concurrency utilities were added in Java 1.5Blocking buffersdon’t write your own: someone else has already done itExchangerCountDownLatchSemaphoresExecutorsthread poolsExchanger<V>Allows threads to pair up to exchange objectmethods:V exchange(V v) - pairs up with another thread that wants to exchange a VCountDownLatchStarts with some non-negative valuehas several methods:getCount() - returns current valuecountDown() - decrements valueawait() - waits until count reaches zeroSemaphoreSemaphore starts with some number of permitsOperations:acquire() - acquires one permit from the semaphore. If none are available, blocks until one can be returnedrelease() - adds a permit to the semaphoreExecutorVery generic interfaceOne method:void execute(Runnable work)Several implementations:ThreadPoolExecutorEasy to define your own:class DirectExecutor implements Executor { public void execute(Runnable r) { r.run(); }}More Executors class ThreadPerTaskExecutor implements Executor { public void execute(Runnable r) { new Thread(r).start(); } }Swing and ThreadsWHAT 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 ThreadsThere is a Swing ThreadAll callbacks in response to events occur in the Swing threadAny modification, adjustment or reading of Swing components must be done within the Swing threadNo operations that take longer than a few milliseconds should be done in the Swing threadWhy?Avoiding synchronization improves performanceavoids the possibility that the Swing thread will be blocked by some other threadDoing workIf, in response to a GUI event, you want to perform a task that might take a whilesuch as saving a file or spell checking a documentcan’t do it in the event threadAsk another thread to do itBetterfinal 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();RecommendedAction updateCursorAction = new AbstractAction() { boolean shouldDraw = false; public void actionPerformed(ActionEvent e) { if (shouldDraw) drawCursor(); else eraseCursor(); shownDraw = !shouldDraw; }};new Timer(300,


View Full Document

UMD CMSC 132 - Advanced Concurrency Topics

Documents in this Course
Notes

Notes

8 pages

Recursion

Recursion

12 pages

Sorting

Sorting

31 pages

HTML

HTML

7 pages

Trees

Trees

19 pages

HTML

HTML

18 pages

Trees

Trees

19 pages

Honors

Honors

19 pages

Lecture 1

Lecture 1

11 pages

Quiz #3

Quiz #3

2 pages

Hashing

Hashing

21 pages

Load more
Download Advanced Concurrency Topics
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Advanced Concurrency Topics and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Advanced Concurrency Topics 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?