Upload
others
View
0
Download
0
Embed Size (px)
Citation preview
Task managementState managementTask coordination
Future enhancementsFurther reading
Concurrency Support in Java
Daniel Solano Gomez
27 January 2011
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
OverviewTask management
Task primitivesExecutor framework
State managementAtomic variablesGeneral purpose concurrent collectionsSpecial-purpose collections
Task coordinationSynchronizersLocks
Future enhancementsFurther reading
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
Task primitivesExecutor framework
Where we are. . .Task management
Task primitivesExecutor framework
State managementAtomic variablesGeneral purpose concurrent collectionsSpecial-purpose collections
Task coordinationSynchronizersLocks
Future enhancementsFurther reading
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
Task primitivesExecutor framework
Runnable
public interface Runnable {void run ();
}
I Provides the primary abstraction for a taskI Allows definition of concurrent tasks without
subclassing Thread
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
Task primitivesExecutor framework
Callable
public interface Callable <V> {V call () throws Exception ;
}
A Callable is similar to Runnable, but it returns aresult and can throw checked exceptions.
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
Task primitivesExecutor framework
Future
I A Future represents the result of anasynchronous computation.
I Retrieve the result with the blocking get method.I It is possible to cancel the task.I Futures are normally created indirectly from
Runnable or Callable objects.I These are the same as the futures provided in
Clojure 1.1.
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
Task primitivesExecutor framework
Where we are. . .Task management
Task primitivesExecutor framework
State managementAtomic variablesGeneral purpose concurrent collectionsSpecial-purpose collections
Task coordinationSynchronizersLocks
Future enhancementsFurther reading
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
Task primitivesExecutor framework
Executors
public interface Executor {void execute ( Runnable command );
}
Executor is the preferred abstraction for taskexecution in Java.
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
Task primitivesExecutor framework
Creating executorsThe utility class Executors provides factorymethods to create preconfigured executors.I newCachedThreadPool() reuses available threads,
creating new threads as necessary
I newFixedThreadPool() uses a fixed number of threadsand an unbounded queue
I newSingleThreadedExecutor() is guaranteed to besingle threaded and uses an unbounded queue
I newScheduledThreadPool() is a fixed-size thread poolthat supports delayed and periodic tasks, similar to Timer
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
Task primitivesExecutor framework
ExecutorService
All executors provided by Java implementExecutorService, an extension of Executor that:
I Manages termination of the executorI Creates Future objects from submitted tasksI Provides methods for invoking collections of tasks
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
Task primitivesExecutor framework
More on executors
I Most executors created by Java are subclasses ofThreadPoolExecutor, which allows a largedegree of tuning.
I An executor can act like a blocking queue throughthe use of a ExecutorCompletionService.
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
Task primitivesExecutor framework
Executor demonstrations
It’s demo time!
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
Atomic variablesGeneral purpose concurrent collectionsSpecial-purpose collections
Where we are. . .Task management
Task primitivesExecutor framework
State managementAtomic variablesGeneral purpose concurrent collectionsSpecial-purpose collections
Task coordinationSynchronizersLocks
Future enhancementsFurther reading
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
Atomic variablesGeneral purpose concurrent collectionsSpecial-purpose collections
Features of atomic variablesI Operations have specific memory guarantees,
generally equivalent to volatile variablesI Allow use of compareAndSet to exploit
instructions available on modern processorsI Provide atomic equivalents to ++ and --
operatorsI Available for boolean, integer, long, and reference
typesI Available for integer, long, and reference arrays
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
Atomic variablesGeneral purpose concurrent collectionsSpecial-purpose collections
Using atomic variables
I Useful for implementing non-blocking datastructures
I Only for state confined to a single variableI Not a general replacement for locking, best for
little or no contention
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
Atomic variablesGeneral purpose concurrent collectionsSpecial-purpose collections
Xorshift PRNG performanceDual core
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
Atomic variablesGeneral purpose concurrent collectionsSpecial-purpose collections
Xorshift PRNG performanceQuad core
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
Atomic variablesGeneral purpose concurrent collectionsSpecial-purpose collections
Where we are. . .Task management
Task primitivesExecutor framework
State managementAtomic variablesGeneral purpose concurrent collectionsSpecial-purpose collections
Task coordinationSynchronizersLocks
Future enhancementsFurther reading
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
Atomic variablesGeneral purpose concurrent collectionsSpecial-purpose collections
Java collections and thread safetyI Original Java Hashtable and Vector are
synchronizedI Collections introduced in Java 1.2 are generally
not thread-safeI Collections.synchronizedXxx methods can
make collections thread-safeI Compound operations must be externally
synchronizedI Beware of ConcurrentModificationException
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
Atomic variablesGeneral purpose concurrent collectionsSpecial-purpose collections
Concurrent collectionsI Java SE 5 introduced new thread-safe collectionsI Offer specific memory model guaranteesI Performance and consistency guarantees may
differ from unsafe and synchronized collectionsI Iterators are weakly-consistent and do not throw
ConcurrentModificationExceptionI Bulk operations are not atomicI Do not support null elementsI size and isEmpty may be approximations
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
Atomic variablesGeneral purpose concurrent collectionsSpecial-purpose collections
ConcurrentHashMap
I Implements ConcurrentMap, an extension of Mapwith compare-and-set-like functions
I Drop-in replacement for Hashtable forthread-safety
I Reads require no lockingI Adjustable write concurrencyI Impossible to lock entire table
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
Atomic variablesGeneral purpose concurrent collectionsSpecial-purpose collections
ConcurrentSkipListMap
I Implements ConcurrentNavigableMap, acombination of ConcurrentMap andNavigableMap
I Returned Map.Entry objects are snapshots anddo not support setValue
I size is not constant-time
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
Atomic variablesGeneral purpose concurrent collectionsSpecial-purpose collections
ConcurrentSkipListSet
I Implements NavigableSetI size method is not constant-time
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
Atomic variablesGeneral purpose concurrent collectionsSpecial-purpose collections
ConcurrentLinkedQueue
I Implements unbounded Queue based on linkednodes
I size method is not constant-time
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
Atomic variablesGeneral purpose concurrent collectionsSpecial-purpose collections
Where we are. . .Task management
Task primitivesExecutor framework
State managementAtomic variablesGeneral purpose concurrent collectionsSpecial-purpose collections
Task coordinationSynchronizersLocks
Future enhancementsFurther reading
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
Atomic variablesGeneral purpose concurrent collectionsSpecial-purpose collections
COW Collections
I CopyOnWriteArrayList andCopyOnWriteArraySet
I Similar behaviour to Clojure’s persistent datastructures
I Reads are always consistentI However, writes are expensive and require full
copies
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
Atomic variablesGeneral purpose concurrent collectionsSpecial-purpose collections
Blocking queues
I Facilitate consumer-produce patternI Producers may block if queue is fullI Consumers may block if queue is emptyI Useful in combination with thread pools
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
Atomic variablesGeneral purpose concurrent collectionsSpecial-purpose collections
Basic blocking queuesLinkedBlockingQueue and ArrayBlockingQueue
I FIFO queues analogous to ArrayList andLinkedList
I Thread-safe and better performance than asynchronized List
I ArrayBlockingQueue is bounded and cansupport fairness
I LinkedBlockingQueue is optionally bounded
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
Atomic variablesGeneral purpose concurrent collectionsSpecial-purpose collections
More blocking queues
I LinkedBlockingDeque, a thread-safe Dequebacked by a linked list
I PriorityBlockingQueue, a thread-safecounterpart to PriorityQueue
I DelayQueue, a blocking queue of Delayedelements, like a priority queue sorted by expiration
I SynchronousQueue, a zero-capacity queue usedto coordinate handoffs, with optional fairness
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
SynchronizersLocks
Where we are. . .Task management
Task primitivesExecutor framework
State managementAtomic variablesGeneral purpose concurrent collectionsSpecial-purpose collections
Task coordinationSynchronizersLocks
Future enhancementsFurther reading
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
SynchronizersLocks
CountDownLatch
I General purpose latch is initialised with a countI countDown method decrements countI await method will block until count is zeroI Not reusableI Good for waiting for one-time events or waiting
for multiple parties to be ready to proceed
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
SynchronizersLocks
Semaphore
I A counting semaphore with a number of virtualpermits
I Binary semaphore is a mutexI Permits are not tied to objects or threadsI Ideal for implementing resource pools
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
SynchronizersLocks
CyclicBarrier
I Coordinates a fixed number of threadsI All threads come together at the barrier at the
same timeI ReusableI Good for simulations, ensuring that one step of
the simulation is complete before proceeding tonext step
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
SynchronizersLocks
Exchanger
I Like a two-party barrier, but includes swappingobjects
I Example use is swapping of buffers between aproducer and consumer
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
SynchronizersLocks
Where we are. . .Task management
Task primitivesExecutor framework
State managementAtomic variablesGeneral purpose concurrent collectionsSpecial-purpose collections
Task coordinationSynchronizersLocks
Future enhancementsFurther reading
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
SynchronizersLocks
The Lock interface
I Allows alternatives to intrinsic lockingI Implementations must guarantee same
memory-visibility semanticsI Offers choice of unconditional, polled, timed, and
interruptible lock acquisitionI Allows non-block-structured blocking, requires
explicit lock and unlockI Allows multiple wait conditions
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
SynchronizersLocks
Intrinsic locking example
Object lock = new Object ();...synchronized (lock) {
// do something}
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
SynchronizersLocks
Lock object example
Object lock = new ReentrantLock ();...lock.lock ();try {
// do something} finally {
lock.unlock ();}
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
SynchronizersLocks
ReentrantLock
I ReentrantI Offers same memory semantics as a synchronized
blockI Allows fair lock acquisition
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
SynchronizersLocks
Read-write locksReadWriteLock interfaceI Allows either multiple readers or a single writerI Separate read and write locksI Allows implementation flexibilityReentrantReadWriteLock implementationI Reentrant for both locksI Allows fairnessI Allows downgrades from writer to readerI Best when locks are held for long times
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
Where we are. . .Task management
Task primitivesExecutor framework
State managementAtomic variablesGeneral purpose concurrent collectionsSpecial-purpose collections
Task coordinationSynchronizersLocks
Future enhancementsFurther reading
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
Expected in JDK 7Subject to change
I Fork/Join framework for recursive breakdown oftasks
I TransferQueue allows producer to wait forconsumers
I Phaser, a flexible combination ofCyclicBarrier with CountDownLatch
I ThreadLocalRandom
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
Where we are. . .Task management
Task primitivesExecutor framework
State managementAtomic variablesGeneral purpose concurrent collectionsSpecial-purpose collections
Task coordinationSynchronizersLocks
Future enhancementsFurther reading
Daniel Solano Gomez Concurrency Support in Java
Task managementState managementTask coordination
Future enhancementsFurther reading
Further reading
I Java Concurrency in Practice, Brian Goetz et al.I Java SE 6.0 SDK DocumentationI The JavaTM Tutorials: Concurrency
Daniel Solano Gomez Concurrency Support in Java