Date post: | 15-Dec-2014 |
Category: |
Technology |
Upload: | kyung-koo-yoon |
View: | 660 times |
Download: | 4 times |
Practical Concurrency
January, 2009
http://javadom.blogspot.com/2011/06/lecture-on-java-concurrency-day-1.html
Contents
Concurrency Basics Designing Concurrency Concurrent Utils
Day 1 (9am, Jan 21, 2009)
Hardware BasicsThread BasicsThread Revisited
1-1 Hardware Basics
ProcessorInterconnectMemoryCache
Hardware
1-2 Thread Basics
Race ConditionLock and Conditioninterrupt
Race condition Competing condition against some shared
resources Example
T1 : var += 5 T2 : var *= 5 Initially var was 0, then what’s the result value?
OS scheduling Critical Section
a piece of code that accesses a shared resource that must not be concurrently accessed by more than one thread of execution
Lock and Condition Monitor object
Synchronized block Wait and notify on monitor object Spurious wakeup
Lock and Condition java.util.concurrent.locks package Lock, Condition, ReadWriteLock
interfaces ReentrantLock, ReentrantReadWriteLock
classes
interrupt
static boolean Thread.interrupted Tells current interrupt flag and reset
Boolean Thread.isInterrupted Just tell current interrupt flag
InterruptedException Object.wait Thread.sleep
1-3 Thread Revisited
Spurious wakeupNotify or notifyAllVolatile and visibility
Spurious Wakeup http://java.sun.com/j2se/1.5.0/docs/api/
java/lang/Object.html#wait() A thread can also wake up without being
notified, interrupted, or timing out, a so-called spurious wakeup. While this will rarely occur in practice, applications must guard against it by testing for the condition that should have caused the thread to be awakened, and continuing to wait if the condition is not satisfied.
Always use guarded wait Wait should be used in guarded loops
Notify or NotifyAll
Notify Wake up only one thread if any was
waiting less overhead (better performance) Applicable on limited cases
NotifyAll All the waiting threads can check its
guard condition
Java Memory Model Locking
Atomicity Visibility
Volatile Visibility only
Ordering Intra-thread semantics only Synchronization order is total ordering
among threads which locks or unlocks the lock