0
Double Checked Locking For Java Singletons Parag Shah Twitter: @coding_insights Blog: http://blog.adaptivesoftware.biz
Singleton
Is This A Safe Singleton ? This Singleton is not thread safe
This Singleton Is Not Threadsafe
Lets Make It Threadsafe
That Was Expensive Making it threadsafe incurred the expense of obtaining a monitor for every invocation of getInstance()
Let's Use Double Checked Locking
Will It Work ? Very Smart... but this does not work
Why ? <ul><li>Instructions can be re-ordered </li><ul><li>Compiler
Processor </li></ul><li>Memory reads and writes may appear to be re-ordered due to processor caches </li></ul>
instance = new LoneRanger(””)   !=  Atomic
Compiler Reoredering
Compiler Reoredering Pseudocode
Can We Prevent Reoredering ? <ul><li>Reordering will be done at will by the compiler or the processor if it can prove cert...
Memory Barrier <ul><li>Low level (processor) construct
Instructions can be reordered within a MB but outside it
An MB causes synching of memory cache with main memory </li></ul>
Memory Barrier Instr1 Instr2 Instr3 Instr4 Instr5 Allowed Not Allowed Not Allowed cache Main Memory
Memory Barrier <ul>The  synchronized  keyword in Java creates a memory barrier </ul>
Double Checked Locking With Memory Barrier
Monitor Exit Semantics <ul><li>Instructions that happen (in the program) before  the monitor exit should always happen bef...
Instructions that happen after the monitor exit MAY be reordered before the monitor exit </li></ul>
Double Checked Locking With Memory Barrier
Double Checked Locking With Memory Barrier
OK So What The Hell Will Work ? volatile   (but only after JDK 1.5) Volatile fields are special fields which are used to c...
Semantics of volatile Thread T1 Thread 2 Processor P1 Processor P2 cache cache MAIN MEMORY Flush Invalidate
Double Checked Locking With volatile
Singleton With Static Initializer
Singleton With Static Initializer <ul><li>Static initializer is the simplest solution
May not be lazily loaded
Upcoming SlideShare
Loading in...5
×

Double checkedlockingjavasingletons

3,030

Published on

Java Singletons and double checked locking

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
3,030
On Slideshare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Double checkedlockingjavasingletons"

  1. 1. Double Checked Locking For Java Singletons Parag Shah Twitter: @coding_insights Blog: http://blog.adaptivesoftware.biz
  2. 2. Singleton
  3. 3. Is This A Safe Singleton ? This Singleton is not thread safe
  4. 4. This Singleton Is Not Threadsafe
  5. 5. Lets Make It Threadsafe
  6. 6. That Was Expensive Making it threadsafe incurred the expense of obtaining a monitor for every invocation of getInstance()
  7. 7. Let's Use Double Checked Locking
  8. 8. Will It Work ? Very Smart... but this does not work
  9. 9. Why ? <ul><li>Instructions can be re-ordered </li><ul><li>Compiler
  10. 10. Processor </li></ul><li>Memory reads and writes may appear to be re-ordered due to processor caches </li></ul>
  11. 11. instance = new LoneRanger(””) != Atomic
  12. 12. Compiler Reoredering
  13. 13. Compiler Reoredering Pseudocode
  14. 14. Can We Prevent Reoredering ? <ul><li>Reordering will be done at will by the compiler or the processor if it can prove certain things </li><ul><li>A version of the Symantec JIT VM would inline the constructor and reorder as shown </li></ul><li>An intuitive thought - Reordering may not be done accross memory barriers </li></ul>
  15. 15. Memory Barrier <ul><li>Low level (processor) construct
  16. 16. Instructions can be reordered within a MB but outside it
  17. 17. An MB causes synching of memory cache with main memory </li></ul>
  18. 18. Memory Barrier Instr1 Instr2 Instr3 Instr4 Instr5 Allowed Not Allowed Not Allowed cache Main Memory
  19. 19. Memory Barrier <ul>The synchronized keyword in Java creates a memory barrier </ul>
  20. 20. Double Checked Locking With Memory Barrier
  21. 21. Monitor Exit Semantics <ul><li>Instructions that happen (in the program) before the monitor exit should always happen before the monitor exit
  22. 22. Instructions that happen after the monitor exit MAY be reordered before the monitor exit </li></ul>
  23. 23. Double Checked Locking With Memory Barrier
  24. 24. Double Checked Locking With Memory Barrier
  25. 25. OK So What The Hell Will Work ? volatile (but only after JDK 1.5) Volatile fields are special fields which are used to communicate state changes between threads.
  26. 26. Semantics of volatile Thread T1 Thread 2 Processor P1 Processor P2 cache cache MAIN MEMORY Flush Invalidate
  27. 27. Double Checked Locking With volatile
  28. 28. Singleton With Static Initializer
  29. 29. Singleton With Static Initializer <ul><li>Static initializer is the simplest solution
  30. 30. May not be lazily loaded
  31. 31. All information to construct the Singleton may not be available when the class is loaded </li></ul>
  32. 32. Summary <ul><li>The compiler as well as the processor can reorder instructions under certain conditions
  33. 33. Even if the instructions are not re-ordered, a thread may still see a stale value of a field due to memory caching
  34. 34. Synchronization creates an implicit memory barrier </li></ul>
  35. 35. Summary <ul><li>Code cannot be moved from within the memory barrier to outside of it
  36. 36. Code MAY be moved from outside a memory barrier to within it
  37. 37. Volatile fields cannot be re-ordered with respect to other volatile fields, and are very difficult to reorder in respect to non volatile fields </li></ul>
  38. 38. Summary <ul><li>A read to a volatile field invalidates the processor cache and fetches values from main memory
  39. 39. A write to a volatile field flushes the value to main memory
  40. 40. These semantics are all part of the Java memory model </li></ul>
  41. 41. Resources <ul><li>The Java Memory Model
  42. 42. The ”double checked locking is broken” declaration - Bill Pugh
  43. 43. Double checked locking – clever but broken By Brian Goetz
  44. 44. Warning! Threading in a multiprocessor world By Allen Holub </li></ul>
  45. 45. It's a different matter, in contemporary times the Singleton pattern is considered evil ! THANK YOU
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×