DOC PREVIEW
Yale CPSC 433 - Java™ Concurrency Utilities in Practice

This preview shows page 1-2-3-24-25-26-27-49-50-51 out of 51 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 51 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 51 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 51 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 51 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 51 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 51 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 51 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 51 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 51 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 51 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 51 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

3Java™ Concurrency Utilitiesin PracticeBrian GoetzSun Microsystem [email protected] authors: Doug Lea State University of New York, Oswego [email protected] [email protected] BowbeerJava ME SpecialistMobile App [email protected] HolmesSenior Java TechnologistSun Microsystems [email protected]: OOPSLA 2007 tutorialby Joe Bowbeer and David Holmeshttp://www.oopsla.org/oopsla2007/index.php ?page=sub/&id=694About these slides• Java™ is a trademark of Sun Microsystems, Inc.• Material presented is based on latest informationavailable for JavaTM Platform Standard Edition, asimplemented in JDK™ 6.0• Code fragments elide—Exception handling for simplicity—Access modifiers unless relevant• More extensive coverage of most topics can be found inthe book– Java Concurrency in Practice, by Brian Goetz et al,Addison-Wesley (JCiP)• See also– Concurrent Programming in Java, by Doug Lea,Addison-Wesley (CPJ)5Review: Java Threading Model• The Java virtual machine (JVM)—Creates the initial thread which executes the main methodof the class passed to the JVM—Creates internal JVM helper threads Garbage collection, finalization, signal dispatching …• The code executed by the ‘main’ thread can create otherthreads—Either explicitly; or—Implicitly via libraries: AWT/Swing, Applets Servlets, web services RMI image loading …6Review: Java Thread Creation• Concurrency is introduced through objects of the classThread— Provides a ‘handle’ to an underlying thread of control• There is always a ‘current’ thread running: a— Static method Thread.currentThread()• The start() method— Creates a new thread of control to execute the Thread object’srun() method• Two ways to provide a run() method:— Subclass Thread and override run()— Define a class that implements the Runnable interface and getthe Thread object to run it new Thread(aRunnable).start();• Runnable defines the abstraction of work• Thread defines the abstraction of a worker7Review: Thread Interaction• void start()— Creates a new thread of control to execute the run() method of the Threadobject— Can only be invoked once per Thread object• void join()— Waits for a thread to terminate— t1.join(); // blocks current thread until t1 terminates• static void sleep(long ms)throws InterruptedException— Blocks current thread for approximately at least the specified time• static void yield()— Allows the scheduler to select another thread to run8Review: Java Synchronization• Every Java object has an associated lock acquired via:— synchronized statements– synchronized( foo ){ // execute code while holding foo’s lock}— synchronized methods– public synchronized void op1(){ // execute op1 while holding ‘this’ lock}• Only one thread can hold a lock at a time— If the lock is unavailable the thread is blocked— Locks are granted per-thread: reentrant or recursive locks• Locking and unlocking are automatic— Can’t forget to release a lock— Locks are released when a block goes out of scope By normal means or when an exception is thrown9Review: Use of wait/notify• Waiting for a condition to hold: synchronized (obj) { // obj protects the mutablestate while (!condition) { try { obj.wait(); } catch (InterruptedException ex) { ... } } // make use of condition while obj still locked }• Changing a condition: synchronized (obj) { // obj protects the mutablestate condition = true; obj.notifyAll(); // or obj.notify() }• Golden rule: Always test a condition in a loop— Change of state may not be what you need— Condition may have changed again No built-in protection from ‘barging’— Spurious wakeups are permitted – and can occur10java.util.concurrent• General purpose toolkit for developing concurrentapplications—No more “reinventing the wheel”!• Goals: “Something for Everyone!”—Make some problems trivial to solve by everyone Develop thread-safe classes, such as servlets, built onconcurrent building blocks like ConcurrentHashMap—Make some problems easier to solve by concurrent programmers Develop concurrent applications using thread pools, barriers,latches, and blocking queues—Make some problems possible to solve by concurrency experts Develop custom locking classes, lock-free algorithms11Overview of j.u.c• Executors— Executor— ExecutorService— ScheduledExecutorService— Callable— Future— ScheduledFuture— Delayed— CompletionService— ThreadPoolExecutor— ScheduledThreadPoolExecutor— AbstractExecutorService— Executors— FutureTask— ExecutorCompletionService• Queues— BlockingQueue— ConcurrentLinkedQueue— LinkedBlockingQueue— ArrayBlockingQueue— SynchronousQueue— PriorityBlockingQueue— DelayQueue• Concurrent Collections— ConcurrentMap— ConcurrentHashMap— CopyOnWriteArray{List,Set}• Synchronizers— CountDownLatch— Semaphore— Exchanger— CyclicBarrier• Locks: java.util.concurrent.locks— Lock— Condition— ReadWriteLock— AbstractQueuedSynchronizer— LockSupport— ReentrantLock— ReentrantReadWriteLock• Atomics: java.util.concurrent.atomic— Atomic[Type]— Atomic[Type]Array— Atomic[Type]FieldUpdater— Atomic{Markable,Stampable}Reference12Key Functional Groups in j.u.c.• Executors, Thread pools and Futures—Execution frameworks for asynchronous tasking• Concurrent Collections:—Queues, blocking queues, concurrent hash map, …—Data structures designed for concurrent environments• Locks and Conditions—More flexible synchronization control—Read/write locks• Synchronizers: Semaphore, Latch, Barrier, Exchanger—Ready made tools for thread coordination• Atomic variables—The key to writing lock-free algorithms13The Executor Framework• Framework for asynchronous task execution• Standardize asynchronous invocation—Framework to execute Runnable and Callable tasks Runnable: void run() Callable<V>: V call() throws Exception• Separate submission from execution policy—Use anExecutor.execute(aRunnable)—Not new Thread(aRunnable).start()• Cancellation and shutdown support• Usually created via Executors factory class—Configures flexible ThreadPoolExecutor—Customize shutdown methods, before/after hooks, saturationpolicies, queuing14Creating Executors• Sample ExecutorService implementations from Executors—


View Full Document
Download Java™ Concurrency Utilities in Practice
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 Java™ Concurrency Utilities in Practice 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 Java™ Concurrency Utilities in Practice 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?