Java Memory Model 
for mortals 
Nikolas Papirniy
Java and c++ developers 
Java dev: May I…... 
C++ dev: DEPENDS ON 
IMPLEMENTATION! 
Java dev: Wait, I didn’t ask a 
question
Why should I spend time on JMM?
Why should I spend time on JMM? 
It’s getting asked on job interviews!
Why should I spend time on JMM
Why not everyone knows JMM?
JMM is important!
Today’s goal!
Agenda? 
1. Program Order 
2. Memory Model 
3. Sequential Consistency 
4. Synchronization action 
5. Synchronization order 
6. Synchronezed with 
7. Happens-before 
8. Double checked locking
Baruch about concurrency 
In this case, you can shoot 
yourself in any limb of any 
caliber!
Is it about Shipilev? 
Java Memory Model Pragmatics
There is a little problem
Who visit Shipilev’s presentations? 
1) Those who don’t understand Shipilev 
2) Those who think they understand Shipilev 
3) Shipilev
What was before JMM?
Single threaded 
1. int a = 2; 
2. int b = 2; 
3. int c = a + b 
4. System.out.println(c); 
Program Order 
?
Single threaded
What have we learned? 
1. Program order
What about now?
int v = 0; 
Thread 1 Thread 2 
1a. v = 10; 1b. v = 20; 
2b. System.out.println(v); 
?
int v = 0; 
Thread 1 Thread 2 
1a. v = 10; 1b. v = 20; 
2b. System.out.println(v); 
A 
v = 10; 
v = 20; 
print v
int v = 0; 
Thread 1 Thread 2 
1a. v = 10; 1b. v = 20; 
2b. System.out.println(v); 
A 
v = 10; 
v = 20; 
print v 
B 
v = 20; 
v = 10; 
print v
int v = 0; 
Thread 1 Thread 2 
1a. v = 10; 1b. v = 20; 
2b. System.out.println(v); 
A 
v = 10; 
v = 20; 
print v 
B 
v = 20; 
v = 10; 
print v 
C 
v = 20; 
print v 
v = 10
int v = 0; 
Thread 1 Thread 2 
1a. v = 10; 1b. v = 20; 
2b. System.out.println(v); 
A 
v = 10; 
v = 20; 
print v 
B 
v = 20; 
v = 10; 
print v 
C 
v = 20; 
print v 
v = 10 
D 
print v 
v = 20; 
v = 10
int v = 0; 
Thread 1 Thread 2 
1a. v = 10; 1b. v = 20; 
2b. System.out.println(v); 
A 
v = 10; 
v = 20; 
print v 
B 
v = 20; 
v = 10; 
print v 
C 
v = 20; 
print v 
v = 10 
D 
print v 
v = 20; 
v = 10
WAT?
Situation…. 
1. Compiler 
2. Runtime 
3. Hardware
Hello memory model!
Memory model QUESTION 
What will you see in a certain moment of 
program execution?
What is Java Memory Model
int v = 0; 
Thread 1 Thread 2 
1a. v = 10; 1b. v = 20; 
2b. System.out.println(v); 
A 
v = 10; 
v = 20; 
print v 
B 
v = 20; 
v = 10; 
print v 
C 
v = 20; 
print v 
v = 10 
D 
print v 
v = 20; 
v = 10
What have we learned? 
1. Program order 
2. Memory model
Sequentially consistent 
(Lamport 1979) 
1. Executing instructions one at a time 
2. Each instruction see result of previos 
instructions 
3. Total order is consistent with Program Order
Sequentially consistent 
1 
5 
3 
4 
2
Multi-core heaven?
Caches
Cache line 
read variable 
Cache 1 
64 bytes 
Cache line
Cache line 
Cache 1 
Cache line 
Cache line
What have we learned? 
1. Program order 
2. Memory model 
3. Sequential Consistency
SC Reality: concurrency 
1 
write 
1 
5 
4 
read 
2
Data race 
If: 
1. Several threads access one variable 
2. One thread writes 
3. No synchronization
Data race
Tread 1: read A 
Thread 1 Thread 2 
A Shared memory
Tread 1: read A (cont) 
Thread 1 Thread 2 
A Shared memory 
A
Tread 2: read A 
Thread 1 Thread 2 
A A 
A Shared memory
Thread 1: write A 
Thread 1 Thread 2 
A A 
A Shared memory
Thread 2: write A 
Thread 1 Thread 2 
A A 
A Shared memory
Thread 1: memory flushes to main memory 
Thread 1 Thread 2 
A A 
A 
Shared memory
Thread 2: memory flushes to main memory 
Thread 1 Thread 2 
A A 
A 
Shared memory
Read from local variable 
! 
Thread 1 Thread 2 
A A 
A 
Shared memory
Synchronization 
read 
read write 
write read 
write 
Synchronization
Synchronization action 
➔Read / write volatile 
➔Lock / unlock monitor 
➔And other
Read / write volatile 
volatile int v = 0; 
Thread 1 Thread 2 
1a. v = 10; 1b. v = 20; 
2b. System.out.println(v); 
SA
Read / write volatile 
volatile int v = 0; 
Thread 1 Thread 2 
1a. v = 10; 1b. v = 20; 
2b. System.out.println(v); 
SA
Read / write volatile 
volatile int v = 0; 
Thread 1 Thread 2 
1a. v = 10; 1b. v = 20; 
2b. System.out.println(v); 
SA
Read / write volatile 
volatile int v = 0; 
Thread 1 Thread 2 
1a. v = 10; 1b. v = 20; 
2b. System.out.println(v); 
SA
Lock / unlock monitor 
Object lock = new Object(); 
Thread 1 Thread 2 
1a. synchronized(lock) { 
2a. // actions 
3a. } 
1b. synchronized(lock) { 
2b. // actions 
3b. } 
SA
Lock / unlock monitor 
Object lock = new Object(); 
Thread 1 Thread 2 
1a. synchronized(lock) { 
2a. // actions 
3a. } 
1b. synchronized(lock) { 
2b. // actions 
3b. } 
SA
Lock / unlock monitor 
Object lock = new Object(); 
Thread 1 Thread 2 
1a. synchronized(lock) { 
2a. // actions 
3a. } 
1b. synchronized(lock) { 
2b. // actions 
3b. } 
SA
Lock / unlock monitor 
Object lock = new Object(); 
Thread 1 Thread 2 
1a. synchronized(lock) { 
2a. // actions 
3a. } 
1b. synchronized(lock) { 
2b. // actions 
3b. } 
SA
Lock / unlock monitor 
Object lock = new Object(); 
Thread 1 Thread 2 
1a. synchronized(lock) { 
2a. // actions 
3a. } 
1b. synchronized(lock) { 
2b. // actions 
3b. } 
SA
SC Reality: visibility 
int v = 0; 
Thread 1 Thread 2 
1a. v = 10; 1b. v = 20; 
2b. System.out.println(v); 
20 
B 
v = 20; 
v = 10; 
print v
What have we learned? 
1. Program order 
2. Memory model 
3. Sequential Consistency 
4. Synchronization action
Visibility of volatile variables
Thead 1: read A 
Thread 1 Thread 2 
read barrier 
A Shared memory
Thead 1: read A (cont) 
Thread 1 Thread 2 
A Shared memory 
A
Thead 2: read A 
Thread 1 Thread 2 
A Shared memory 
A 
read barrier 
A
Thead 1: write A 
Thread 1 Thread 2 
A A 
write barrier 
A 
Shared memory
Thead 2: write A 
Thread 1 Thread 2 
A A 
A 
Shared memory 
write barrier
Thead 2: write A 
Thread 1 Thread 2 
A A 
A 
Shared memory 
write barrier
Thead 1: read A 
Thread 1 Thread 2 
A 
Shared memory 
A 
read barrier 
A
SC Reality: atomicity 
long v = 0L; 
Thread 1 Thread 2 
v = Long.MAX_VALUE; System.out.println(v); 
A. Long.MAX_VALUE B. 0L 
C. FFFF FFFF 0000 0000 D. 0000 0000 FFFF FFFF
SC Reality: atomicity 
long v = 0L; 
Thread 1 Thread 2 
v = Long.MAX_VALUE; System.out.println(v); 
A. Long.MAX_VALUE B. 0L 
C. FFFF FFFF 0000 0000 D. 0000 0000 FFFF FFFF
SC Reality: atomicity
Atomicity JMM 
A single write to a non-volatile long or double 
value is treated as two separate writes: one to 
each 32-bit half.
SC Reality: atomicity - SOLUTION 
AtomicLong v = new AtomicLong(0L); 
Thread 1 Thread 2 
l.set(Long.MAX_VALUE); System.out.println(v.get()); 
A. Long.MAX_VALUE B. 0L 
C. FFFF FFFF 0000 0000 D. 0000 0000 FFFF FFFF
SC Reality: atomicity - SOLUTION 
AtomicLong v = new AtomicLong(0L); 
Thread 1 Thread 2 
l.set(Long.MAX_VALUE); System.out.println(v.get()); 
A. Long.MAX_VALUE B. 0L 
C. FFFF FFFF 0000 0000 D. 0000 0000 FFFF FFFF
How?
Synchronization order 
Thread 1 Thread 2 
2 1 
1 
3 
2 
3 
4 4 
SA 
SA 
SA 
SA 
5 5
Synchronization order 
volatile int x, y; 
Thread 1 Thread 2 
x = 10; 
int result1 = y; 
y = 20; 
int result2 = x;
Synchronization order (1) 
volatile int x, y; 
Thread 1 Thread 2 
x = 10; 
int result1 = y; 
y = 20; 
int result2 = x;
Synchronization order (2) 
volatile int x, y; 
Thread 1 Thread 2 
x = 10; 
int result1 = y; 
y = 20; 
int result2 = x;
Synchronization order (3) 
volatile int x, y; 
Thread 1 Thread 2 
x = 10; 
int result1 = y; 
y = 20; 
int result2 = x;
Synchronization order (4) 
volatile int x, y; 
Thread 1 Thread 2 
x = 10; 
int result1 = y; 
y = 20; 
int result2 = x; 
?
Synchronization order (4) 
volatile int x, y; 
Thread 1 Thread 2 
x = 10; 
int result1 = y; 
y = 20; 
int result2 = x; 
Synchronization order consistent with Program Order
What have we learned? 
1. Program order 
2. Memory model 
3. Sequential Consistency 
4. Synchronization action 
5. Synchronization order
Synchronizes with 
2 
1 
2 
3 
1 
3 
4 4 
SA 
SA 
Thread 1 Thread 2
Synchronizes-with Consistency 
● volatile read - can read nearest volatile write 
on SO 
● monitor unlock - can release nearest monitor 
lock on SO
Synchronizes-with 
int x; 
volatile int y; 
Thread 1 Thread 2 
x = 10; 
int result1 = y; 
write x: 10 
read y: ? read x: ? 
y = 20; 
int result2 = x; 
write y: 20
Synchronizes-with 
int x; 
volatile int y; 
Thread 1 Thread 2 
x = 10; 
int result1 = y; 
write x: 10 
read y: ? read x: ? 
y = 20; 
int result2 = x; 
write y: 20 
SW 
Write y 
Read y 
SW
What have we learned? 
1. Program order 
2. Memory model 
3. Sequential Consistency 
4. Synchronization action 
5. Synchronization order 
6. Synchronezed with
One more thing 
Thread 1 Thread 2 
2 
1 
2 
3 
4 
1 
3 
4 
SA 
SA 
5 5
One more thing 
Thread 1 Thread 2 
2 
1 
2 
3 
4 
1 
3 
4 
SA 
SA 
5 5 
PO 
PO
One more thing 
Thread 1 Thread 2 
2 
1 
2 
3 
4 
1 
3 
4 
SA 
SA 
5 5 
PO 
PO 
SW
Happens-before! 
Thread 1 Thread 2 
2 
1 
2 
3 
4 
1 
3 
4 
SA 
SA 
5 5 
PO 
PO 
SW
Happens-before 
Program Order 
Synchronizati 
on Order 
Synchronizes 
With 
Happens-before 
Visibility
Happens-before for smarties 
HB = {PO & SW}+
What have we learned? 
1. Program order 
2. Memory model 
3. Sequential Consistency 
4. Synchronization action 
5. Synchronization order 
6. Synchronezed with 
7. Happens-before
Double checked locking 
public static class SingletonFactory{ 
private Singleton instance; 
public Singleton getInstance () { 
if ( instance == null ) { 
synchronized ( this ) { 
if ( instance == null ) { 
instance = new Singleton (); 
} 
} 
} 
return instance ; 
} 
} 
public static class Singleton { 
public Integer x; 
public Singleton () { x = 42; } 
}
Double checked locking 
public static class SingletonFactory{ 
private Singleton instance; 
public Singleton getInstance () { 
if ( instance == null ) { 
synchronized ( this ) { 
if ( instance == null ) { 
instance = alloc + Singleton object copy 
instance.x = 42 
} 
} 
} 
return instance ; 
} 
} 
public static class Singleton { 
public Integer x; 
public Singleton () { x = 42; } 
} 
Constructor inline
Double checked locking 
public static class SingletonFactory{ 
private Singleton instance; 
public Singleton getInstance () { 
if ( instance == null ) { 
synchronized ( this ) { 
if ( instance == null ) { 
instance = new Singleton (); 
} 
} 
} 
return instance ; 
} 
} 
public static class Singleton { 
public Integer x; 
public Singleton () { x = 42; } 
} 
Returns null
Double checked locking 
public static class SingletonFactory{ 
private Singleton instance; 
public Singleton getInstance () { 
if ( instance == null ) { 
synchronized ( this ) { 
if ( instance == null ) { 
instance = new Singleton (); 
} 
} 
} 
return instance ; 
} 
} 
public static class Singleton { 
public Integer x; 
public Singleton () { x = 42; } 
} 
Returns null
Double checked locking solution 
public static class SingletonFactory{ 
private volatile Singleton instance; 
public Singleton getInstance () { 
if ( instance == null ) { 
synchronized ( this ) { 
if ( instance == null ) { 
instance = new Singleton (); 
} 
} 
} 
return instance; 
} 
} 
public static class Singleton { 
public Integer x; 
public Singleton () { x = 42; } 
}
What have we learned? 
1. Program order 
2. Memory model 
3. Sequential Consistency 
4. Synchronization action 
5. Synchronization order 
6. Synchronezed with 
7. Happens-before 
8. Double checked locking
Reading

