Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 What is the “volatile” Keyword? You can skip locking (thread synchronization) in some cases by using the volatile variables. –No locking/unlocking =

Similar presentations


Presentation on theme: "1 What is the “volatile” Keyword? You can skip locking (thread synchronization) in some cases by using the volatile variables. –No locking/unlocking ="— Presentation transcript:

1 1 What is the “volatile” Keyword? You can skip locking (thread synchronization) in some cases by using the volatile variables. –No locking/unlocking = less overhead –No need to synchronize threads, but need to synchronize memory One of the most typical cases is to use a latch.

2 2ThreadUnsafeCounter.java

3 3 The variable done is not protected from two threads. Main threadAnother thread run() done --> false while( !done ) --> true and try to execute a loop Reaches the end of a time slice Gain a time slice setDone(); done --> true Reaches the end of a time slice (done --> true) Gain a time slice done -->false execute a loop Inconsistent/unsynchronized

4 4ThreadSafeCounter.java The variable done is protected with a lock. public void setDone(){ lock.lock(); done = true; lock.unlock(); } public void run(){ try{ while( true ){ lock.lock(); if( done ) return; lock.unlock(); counter++; ……

5 5VolatileLatch.java The variable done is protected with the volatile keyword. –private volatile boolean done = false;

6 6 JVM Memory Model (J2SE 5.0-) Thread A Working memory Thread B Working memory JVM Main Memory An instance Data field Lock Main memory Working memory Local copy of data/values Proxy/fake memory Contained in a thread control block.

7 7 Thread A Working memory “abc” Thread B Working memory JVM Main Memory “abc” An instance Data field Lock Read operation: When thread A reads a value for the first time… (1) Read data from the main memory (2) Store it in the local working memory. Always Read-Use. When thread A accesses the same variable… Use OR Read-Use It’s up to JVM designers, but probably, Use only. read use

8 8 Thread A Working memory “abc” Thread B Working memory JVM Main Memory “abc” An instance Data field Lock Write/assignment operation: When thread A assigns a value to a data field… (1)Assign the value to a data field in the local working mem (2) Write it to the main mem. Always Assign-Write. It is not determined when “write” occurs. Maybe immediately after “assign,” but some interval may exist. If thread A continuously assigns different values to the the same data field…. Assign-Write… Assign-Write Assign-Assign…-Assign-Write write assign

9 9 Thread A Working memory “abc” Thread B Working memory “abc” JVM Main Memory “abc” An instance Data field Lock write assign unlock lock

10 10 JVM actions –read, write, use, assign, lock, unlock –indivisible operations. –No context (thread) switches during the execution of each operation.

11 11 When a thread tries and execute atomic code… lock.lock() // atomic code; lock.unlock(); Public synchronized void setDone(){ atomic code } –a JVM performs two things: Thread synchronization –Only one thread enters and executes atomic code at a time. –All other threads wait. Memory synchronization –Upon entering atomic code »Flush working memory to the main memory »Destroy working memory –Upon existing atomic code »Flush working memory to the main memory

12 12 Thread Unsafety Failure of thread synchronization Failure of memory synchronization –Inconsistency between data in working and main memories. Thread A W Mem Thread B W Mem Main Mem Thread A W Mem Thread B W Mem Main Mem Thread A W Mem Thread B W Mem Main Mem 1 Thread switch 10 1 assignuseassign write 10 1

13 13 Volatile Variables When a thread reads/writes (uses/assigns) a value from/to a volatile variable… – Memory synchronization is always performed. Thread A W Mem Thread B W Mem Main Mem Thread A W Mem Thread B W Mem Main Mem Thread A W Mem Thread B W Mem Main Mem 1 Thread switch 10 1 assignuseassign write Thread A W Mem Thread B W Mem Main Mem Thread A W Mem Thread B W Mem Main Mem Thread A W Mem Thread B W Mem Main Mem 1 10 assign use assign write 110 write 110 read

14 14 No need to use locks to perform memory synchronization for volatile variables. –No locking/unlocking = less overhead –No need to synchronize threads, but need to synchronize memory One of the most typical cases is to use a latch. Thread A W Mem Thread B W Mem Main Mem Thread A W Mem Thread B W Mem Main Mem Thread A W Mem Thread B W Mem Main Mem 1 10 assign use assign write 110 read Thread switch

15 15 VolatileLatch.java Revisited Multiple writer threads always assign true to the done variable. –No other possible state changes.

16 16 A shared variable is synchronized b/w threads. Main threadAnother thread run() done --> false while( !done ) --> true and try to execute a loop Reaches the end of a time slice Gain a time slice setDone(); done --> true Reaches the end of a time slice (done --> true) Gain a time slice Read a value of done again from the main memory done -->true exit a loop Consistent/synchronized

17 17Notes “Volatile” works for single write/read operations. volatile int a; a = 0 If( a == 0 ){… –No intermediate states a = a + 1; a++; Do not use volatile for arrays/objects.

18 18 Singleton Design Pattern Guarantee that a class has only one instance. –public class Singleton{ private static Singleton instance = null; private Date date; private Singleton(){} public static synchronized Singleton getInstance() { if( instance==null){ instance = new Singleton(); } return instance; } public get Date() { return date; } }

19 19 Double Checked Locking Pattern Test-test-and-set –Public class Singleton{ private static Singleton instance = null; private Date date = new Date(); private ReentrantLock lock; private Singleton(){} public get Date() { return date; } public static Singleton getInstance() { if( instance==null ){ lock.lock(); if( instance == null ){ instance = new Singleton(); } lock.unlock(); } return instance; } }

20 20 Threaded Singleton in Java –Public class Singleton{ private static Singleton instance = new Singleton(); private Date date = new Date(); private Singleton(){} public get Date() { return date; } public static Singleton getInstance() { return instance; }


Download ppt "1 What is the “volatile” Keyword? You can skip locking (thread synchronization) in some cases by using the volatile variables. –No locking/unlocking ="

Similar presentations


Ads by Google