SSC - Concurrency and Multi-threading High Level Concurrencyszh/teaching/ssc/... · many tasks, use...
Transcript of SSC - Concurrency and Multi-threading High Level Concurrencyszh/teaching/ssc/... · many tasks, use...
SSC - Communication and Networking
SSC - Concurrency and Multi-threadingHigh Level Concurrency
Shan He
School for Computational ScienceUniversity of Birmingham
Module 06-19321: SSC
SSC - Communication and Networking
Outline
Outline of Topics
Review what we learned
Reentrant Locks
Nonblocking Synchronization
SSC - Communication and Networking
Review what we learned
High level synchronisation in Java
High level concurrency tools
Fork/Join framework
Executors framework
SSC - Communication and Networking
Review what we learned
Low level synchronisation in Java
Low level concurrency tools
Atomic Operations
Volatile Variables
Locks
Reentrant Locks
Synchronized keyword
Synchronized methods
Synchronized statements
SSC - Communication and Networking
Reentrant Locks
Reentrant Locks: what is it?
I Q: What does reentrant mean?
I A: Reentrant simply means a thread can claim the lockmultiple times without blocking on itself
I For example: public class Reentrant{public synchronized outer(){ inner(); }public synchronized inner() { //do something }}
I Introduced in Java 1.5 for enhancing intrinsic lockingcapabilities
I Implemented Lock Interface , the simplest case of a lockwhich can be acquired and released
SSC - Communication and Networking
Reentrant Locks
ReentrantLock vs synchronized keywordI Similarity:
I Both use locks for synchronisationI Provides same visibility and orderings
I Advantages:I ReentrantLock get a lock with ability to interrupt: you can use
lockInterruptibly() to interrupt thread when it is waiting
for lock.I ReentrantLock put a timeout on waiting for lock: use
tryLock(long timeout, TimeUnit timeUnit) to wait
for lock with a maximum of timeI ReentrantLock support fairness: you can specify fairness
property when you create instance of ReentrantLock.I ReentrantLock provides methods to get List of all threads
waiting for lock: getQueuedThreads()
I Disadvantages: you are responsible for acquiring and releasinglock and you might forget to release lock
SSC - Communication and Networking
Reentrant Locks
How to use ReentrantLock?
Steps:
I Step 1: Instantiate a ReentrantLock objectprivate final ReentrantLock lock
= new ReentrantLock();
I Step 2: Define the critical section using the following code:lock.lock();
try
{ define your critical section
} finally {lock.unlock(); }
SSC - Communication and Networking
Nonblocking Synchronization
Disadvantages of Exclusive Locking
I Usages of locks:I enforcing exclusive access to an object’s state; andI establishing happens-before relationships, i.e., any changes
made to locked variables are visible to other threads thatsubsequently acquire the lock
I Disadvantages/Costs of using locksI In some situation, a thread will be suspended if it fails to
acquire lock but context switching can be expensiveI A thread cannot do anything when waiting for a lock. If thread
holding lock is delayed, no thread that needs that lock canprogress
I Danger of deadlock and other liveness problems
SSC - Communication and Networking
Nonblocking Synchronization
Volatile variables in Java
I Volatile variables: alternative lighter weight synchronizationmechanism
I Field modifier: Used to indicate that a variable’s value will bemodified by different threads
I Can be seen as a field version (for variables) of synchronizekeyword (for code block) but without blocking
I Volatile variables will never be cached thread-locally: all readsand writes will go straight to “main memory”;
I Essentially to ensure the updated volatile variables by a threadare visible to all other threads −→ Happens-beforerelationships
I Volatile variable never holds a lock: non-blocking
I Still have thread interference problem
SSC - Communication and Networking
Nonblocking Synchronization
Nonlocking Synchronization: what it is?
I Simply put: solving thread interference problem without usingexclusive locking
I Basic idea: instead of serializing access shared resource usinglocks, detect thread interference problem and retry if so
I Detailed steps:I Step 1: Proceed with the updateI Step 2: Collision Detection: check for interference from other
threadsI Step 3: If update fails, retry
I Optimistic synchronization method: you hope you cancomplete update without any interference. If you areinterfered by other people, you just forgive them and retry
I Exclusive locking is a pessimistic method, you proceed only ifyou are guaranteed that other threads will not interfere.
SSC - Communication and Networking
Nonblocking Synchronization
Compare-And-Swap (CAS) operationI Atomic read-modify-write operations: The key operation
behind Nonlocking Synchronization to achieve synchronizationI Atomic operation: an action either happens completely, or it
doesn’t happen at all.I Basic idea: CAS atomically updates a memory location to a
new value if the old value matches what we expected (stored),otherwise does nothing
I Takes three parameters, the memory address V , the expected(stored) old value A and the new value B
I Atomically updates V to the new value B, but only if thevalue in V matches the expected old value A; otherwise itdoes nothing
I Guarantees that the new value is calculated based onup-to-date information; if the value had been updated byanother thread in the meantime, the write would fail.
SSC - Communication and Networking
Nonblocking Synchronization
Compare-And-Swap (CAS) flowchart
Memory address V
Expected old value
A
New value B
value in V == A
Do nothingUpdate V with
value A
Yes No
Return value in V
SSC - Communication and Networking
Nonblocking Synchronization
Atomic Variable Classes in Java
I Part of java concurrency package java.util.concurrent
introduced in Java 1.5
I Used to build lighter-weight high performance non-blockingsynchronisation
I Based on Compare-And-Swap operation
I Atomic variables limit the to a single variable
I Generalization of volatile variablesI Twelve atomic variable classes, two popular groups:
I Scalars: AtomicInteger, AtomicLong, AtomicBoolean, andAtomicReference
I Arrays: elements can be updated atomically, available inInteger, Long, and Reference versions
I Other non-blocking data structures are available in Java, e.g.,non-blocking queue
SSC - Communication and Networking
Nonblocking Synchronization
Executors or ReentrantLock or synchronized keyword ornon-blocking synchronisation?
I If you are dealing with complex synchronisation, e.g., involvemany tasks, use Java high level concurrency tools, e.g.,Executors framework
I If you are dealing with simple synchronisation, e.g., involve acouple of tasks and threads, use synchronized keyword toavoid subtle bugs and keep code cleaner
I If you are dealing with simple synchronisation but you needmore control of the locks, e.g., to have better fairness amongthreads, use ReentrantLock
I If you are dealing with simple synchronisation andperformance is an critical issue, avoid using above but usenon-blocking synchronisation