lecture on java concurrency day 1 on jan 21, 2009

13
Practical Concurrency January, 2009 http://javadom.blogspot.com/2011/06/ lecture-on-java-concurrency-day- 1.html

Upload: kyung-koo-yoon

Post on 15-Dec-2014

660 views

Category:

Technology


4 download

DESCRIPTION

Lecture on Java Concurrency Day 1 on Jan 21, 2009. (in Korean)Lectures are 4 days in all.See http://javadom.blogspot.com/2011/06/lecture-on-java-concurrency-day-1.html

TRANSCRIPT

Page 1: Lecture on Java Concurrency Day 1 on Jan 21, 2009

Practical Concurrency

January, 2009

http://javadom.blogspot.com/2011/06/lecture-on-java-concurrency-day-1.html

Page 2: Lecture on Java Concurrency Day 1 on Jan 21, 2009

Contents

Concurrency Basics Designing Concurrency Concurrent Utils

Page 3: Lecture on Java Concurrency Day 1 on Jan 21, 2009

Day 1 (9am, Jan 21, 2009)

Hardware BasicsThread BasicsThread Revisited

Page 4: Lecture on Java Concurrency Day 1 on Jan 21, 2009

1-1 Hardware Basics

ProcessorInterconnectMemoryCache

Page 5: Lecture on Java Concurrency Day 1 on Jan 21, 2009

Hardware

Page 6: Lecture on Java Concurrency Day 1 on Jan 21, 2009

1-2 Thread Basics

Race ConditionLock and Conditioninterrupt

Page 7: Lecture on Java Concurrency Day 1 on Jan 21, 2009

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

Page 8: Lecture on Java Concurrency Day 1 on Jan 21, 2009

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

Page 9: Lecture on Java Concurrency Day 1 on Jan 21, 2009

interrupt

static boolean Thread.interrupted Tells current interrupt flag and reset

Boolean Thread.isInterrupted Just tell current interrupt flag

InterruptedException Object.wait Thread.sleep

Page 10: Lecture on Java Concurrency Day 1 on Jan 21, 2009

1-3 Thread Revisited

Spurious wakeupNotify or notifyAllVolatile and visibility

Page 11: Lecture on Java Concurrency Day 1 on Jan 21, 2009

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

Page 12: Lecture on Java Concurrency Day 1 on Jan 21, 2009

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

Page 13: Lecture on Java Concurrency Day 1 on Jan 21, 2009

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