15-multithreading2 - Overview CMSC 330: Organization of...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CMSC 330: Organization of Programming Languages Java 1.5, Ruby, and MapReduce CMSC 330 2 Overview Java 1.5 • ReentrantLock class • Condition interface - await( ), signalAll( ) Ruby multithreading Concurrent programming • MapReduce CMSC 330 3 Lock Interface (Java 1.5) Explicit Lock objects • Same as implicit lock used by synchronized keyword Only one thread can hold a lock at once • lock( ) causes thread to block (become suspended) until lock can be acquired • unlock( ) allows lock to be acquired by different thread interface Lock { void lock(); void unlock(); ... /* Some more stuff, also */ } class ReentrantLock implements Lock { . .. } CMSC 330 4 Synchronization Example (Java 1.5) public class Example extends Thread { private static int cnt = 0; static Lock lock = new ReentrantLock(); public void run() { lock.lock(); int y = cnt; cnt = y + 1; lock.unlock(); } } } Lock , for protecting the shared state Acquires the lock; only succeeds if not held by another thread, otherwise blocks Releases the lock CMSC 330 5 ReentrantLock Class (Java 1.5) Reentrant lock • Can be reacquired by same thread by invoking lock( ) Up to 2147483648 times • To release lock, must invoke unlock( ) The same number of times lock( ) was invoked Reentrancy is useful • Each method can acquire/release locks as necessary No need to worry about whether callers already have locks • Discourages complicated coding practices To determine whether lock has already been acquired class ReentrantLock implements Lock { . .. } CMSC 330 6 Reentrant Lock Example Example • returnAndInc( ) can acquire lock and invoke inc( ) • inc( ) can acquire lock without having to worry about whether thread already has lock static int count = 0; static Lock l = new ReentrantLock(); void inc() { l.lock(); count ++; l.unlock(); } void returnAndInc() { int temp; l.lock(); temp = count ; inc(); l.unlock(); }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 CMSC 330 7 Condition Interface (Java 1.5) Explicit condition variable objects • Condition variable C is created from a Lock object L by calling L .newCondition( ) • Condition variable C is then associated with L Multiple condition objects per lock • Allows different wait sets to be created for lock • Can wake up different threads depending on condition interface Lock { Condition newCondition(); . .. } interface Condition { void await(); void signalAll(); . .. } CMSC 330 8 Condition – await() and signallAll() Calling await( ) w/ lock held • Releases the lock But not any other locks held by this thread • Adds this thread to wait set for condition • Blocks the thread Calling signallAll( ) w/ lock held • Resumes all threads in condition’s wait set • Threads must reacquire lock Before continuing (returning from await) Enforced automatically; you don' t have to do it L C C C T T T ... ... wait set
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/13/2012 for the course CMSC 330 taught by Professor Staff during the Fall '08 term at Maryland.

Page1 / 6

15-multithreading2 - Overview CMSC 330: Organization of...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online