Николай Папирный Тема: "Java memory model для простых смертных"

  • 1.
    Java Memory Model for mortals Nikolas Papirniy
  • 2.
    Java and c++developers Java dev: May I…... C++ dev: DEPENDS ON IMPLEMENTATION! Java dev: Wait, I didn’t ask a question
  • 3.
    Why should Ispend time on JMM?
  • 4.
    Why should Ispend time on JMM? It’s getting asked on job interviews!
  • 5.
    Why should Ispend time on JMM
  • 6.
    Why not everyoneknows JMM?
  • 7.
  • 8.
  • 9.
    Agenda? 1. ProgramOrder 2. Memory Model 3. Sequential Consistency 4. Synchronization action 5. Synchronization order 6. Synchronezed with 7. Happens-before 8. Double checked locking
  • 10.
    Baruch about concurrency In this case, you can shoot yourself in any limb of any caliber!
  • 11.
    Is it aboutShipilev? Java Memory Model Pragmatics
  • 12.
    There is alittle problem
  • 13.
    Who visit Shipilev’spresentations? 1) Those who don’t understand Shipilev 2) Those who think they understand Shipilev 3) Shipilev
  • 14.
  • 15.
    Single threaded 1.int a = 2; 2. int b = 2; 3. int c = a + b 4. System.out.println(c); Program Order ?
  • 16.
  • 17.
    What have welearned? 1. Program order
  • 18.
  • 19.
    int v =0; Thread 1 Thread 2 1a. v = 10; 1b. v = 20; 2b. System.out.println(v); ?
  • 20.
    int v =0; Thread 1 Thread 2 1a. v = 10; 1b. v = 20; 2b. System.out.println(v); A v = 10; v = 20; print v
  • 21.
    int v =0; Thread 1 Thread 2 1a. v = 10; 1b. v = 20; 2b. System.out.println(v); A v = 10; v = 20; print v B v = 20; v = 10; print v
  • 22.
    int v =0; Thread 1 Thread 2 1a. v = 10; 1b. v = 20; 2b. System.out.println(v); A v = 10; v = 20; print v B v = 20; v = 10; print v C v = 20; print v v = 10
  • 23.
    int v =0; Thread 1 Thread 2 1a. v = 10; 1b. v = 20; 2b. System.out.println(v); A v = 10; v = 20; print v B v = 20; v = 10; print v C v = 20; print v v = 10 D print v v = 20; v = 10
  • 24.
    int v =0; Thread 1 Thread 2 1a. v = 10; 1b. v = 20; 2b. System.out.println(v); A v = 10; v = 20; print v B v = 20; v = 10; print v C v = 20; print v v = 10 D print v v = 20; v = 10
  • 25.
  • 26.
    Situation…. 1. Compiler 2. Runtime 3. Hardware
  • 27.
  • 28.
    Memory model QUESTION What will you see in a certain moment of program execution?
  • 29.
    What is JavaMemory Model
  • 30.
    int v =0; Thread 1 Thread 2 1a. v = 10; 1b. v = 20; 2b. System.out.println(v); A v = 10; v = 20; print v B v = 20; v = 10; print v C v = 20; print v v = 10 D print v v = 20; v = 10
  • 31.
    What have welearned? 1. Program order 2. Memory model
  • 32.
    Sequentially consistent (Lamport1979) 1. Executing instructions one at a time 2. Each instruction see result of previos instructions 3. Total order is consistent with Program Order
  • 33.
  • 34.
  • 35.
  • 36.
    Cache line readvariable Cache 1 64 bytes Cache line
  • 37.
    Cache line Cache1 Cache line Cache line
  • 39.
    What have welearned? 1. Program order 2. Memory model 3. Sequential Consistency
  • 40.
    SC Reality: concurrency 1 write 1 5 4 read 2
  • 41.
    Data race If: 1. Several threads access one variable 2. One thread writes 3. No synchronization
  • 42.
  • 43.
    Tread 1: readA Thread 1 Thread 2 A Shared memory
  • 44.
    Tread 1: readA (cont) Thread 1 Thread 2 A Shared memory A
  • 45.
    Tread 2: readA Thread 1 Thread 2 A A A Shared memory
  • 46.
    Thread 1: writeA Thread 1 Thread 2 A A A Shared memory
  • 47.
    Thread 2: writeA Thread 1 Thread 2 A A A Shared memory
  • 48.
    Thread 1: memoryflushes to main memory Thread 1 Thread 2 A A A Shared memory
  • 49.
    Thread 2: memoryflushes to main memory Thread 1 Thread 2 A A A Shared memory
  • 50.
    Read from localvariable ! Thread 1 Thread 2 A A A Shared memory
  • 51.
    Synchronization read readwrite write read write Synchronization
  • 52.
    Synchronization action ➔Read/ write volatile ➔Lock / unlock monitor ➔And other
  • 53.
    Read / writevolatile volatile int v = 0; Thread 1 Thread 2 1a. v = 10; 1b. v = 20; 2b. System.out.println(v); SA
  • 54.
    Read / writevolatile volatile int v = 0; Thread 1 Thread 2 1a. v = 10; 1b. v = 20; 2b. System.out.println(v); SA
  • 55.
    Read / writevolatile volatile int v = 0; Thread 1 Thread 2 1a. v = 10; 1b. v = 20; 2b. System.out.println(v); SA
  • 56.
    Read / writevolatile volatile int v = 0; Thread 1 Thread 2 1a. v = 10; 1b. v = 20; 2b. System.out.println(v); SA
  • 57.
    Lock / unlockmonitor Object lock = new Object(); Thread 1 Thread 2 1a. synchronized(lock) { 2a. // actions 3a. } 1b. synchronized(lock) { 2b. // actions 3b. } SA
  • 58.
    Lock / unlockmonitor Object lock = new Object(); Thread 1 Thread 2 1a. synchronized(lock) { 2a. // actions 3a. } 1b. synchronized(lock) { 2b. // actions 3b. } SA
  • 59.
    Lock / unlockmonitor Object lock = new Object(); Thread 1 Thread 2 1a. synchronized(lock) { 2a. // actions 3a. } 1b. synchronized(lock) { 2b. // actions 3b. } SA
  • 60.
    Lock / unlockmonitor Object lock = new Object(); Thread 1 Thread 2 1a. synchronized(lock) { 2a. // actions 3a. } 1b. synchronized(lock) { 2b. // actions 3b. } SA
  • 61.
    Lock / unlockmonitor Object lock = new Object(); Thread 1 Thread 2 1a. synchronized(lock) { 2a. // actions 3a. } 1b. synchronized(lock) { 2b. // actions 3b. } SA
  • 62.
    SC Reality: visibility int v = 0; Thread 1 Thread 2 1a. v = 10; 1b. v = 20; 2b. System.out.println(v); 20 B v = 20; v = 10; print v
  • 63.
    What have welearned? 1. Program order 2. Memory model 3. Sequential Consistency 4. Synchronization action
  • 64.
  • 65.
    Thead 1: readA Thread 1 Thread 2 read barrier A Shared memory
  • 66.
    Thead 1: readA (cont) Thread 1 Thread 2 A Shared memory A
  • 67.
    Thead 2: readA Thread 1 Thread 2 A Shared memory A read barrier A
  • 68.
    Thead 1: writeA Thread 1 Thread 2 A A write barrier A Shared memory
  • 69.
    Thead 2: writeA Thread 1 Thread 2 A A A Shared memory write barrier
  • 70.
    Thead 2: writeA Thread 1 Thread 2 A A A Shared memory write barrier
  • 71.
    Thead 1: readA Thread 1 Thread 2 A Shared memory A read barrier A
  • 72.
    SC Reality: atomicity long v = 0L; Thread 1 Thread 2 v = Long.MAX_VALUE; System.out.println(v); A. Long.MAX_VALUE B. 0L C. FFFF FFFF 0000 0000 D. 0000 0000 FFFF FFFF
  • 73.
    SC Reality: atomicity long v = 0L; Thread 1 Thread 2 v = Long.MAX_VALUE; System.out.println(v); A. Long.MAX_VALUE B. 0L C. FFFF FFFF 0000 0000 D. 0000 0000 FFFF FFFF
  • 74.
  • 75.
    Atomicity JMM Asingle write to a non-volatile long or double value is treated as two separate writes: one to each 32-bit half.
  • 76.
    SC Reality: atomicity- SOLUTION AtomicLong v = new AtomicLong(0L); Thread 1 Thread 2 l.set(Long.MAX_VALUE); System.out.println(v.get()); A. Long.MAX_VALUE B. 0L C. FFFF FFFF 0000 0000 D. 0000 0000 FFFF FFFF
  • 77.
    SC Reality: atomicity- SOLUTION AtomicLong v = new AtomicLong(0L); Thread 1 Thread 2 l.set(Long.MAX_VALUE); System.out.println(v.get()); A. Long.MAX_VALUE B. 0L C. FFFF FFFF 0000 0000 D. 0000 0000 FFFF FFFF
  • 78.
  • 80.
    Synchronization order Thread1 Thread 2 2 1 1 3 2 3 4 4 SA SA SA SA 5 5
  • 81.
    Synchronization order volatileint x, y; Thread 1 Thread 2 x = 10; int result1 = y; y = 20; int result2 = x;
  • 82.
    Synchronization order (1) volatile int x, y; Thread 1 Thread 2 x = 10; int result1 = y; y = 20; int result2 = x;
  • 83.
    Synchronization order (2) volatile int x, y; Thread 1 Thread 2 x = 10; int result1 = y; y = 20; int result2 = x;
  • 84.
    Synchronization order (3) volatile int x, y; Thread 1 Thread 2 x = 10; int result1 = y; y = 20; int result2 = x;
  • 85.
    Synchronization order (4) volatile int x, y; Thread 1 Thread 2 x = 10; int result1 = y; y = 20; int result2 = x; ?
  • 86.
    Synchronization order (4) volatile int x, y; Thread 1 Thread 2 x = 10; int result1 = y; y = 20; int result2 = x; Synchronization order consistent with Program Order
  • 87.
    What have welearned? 1. Program order 2. Memory model 3. Sequential Consistency 4. Synchronization action 5. Synchronization order
  • 88.
    Synchronizes with 2 1 2 3 1 3 4 4 SA SA Thread 1 Thread 2
  • 89.
    Synchronizes-with Consistency ●volatile read - can read nearest volatile write on SO ● monitor unlock - can release nearest monitor lock on SO
  • 90.
    Synchronizes-with int x; volatile int y; Thread 1 Thread 2 x = 10; int result1 = y; write x: 10 read y: ? read x: ? y = 20; int result2 = x; write y: 20
  • 91.
    Synchronizes-with int x; volatile int y; Thread 1 Thread 2 x = 10; int result1 = y; write x: 10 read y: ? read x: ? y = 20; int result2 = x; write y: 20 SW Write y Read y SW
  • 92.
    What have welearned? 1. Program order 2. Memory model 3. Sequential Consistency 4. Synchronization action 5. Synchronization order 6. Synchronezed with
  • 94.
    One more thing Thread 1 Thread 2 2 1 2 3 4 1 3 4 SA SA 5 5
  • 95.
    One more thing Thread 1 Thread 2 2 1 2 3 4 1 3 4 SA SA 5 5 PO PO
  • 96.
    One more thing Thread 1 Thread 2 2 1 2 3 4 1 3 4 SA SA 5 5 PO PO SW
  • 97.
    Happens-before! Thread 1Thread 2 2 1 2 3 4 1 3 4 SA SA 5 5 PO PO SW
  • 98.
    Happens-before Program Order Synchronizati on Order Synchronizes With Happens-before Visibility
  • 99.
  • 100.
    What have welearned? 1. Program order 2. Memory model 3. Sequential Consistency 4. Synchronization action 5. Synchronization order 6. Synchronezed with 7. Happens-before
  • 101.
    Double checked locking public static class SingletonFactory{ private Singleton instance; public Singleton getInstance () { if ( instance == null ) { synchronized ( this ) { if ( instance == null ) { instance = new Singleton (); } } } return instance ; } } public static class Singleton { public Integer x; public Singleton () { x = 42; } }
  • 102.
    Double checked locking public static class SingletonFactory{ private Singleton instance; public Singleton getInstance () { if ( instance == null ) { synchronized ( this ) { if ( instance == null ) { instance = alloc + Singleton object copy instance.x = 42 } } } return instance ; } } public static class Singleton { public Integer x; public Singleton () { x = 42; } } Constructor inline
  • 103.
    Double checked locking public static class SingletonFactory{ private Singleton instance; public Singleton getInstance () { if ( instance == null ) { synchronized ( this ) { if ( instance == null ) { instance = new Singleton (); } } } return instance ; } } public static class Singleton { public Integer x; public Singleton () { x = 42; } } Returns null
  • 105.
    Double checked locking public static class SingletonFactory{ private Singleton instance; public Singleton getInstance () { if ( instance == null ) { synchronized ( this ) { if ( instance == null ) { instance = new Singleton (); } } } return instance ; } } public static class Singleton { public Integer x; public Singleton () { x = 42; } } Returns null
  • 106.
    Double checked lockingsolution public static class SingletonFactory{ private volatile Singleton instance; public Singleton getInstance () { if ( instance == null ) { synchronized ( this ) { if ( instance == null ) { instance = new Singleton (); } } } return instance; } } public static class Singleton { public Integer x; public Singleton () { x = 42; } }
  • 107.
    What have welearned? 1. Program order 2. Memory model 3. Sequential Consistency 4. Synchronization action 5. Synchronization order 6. Synchronezed with 7. Happens-before 8. Double checked locking
  • 108.

Editor's Notes