SlideShare une entreprise Scribd logo
1  sur  65
Télécharger pour lire hors ligne
JMM
Java Memory Model
Łukasz Koniecki
24/10/2016
About me
Java
Universe
Spring
MyFaces
JSF
Play
Spark
GWT
Vadin
Tapestry
Wicket
Spring MVC
Struts
Grails
REST API
JPA
GC
JVM
JAVA EE
Tomcat
Spark
Goal
• Familiarize with the JMM,
• How processor works?
• Recall how Java compiler and JVM work,
• JIT in action,
• Explain what is a data race and a correctly synchronized
program,
• Talk about synchronization and atomicity,
• Based on examples...
• Next-gen JMM...
§17.4 Memory Model
John von Neumann
Wikipedia: http://bit.ly/2cMU0GB
Von Neumann Architecture
Dummy program
public class Example {
int i, j;
public void myDummyMethod() {
i+=1;
j+=1;
i+=1;
...
}
}
RAM
i = 0
j = 0
Cache
Program execution
System Bus
public class Example {
int i, j;
public void myDummyMethod() {
i+=1;
j+=1;
i+=1;
...
}
}
The Java Memory Model for Practitioners: http://bit.ly/2cMXklJ
RAM
i = 0
j = 0
Cache
Program execution
System Bus
i = 0
public class Example {
int i, j;
public void myDummyMethod() {
i+=1;
j+=1;
i+=1;
...
}
}
RAM
i = 0
j = 0
Cache
Program execution
System Bus
i = 1
public class Example {
int i, j;
public void myDummyMethod() {
i+=1;
j+=1;
i+=1;
...
}
}
RAM
i = 0
j = 0
Cache
Program execution
System Bus
i = 1
public class Example {
int i, j;
public void myDummyMethod() {
i+=1;
j+=1;
i+=1;
...
}
}
RAM
i = 1
j = 0
Cache
Program execution
System Bus
public class Example {
int i, j;
public void myDummyMethod() {
i+=1;
j+=1;
i+=1;
...
}
}
RAM
i = 1
j = 0
Cache
Program execution
System Bus
j = 0
public class Example {
int i, j;
public void myDummyMethod() {
i+=1;
j+=1;
i+=1;
...
}
}
RAM
i = 1
j = 0
Cache
Program execution
System Bus
j = 1
public class Example {
int i, j;
public void myDummyMethod() {
i+=1;
j+=1;
i+=1;
...
}
}
RAM
i = 1
j = 1
Cache
Program execution
System Bus
j = 1
public class Example {
int i, j;
public void myDummyMethod() {
i+=1;
j+=1;
i+=1;
...
}
}
RAM
i = 1
j = 1
Cache
Program execution
System Bus
Sequentialy consistent
execution
public class Example {
int i, j;
public void myDummyMethod() {
i+=1;
j+=1;
i+=1;
...
}
}
PC World: http://bit.ly/2cE9f7q
Haswell-E processor
Our world in data: http://bit.ly/1NLxNcH
Moore’s Law
Moore’s Law
Our world in data: http://bit.ly/1NLxNcH
2006
Processor technology
• ...
• 22 nm – 2012
• 14 nm – 2014
• 10 nm – 2017
• 7 nm – ~2019
• 5 nm – ~2021
Wikipedia: http://bit.ly/2cMWoNg
Processor vs. Memory Performance
How L1 and L2 CPU caches work, and why they’re an essential part of modern chips: http://bit.ly/2cpHu1x
Wikipedia: http://bit.ly/2cm33me
Cache hierarchy in a modern processor
Wikipedia: http://bit.ly/2cm33me
Cache hierarchy in a modern processor
Important latency numbers
Core i7 Xeon 5500 Series Data Source Latency (approximate)
local L1 CACHE hit, ~4 cycles ( 2.1 - 1.2 ns )
local L2 CACHE hit, ~10 cycles ( 5.3 - 3.0 ns )
local L3 CACHE hit, line unshared ~40 cycles ( 21.4 - 12.0 ns )
local L3 CACHE hit, shared line in another core ~65 cycles ( 34.8 - 19.5 ns )
local L3 CACHE hit, modified in another core ~75 cycles ( 40.2 - 22.5 ns )
remote L3 CACHE (Ref: Fig.1 [Pg. 5]) ~100-300 cycles ( 160.7 - 30.0 ns )
local DRAM ~60 ns
remote DRAM ~100 ns
Performance Analysis Guide for Intel® Core™ i7 Processor and Intel® Xeon™ 5500 processors: http://intel.ly/2cV1ZFZ
Cache latency
Weak vs. Strong hardware Memory Models
Weak vs. Strong Memory Models: http://bit.ly/2cC4avk
x86/x64 processor memory model
R-R R-W
W-R W-W
Intel® 64 and IA-32 Architectures Software Developer’s Manual: http://intel.ly/2csMyB2
Processor P can read B
before it’s write to A is seen
by all processors
(processor can move its
own reads in front of its
own writes)
x86/x64 processor memory model
R-R R-W
W-R W-W
Intel® 64 and IA-32 Architectures Software Developer’s Manual: http://intel.ly/2csMyB2
Processor P can read B
before it’s write to A is seen
by all processors
(processor can move its
own reads in front of its
own writes)
How Java compiler works?
javac
Source
code
Byte
code
Bytecode
verifier
Class loader
JIT
JVM
OS
Native
code
Byte
code
JIT
•Profile guided,
•Speculatively optimizing,
•Backup strategies,
•Optimizes code for us,
•We don’t have to care so much about cache-wise
operations
Tiered compilation
time
throuput
startup
interpreted
C1
C2
sampling full speed
deoptimize
bail to interpreter
Tiered compilation (interpreter)
time
throuput
startup
interpreted
C1
C2
sampling full speed
deoptimize
bail to interpreter
Interpreter
• extremly slow,
• not profiling
Tiered compilation (C1 compiler)
time
throuput
startup
interpreted
C1
C2
sampling full speed
deoptimize
bail to interpreter
C1
• client,
• fast but dummy,
• does the profiling,
• e.g: branches, typechecks,
Tiered compilation (C2 compiler)
time
throuput
startup
interpreted
C1
C2
sampling full speed
deoptimize
bail to interpreter
C2
• server,
• slow but clever,
• aggresively optimizing,
• based on profile,
• e.g.: loop optimizations
(unswitching, unrolling),
Implicit Null Checking
Why do we need a JMM?
• Different platform memory models (none of them match the JMM!!!)
• Many JVM implementations,
• People don’t know how to program concurrently,
• Programmers: write reliable and multithreaded code,
• Compiler writers: implement optimization which will be a legal,
optimization according to the JLS
• Compiler: produce fast and optimal native code,
JMM
• Action: read and write to variable, lock and unlock of monitor, starting
and joining with thread,
• Happens-before partial order,
• Thread executing action B can see the results of action A (any thread),
there must be a happens-before relationship between A and B,
• Otherwise JVM is free to reorder,
Happens-before orderings
• Unlock of a monitor / lock of that monitor,
• Write to a volatile variable / read of that variable,
• Call to start() / any action in the started thread,
• All actions in a thread / any other thread successfully returns from
join() on that thread,
• Setting default values for variables, setting value to a final field in the
constructor / constructor finish,
• Write to an Atomic variable / read from that variable,
• Many java.util.concurrent methods,
JMM
• A promise for programmers: sequential consistency must be sacrificed to allow
optimizations, but it will still hold for data race free programs. This is the data
race free (DRF) guarantee.
• A promise for security: even for programs with data races, values should not
appear “out of thin air”, preventing unintended information leakage.
• A promise for compilers: common hardware and software optimizations should
be allowed as far as possible without violating the first two requirements.
Java Memory Model Examples: Good, Bad and Ugly: http://bit.ly/2cZfF1I
Example
@NotThreadSafe
class DataRace {
int a, b;
int x, y;
void thread1() {
y = a;
b = 1;
}
void thread2() {
x = b;
a = 2;
}
}
y == 2, x == 1 ???
How can this happen?
• Processor can reorder statements (out-of-order execution,
HT)
• Lazy synchronization between caches and main memory,
• Compiler can reorder statements (or keep values is registers),
• Aggressive optimizations in JIT,
Example
@NotThreadSafe
class DataRace {
int a, b;
int x, y;
void thread1() {
y = a;
b = 1;
}
void thread2() {
x = b;
a = 2;
}
}
time
Thread 1 Thread 2
y = a;
b = 1;
x = b;
a = 2;
Example
@NotThreadSafe
class DataRace {
int a, b;
int x, y;
void thread1() {
y = a;
b = 1;
}
void thread2() {
x = b;
a = 2;
}
}
time
Thread 1 Thread 2
b = 1;
y = a;
x = b;
a = 2;
Example
@NotThreadSafe
class DataRace {
int a, b;
int x, y;
void thread1() {
y = a;
b = 1;
}
void thread2() {
x = b;
a = 2;
}
}
time
Thread 1 Thread 2
b = 1;
y = a;
a = 2;
x = b;
Example
@NotThreadSafe
class DataRace {
int a, b;
int x, y;
void thread1() {
y = a;
b = 1;
}
void thread2() {
x = b;
a = 2;
}
}
time
Thread 1 Thread 2
b = 1;
a = 2;
x = b;
y = a;
y == 2, x == 1
Example of x86/x64 test results
Test using jstress
@JCStressTest
@Description("Data race")
@Outcome(id = {"0, 0", "0, 1", "2, 0"}, expect = ACCEPTABLE,
desc = "Trivial under sequential consistency")
@Outcome(id = {"2, 1"}, expect = ACCEPTABLE, desc = "Racy read of x")
@State
public class DataRace {
int a, b;
int x, y;
@Actor
void thread1(IntResult2 r) {
y = a;
b = 1;
r.r1 = y;
}
@Actor
void thread2(IntResult2 r) {
x = b;
a = 2;
r.r2 = x;
}
}
jcstress: http://bit.ly/2daSL5Q
Example of x86/x64 test results
R-R R-W
W-R W-W
Test results interpretation
y==0, x==0
y==0, x==1
y==2, x==0
time
.
.
.
y = a;
b = 1;
.
.
.
x = b;
a = 2;
Test results interpretation
y==0, x==0
y==0, x==1
y==2, x==0
time
.
.
.
y = a;
b = 1;
.
.
.
x = b;
a = 2;
Test results interpretation
y==0, x==0
y==0, x==1
y==2, x==0
time
.
.
.
y = a;
b = 1;
.
.
.
x = b;
a = 2;
Visibility between threads
@ThreadSafe
public class DataRace {
int a, b;
int x, y;
void thread1() {
synchronized (this) {
y = a;
b = 1;
}
}
void thread2() {
synchronized (this) {
x = b;
a = 2;
}
}
}
Visibility between threads
time
Thread 1 Thread 2
(Th2 starts after Th1)
Program
order
Program
order
synchronization
order
Every operation that
happens before
an unlock (release)
Is visible to an operation that
happens after
a later lock (aquire)happens-before
order
@ThreadSafe
public class DataRace {
int a, b;
int x, y;
void thread1() {
synchronized (this) {
y = a;
b = 1;
}
}
void thread2() {
synchronized (this) {
x = b;
a = 2;
}
}
}
.
.
.
<enter this>
y = a;
b = 1;
<exit this>
<enter this>
x = b;
a = 2;
<exit this>
.
.
.
Possible results:
y==0, x == 1
y==2, x == 0
Synchronization
High level
• java.util.concurrent
Low level
• synchronized() blocks and methods,
• java.util.concurrent.locks
Low level primitives
• volatile variables
• java.util.concurrent.atomic
Volatile
@ThreadUnsafe
public class Looper {
static boolean done;
public static void main(String[] args)
throws InterruptedException {
new Thread(new Runnable() {
@Override
public void run() {
int count = 0;
while (!done) {
count++;
}
System.out.println("Ending this task");
}
}).start();
Thread.sleep(1000);
System.out.println("Waiting done");
done = true;
}
}
Volatile
@ThreadSafe
public class Looper {
volatile static boolean done;
public static void main(String[] args)
throws InterruptedException {
new Thread(new Runnable() {
@Override
public void run() {
int count = 0;
while (!done) {
count++;
}
System.out.println("Ending this task");
}
}).start();
Thread.sleep(1000);
System.out.println("Waiting done");
done = true;
}
}
Program
order
Program
order
synchronization
order
Thread 1
time
Thread 2
.
.
.
done = true;
while (!done)
.
.
.
happens-before
order
More about volatile
• Volatile reads are very cheep (no locks compared to
synchronized)
• Volatile increment is not atomic (!!!)
• Elements in volatile collection are not volatile (e.g. volatile
int[])
• Consider using java.util.concurrent
What operations in Java are atomic?
• Read/write on variables of primitive types (except of long
and double – Word Tearing problem),
• Read/write on volatile variables of primitive type (including
long and double),
• All read/writes to references are always atomic
(http://bit.ly/2c8kn8i),
• All operations on java.util.concurrent.atomic types,
Examples
Be careful what you’re doing...
Double-checked locking
@ThreadSafe
public class DoubleCheckedLocking {
private volatile Helper helper = null;
public Helper getHelper() {
if (helper == null) {
synchronized (this) {
if (helper == null)
helper = new Helper();
}
}
return helper;
}
}
The "Double-Checked Locking is Broken" Declaration: http://bit.ly/2cIDBnA
Final
@ThreadUnsafe
class UnsafePublication {
private int a;
private static UnsafePublication instance;
private UnsafePublication() {
a = 1;
}
void thread1() throws InterruptedException {
instance = new UnsafePublication();
}
void thread2() {
if (instance != null) {
System.out.println(instance.a);
}
}
}
What state
can thread 2 see???
null, 0, 1
Final
@ThreadSafe
class SafePublication {
private final int a;
private static SafePublication instance;
private SafePublication() {
a = 1;
}
void thread1() throws InterruptedException {
instance = new SafePublication();
}
void thread2() {
if (instance != null) {
System.out.println(instance.a);
}
}
}
Next-JMM
• JEP 188,
• Improve formalization,
• JVM coverage,
• Extend scope,
• Testing support,
• Tool support,
• Enh: atomic r/w for long and double,
To sum up...
• Concurrent programming isn’t easy,
• Design your code for concurrency (make it right before you
make it fast),
• Do not code against the implementation. Code against the
specification,
• Use high level synchronization wherever possible,
• Watch out for useless synchronization,
• Use Thread Safe Immutable objects,
Further reading
• Aleksey Shipilëv: One Stop Page
(http://bit.ly/2cqBt4x),
• Rafael Winterhalter: The Java Memory Model for
Practitioners (http://bit.ly/2cMXklJ),
• Brian Goetz: Java Concurrency in Practice
(http://amzn.to/2cloe76)
Thank you!

Contenu connexe

Tendances

Basics of Java Concurrency
Basics of Java ConcurrencyBasics of Java Concurrency
Basics of Java Concurrencykshanth2101
 
Advanced Introduction to Java Multi-Threading - Full (chok)
Advanced Introduction to Java Multi-Threading - Full (chok)Advanced Introduction to Java Multi-Threading - Full (chok)
Advanced Introduction to Java Multi-Threading - Full (chok)choksheak
 
Java Performance Tuning
Java Performance TuningJava Performance Tuning
Java Performance TuningMinh Hoang
 
Programming Language Memory Models: What do Shared Variables Mean?
Programming Language Memory Models: What do Shared Variables Mean?Programming Language Memory Models: What do Shared Variables Mean?
Programming Language Memory Models: What do Shared Variables Mean?greenwop
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaRaghu nath
 
Jvm profiling under the hood
Jvm profiling under the hoodJvm profiling under the hood
Jvm profiling under the hoodRichardWarburton
 
Java Tutorial | My Heart
Java Tutorial | My HeartJava Tutorial | My Heart
Java Tutorial | My HeartBui Kiet
 
Дмитрий Копляров , Потокобезопасные сигналы в C++
Дмитрий Копляров , Потокобезопасные сигналы в C++Дмитрий Копляров , Потокобезопасные сигналы в C++
Дмитрий Копляров , Потокобезопасные сигналы в C++Sergey Platonov
 
Inter thread communication &amp; runnable interface
Inter thread communication &amp; runnable interfaceInter thread communication &amp; runnable interface
Inter thread communication &amp; runnable interfacekeval_thummar
 
Why GC is eating all my CPU?
Why GC is eating all my CPU?Why GC is eating all my CPU?
Why GC is eating all my CPU?Roman Elizarov
 
Medical Image Processing Strategies for multi-core CPUs
Medical Image Processing Strategies for multi-core CPUsMedical Image Processing Strategies for multi-core CPUs
Medical Image Processing Strategies for multi-core CPUsDaniel Blezek
 
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia "What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia Vladimir Ivanov
 
Synchronization problem with threads
Synchronization problem with threadsSynchronization problem with threads
Synchronization problem with threadsSyed Zaid Irshad
 
Objective-C Blocks and Grand Central Dispatch
Objective-C Blocks and Grand Central DispatchObjective-C Blocks and Grand Central Dispatch
Objective-C Blocks and Grand Central DispatchMatteo Battaglio
 
JCConf 2020 - New Java Features Released in 2020
JCConf 2020 - New Java Features Released in 2020JCConf 2020 - New Java Features Released in 2020
JCConf 2020 - New Java Features Released in 2020Joseph Kuo
 

Tendances (20)

Basics of Java Concurrency
Basics of Java ConcurrencyBasics of Java Concurrency
Basics of Java Concurrency
 
Advanced Introduction to Java Multi-Threading - Full (chok)
Advanced Introduction to Java Multi-Threading - Full (chok)Advanced Introduction to Java Multi-Threading - Full (chok)
Advanced Introduction to Java Multi-Threading - Full (chok)
 
Java Performance Tuning
Java Performance TuningJava Performance Tuning
Java Performance Tuning
 
Java Performance Tuning
Java Performance TuningJava Performance Tuning
Java Performance Tuning
 
Programming Language Memory Models: What do Shared Variables Mean?
Programming Language Memory Models: What do Shared Variables Mean?Programming Language Memory Models: What do Shared Variables Mean?
Programming Language Memory Models: What do Shared Variables Mean?
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Java Tut1
Java Tut1Java Tut1
Java Tut1
 
Javatut1
Javatut1 Javatut1
Javatut1
 
Jvm profiling under the hood
Jvm profiling under the hoodJvm profiling under the hood
Jvm profiling under the hood
 
Java Tutorial | My Heart
Java Tutorial | My HeartJava Tutorial | My Heart
Java Tutorial | My Heart
 
Дмитрий Копляров , Потокобезопасные сигналы в C++
Дмитрий Копляров , Потокобезопасные сигналы в C++Дмитрий Копляров , Потокобезопасные сигналы в C++
Дмитрий Копляров , Потокобезопасные сигналы в C++
 
Free FreeRTOS Course-Task Management
Free FreeRTOS Course-Task ManagementFree FreeRTOS Course-Task Management
Free FreeRTOS Course-Task Management
 
Inter thread communication &amp; runnable interface
Inter thread communication &amp; runnable interfaceInter thread communication &amp; runnable interface
Inter thread communication &amp; runnable interface
 
Why GC is eating all my CPU?
Why GC is eating all my CPU?Why GC is eating all my CPU?
Why GC is eating all my CPU?
 
Medical Image Processing Strategies for multi-core CPUs
Medical Image Processing Strategies for multi-core CPUsMedical Image Processing Strategies for multi-core CPUs
Medical Image Processing Strategies for multi-core CPUs
 
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia "What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia
 
Synchronization problem with threads
Synchronization problem with threadsSynchronization problem with threads
Synchronization problem with threads
 
Objective-C Blocks and Grand Central Dispatch
Objective-C Blocks and Grand Central DispatchObjective-C Blocks and Grand Central Dispatch
Objective-C Blocks and Grand Central Dispatch
 
JCConf 2020 - New Java Features Released in 2020
JCConf 2020 - New Java Features Released in 2020JCConf 2020 - New Java Features Released in 2020
JCConf 2020 - New Java Features Released in 2020
 

En vedette

Java gc
Java gcJava gc
Java gcNiit
 
Java GC - Pause tuning
Java GC - Pause tuningJava GC - Pause tuning
Java GC - Pause tuningekino
 
Вячеслав Блинов «Java Garbage Collection: A Performance Impact»
Вячеслав Блинов «Java Garbage Collection: A Performance Impact»Вячеслав Блинов «Java Garbage Collection: A Performance Impact»
Вячеслав Блинов «Java Garbage Collection: A Performance Impact»Anna Shymchenko
 
Java Garbage Collection(GC)- Study
Java Garbage Collection(GC)- StudyJava Garbage Collection(GC)- Study
Java Garbage Collection(GC)- StudyDhanu Gupta
 
Java Memory Consistency Model - concepts and context
Java Memory Consistency Model - concepts and contextJava Memory Consistency Model - concepts and context
Java Memory Consistency Model - concepts and contextTomek Borek
 
Java gc and JVM optimization
Java gc  and JVM optimizationJava gc  and JVM optimization
Java gc and JVM optimizationRajan Jethva
 
What you need to know about GC
What you need to know about GCWhat you need to know about GC
What you need to know about GCKelum Senanayake
 
HSA Memory Model Hot Chips 2013
HSA Memory Model Hot Chips 2013HSA Memory Model Hot Chips 2013
HSA Memory Model Hot Chips 2013HSA Foundation
 
Tuning Java GC to resolve performance issues
Tuning Java GC to resolve performance issuesTuning Java GC to resolve performance issues
Tuning Java GC to resolve performance issuesSergey Podolsky
 
GC Tuning in the HotSpot Java VM - a FISL 10 Presentation
GC Tuning in the HotSpot Java VM - a FISL 10 PresentationGC Tuning in the HotSpot Java VM - a FISL 10 Presentation
GC Tuning in the HotSpot Java VM - a FISL 10 PresentationLudovic Poitou
 
淺談 Java GC 原理、調教和 新發展
淺談 Java GC 原理、調教和新發展淺談 Java GC 原理、調教和新發展
淺談 Java GC 原理、調教和 新發展Leon Chen
 
Let's Learn to Talk to GC Logs in Java 9
Let's Learn to Talk to GC Logs in Java 9Let's Learn to Talk to GC Logs in Java 9
Let's Learn to Talk to GC Logs in Java 9Poonam Bajaj Parhar
 

En vedette (20)

[BGOUG] Java GC - Friend or Foe
[BGOUG] Java GC - Friend or Foe[BGOUG] Java GC - Friend or Foe
[BGOUG] Java GC - Friend or Foe
 
Java gc
Java gcJava gc
Java gc
 
Java GC - Pause tuning
Java GC - Pause tuningJava GC - Pause tuning
Java GC - Pause tuning
 
Вячеслав Блинов «Java Garbage Collection: A Performance Impact»
Вячеслав Блинов «Java Garbage Collection: A Performance Impact»Вячеслав Блинов «Java Garbage Collection: A Performance Impact»
Вячеслав Блинов «Java Garbage Collection: A Performance Impact»
 
Java Garbage Collection(GC)- Study
Java Garbage Collection(GC)- StudyJava Garbage Collection(GC)- Study
Java Garbage Collection(GC)- Study
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
Java Memory Model
Java Memory ModelJava Memory Model
Java Memory Model
 
Java Memory Consistency Model - concepts and context
Java Memory Consistency Model - concepts and contextJava Memory Consistency Model - concepts and context
Java Memory Consistency Model - concepts and context
 
Java gc and JVM optimization
Java gc  and JVM optimizationJava gc  and JVM optimization
Java gc and JVM optimization
 
What you need to know about GC
What you need to know about GCWhat you need to know about GC
What you need to know about GC
 
HSA Memory Model Hot Chips 2013
HSA Memory Model Hot Chips 2013HSA Memory Model Hot Chips 2013
HSA Memory Model Hot Chips 2013
 
Java GC, Off-heap workshop
Java GC, Off-heap workshopJava GC, Off-heap workshop
Java GC, Off-heap workshop
 
How long can you afford to Stop The World?
How long can you afford to Stop The World?How long can you afford to Stop The World?
How long can you afford to Stop The World?
 
JVM及其调优
JVM及其调优JVM及其调优
JVM及其调优
 
Tuning Java GC to resolve performance issues
Tuning Java GC to resolve performance issuesTuning Java GC to resolve performance issues
Tuning Java GC to resolve performance issues
 
GC Tuning in the HotSpot Java VM - a FISL 10 Presentation
GC Tuning in the HotSpot Java VM - a FISL 10 PresentationGC Tuning in the HotSpot Java VM - a FISL 10 Presentation
GC Tuning in the HotSpot Java VM - a FISL 10 Presentation
 
Memory models
Memory modelsMemory models
Memory models
 
淺談 Java GC 原理、調教和 新發展
淺談 Java GC 原理、調教和新發展淺談 Java GC 原理、調教和新發展
淺談 Java GC 原理、調教和 新發展
 
Java GC
Java GCJava GC
Java GC
 
Let's Learn to Talk to GC Logs in Java 9
Let's Learn to Talk to GC Logs in Java 9Let's Learn to Talk to GC Logs in Java 9
Let's Learn to Talk to GC Logs in Java 9
 

Similaire à Java Memory Model

Finding Xori: Malware Analysis Triage with Automated Disassembly
Finding Xori: Malware Analysis Triage with Automated DisassemblyFinding Xori: Malware Analysis Triage with Automated Disassembly
Finding Xori: Malware Analysis Triage with Automated DisassemblyPriyanka Aash
 
Introduction to Real Time Java
Introduction to Real Time JavaIntroduction to Real Time Java
Introduction to Real Time JavaDeniz Oguz
 
Week1 Electronic System-level ESL Design and SystemC Begin
Week1 Electronic System-level ESL Design and SystemC BeginWeek1 Electronic System-level ESL Design and SystemC Begin
Week1 Electronic System-level ESL Design and SystemC Begin敬倫 林
 
Microservices with Micronaut
Microservices with MicronautMicroservices with Micronaut
Microservices with MicronautQAware GmbH
 
Hs java open_party
Hs java open_partyHs java open_party
Hs java open_partyOpen Party
 
Quantifying Container Runtime Performance: OSCON 2017 Open Container Day
Quantifying Container Runtime Performance: OSCON 2017 Open Container DayQuantifying Container Runtime Performance: OSCON 2017 Open Container Day
Quantifying Container Runtime Performance: OSCON 2017 Open Container DayPhil Estes
 
Software Profiling: Java Performance, Profiling and Flamegraphs
Software Profiling: Java Performance, Profiling and FlamegraphsSoftware Profiling: Java Performance, Profiling and Flamegraphs
Software Profiling: Java Performance, Profiling and FlamegraphsIsuru Perera
 
Threading Successes 03 Gamebryo
Threading Successes 03   GamebryoThreading Successes 03   Gamebryo
Threading Successes 03 Gamebryoguest40fc7cd
 
Super scaling singleton inserts
Super scaling singleton insertsSuper scaling singleton inserts
Super scaling singleton insertsChris Adkin
 
Deep Dive on Delivering Amazon EC2 Instance Performance
Deep Dive on Delivering Amazon EC2 Instance PerformanceDeep Dive on Delivering Amazon EC2 Instance Performance
Deep Dive on Delivering Amazon EC2 Instance PerformanceAmazon Web Services
 
Microservices with Micronaut
Microservices with MicronautMicroservices with Micronaut
Microservices with MicronautQAware GmbH
 
Hadoop cluster performance profiler
Hadoop cluster performance profilerHadoop cluster performance profiler
Hadoop cluster performance profilerIhor Bobak
 
CPN302 your-linux-ami-optimization-and-performance
CPN302 your-linux-ami-optimization-and-performanceCPN302 your-linux-ami-optimization-and-performance
CPN302 your-linux-ami-optimization-and-performanceCoburn Watson
 
Choosing the Right EC2 Instance and Applicable Use Cases - AWS June 2016 Webi...
Choosing the Right EC2 Instance and Applicable Use Cases - AWS June 2016 Webi...Choosing the Right EC2 Instance and Applicable Use Cases - AWS June 2016 Webi...
Choosing the Right EC2 Instance and Applicable Use Cases - AWS June 2016 Webi...Amazon Web Services
 
H2O Design and Infrastructure with Matt Dowle
H2O Design and Infrastructure with Matt DowleH2O Design and Infrastructure with Matt Dowle
H2O Design and Infrastructure with Matt DowleSri Ambati
 
Software Profiling: Understanding Java Performance and how to profile in Java
Software Profiling: Understanding Java Performance and how to profile in JavaSoftware Profiling: Understanding Java Performance and how to profile in Java
Software Profiling: Understanding Java Performance and how to profile in JavaIsuru Perera
 
Performance and predictability (1)
Performance and predictability (1)Performance and predictability (1)
Performance and predictability (1)RichardWarburton
 
Performance and Predictability - Richard Warburton
Performance and Predictability - Richard WarburtonPerformance and Predictability - Richard Warburton
Performance and Predictability - Richard WarburtonJAXLondon2014
 

Similaire à Java Memory Model (20)

Finding Xori: Malware Analysis Triage with Automated Disassembly
Finding Xori: Malware Analysis Triage with Automated DisassemblyFinding Xori: Malware Analysis Triage with Automated Disassembly
Finding Xori: Malware Analysis Triage with Automated Disassembly
 
Introduction to Real Time Java
Introduction to Real Time JavaIntroduction to Real Time Java
Introduction to Real Time Java
 
Week1 Electronic System-level ESL Design and SystemC Begin
Week1 Electronic System-level ESL Design and SystemC BeginWeek1 Electronic System-level ESL Design and SystemC Begin
Week1 Electronic System-level ESL Design and SystemC Begin
 
Microservices with Micronaut
Microservices with MicronautMicroservices with Micronaut
Microservices with Micronaut
 
Hs java open_party
Hs java open_partyHs java open_party
Hs java open_party
 
Quantifying Container Runtime Performance: OSCON 2017 Open Container Day
Quantifying Container Runtime Performance: OSCON 2017 Open Container DayQuantifying Container Runtime Performance: OSCON 2017 Open Container Day
Quantifying Container Runtime Performance: OSCON 2017 Open Container Day
 
Software Profiling: Java Performance, Profiling and Flamegraphs
Software Profiling: Java Performance, Profiling and FlamegraphsSoftware Profiling: Java Performance, Profiling and Flamegraphs
Software Profiling: Java Performance, Profiling and Flamegraphs
 
Threading Successes 03 Gamebryo
Threading Successes 03   GamebryoThreading Successes 03   Gamebryo
Threading Successes 03 Gamebryo
 
Super scaling singleton inserts
Super scaling singleton insertsSuper scaling singleton inserts
Super scaling singleton inserts
 
Deep Dive on Delivering Amazon EC2 Instance Performance
Deep Dive on Delivering Amazon EC2 Instance PerformanceDeep Dive on Delivering Amazon EC2 Instance Performance
Deep Dive on Delivering Amazon EC2 Instance Performance
 
Memory model
Memory modelMemory model
Memory model
 
Microservices with Micronaut
Microservices with MicronautMicroservices with Micronaut
Microservices with Micronaut
 
Hadoop cluster performance profiler
Hadoop cluster performance profilerHadoop cluster performance profiler
Hadoop cluster performance profiler
 
CPN302 your-linux-ami-optimization-and-performance
CPN302 your-linux-ami-optimization-and-performanceCPN302 your-linux-ami-optimization-and-performance
CPN302 your-linux-ami-optimization-and-performance
 
Choosing the Right EC2 Instance and Applicable Use Cases - AWS June 2016 Webi...
Choosing the Right EC2 Instance and Applicable Use Cases - AWS June 2016 Webi...Choosing the Right EC2 Instance and Applicable Use Cases - AWS June 2016 Webi...
Choosing the Right EC2 Instance and Applicable Use Cases - AWS June 2016 Webi...
 
H2O Design and Infrastructure with Matt Dowle
H2O Design and Infrastructure with Matt DowleH2O Design and Infrastructure with Matt Dowle
H2O Design and Infrastructure with Matt Dowle
 
Software Profiling: Understanding Java Performance and how to profile in Java
Software Profiling: Understanding Java Performance and how to profile in JavaSoftware Profiling: Understanding Java Performance and how to profile in Java
Software Profiling: Understanding Java Performance and how to profile in Java
 
Java On CRaC
Java On CRaCJava On CRaC
Java On CRaC
 
Performance and predictability (1)
Performance and predictability (1)Performance and predictability (1)
Performance and predictability (1)
 
Performance and Predictability - Richard Warburton
Performance and Predictability - Richard WarburtonPerformance and Predictability - Richard Warburton
Performance and Predictability - Richard Warburton
 

Dernier

04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdfChristopherTHyatt
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 

Dernier (20)

04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 

Java Memory Model

  • 1. JMM Java Memory Model Łukasz Koniecki 24/10/2016
  • 3. Goal • Familiarize with the JMM, • How processor works? • Recall how Java compiler and JVM work, • JIT in action, • Explain what is a data race and a correctly synchronized program, • Talk about synchronization and atomicity, • Based on examples... • Next-gen JMM...
  • 7. Dummy program public class Example { int i, j; public void myDummyMethod() { i+=1; j+=1; i+=1; ... } }
  • 8. RAM i = 0 j = 0 Cache Program execution System Bus public class Example { int i, j; public void myDummyMethod() { i+=1; j+=1; i+=1; ... } } The Java Memory Model for Practitioners: http://bit.ly/2cMXklJ
  • 9. RAM i = 0 j = 0 Cache Program execution System Bus i = 0 public class Example { int i, j; public void myDummyMethod() { i+=1; j+=1; i+=1; ... } }
  • 10. RAM i = 0 j = 0 Cache Program execution System Bus i = 1 public class Example { int i, j; public void myDummyMethod() { i+=1; j+=1; i+=1; ... } }
  • 11. RAM i = 0 j = 0 Cache Program execution System Bus i = 1 public class Example { int i, j; public void myDummyMethod() { i+=1; j+=1; i+=1; ... } }
  • 12. RAM i = 1 j = 0 Cache Program execution System Bus public class Example { int i, j; public void myDummyMethod() { i+=1; j+=1; i+=1; ... } }
  • 13. RAM i = 1 j = 0 Cache Program execution System Bus j = 0 public class Example { int i, j; public void myDummyMethod() { i+=1; j+=1; i+=1; ... } }
  • 14. RAM i = 1 j = 0 Cache Program execution System Bus j = 1 public class Example { int i, j; public void myDummyMethod() { i+=1; j+=1; i+=1; ... } }
  • 15. RAM i = 1 j = 1 Cache Program execution System Bus j = 1 public class Example { int i, j; public void myDummyMethod() { i+=1; j+=1; i+=1; ... } }
  • 16. RAM i = 1 j = 1 Cache Program execution System Bus Sequentialy consistent execution public class Example { int i, j; public void myDummyMethod() { i+=1; j+=1; i+=1; ... } }
  • 18. Our world in data: http://bit.ly/1NLxNcH Moore’s Law
  • 19. Moore’s Law Our world in data: http://bit.ly/1NLxNcH 2006
  • 20. Processor technology • ... • 22 nm – 2012 • 14 nm – 2014 • 10 nm – 2017 • 7 nm – ~2019 • 5 nm – ~2021 Wikipedia: http://bit.ly/2cMWoNg
  • 21. Processor vs. Memory Performance How L1 and L2 CPU caches work, and why they’re an essential part of modern chips: http://bit.ly/2cpHu1x
  • 25. Core i7 Xeon 5500 Series Data Source Latency (approximate) local L1 CACHE hit, ~4 cycles ( 2.1 - 1.2 ns ) local L2 CACHE hit, ~10 cycles ( 5.3 - 3.0 ns ) local L3 CACHE hit, line unshared ~40 cycles ( 21.4 - 12.0 ns ) local L3 CACHE hit, shared line in another core ~65 cycles ( 34.8 - 19.5 ns ) local L3 CACHE hit, modified in another core ~75 cycles ( 40.2 - 22.5 ns ) remote L3 CACHE (Ref: Fig.1 [Pg. 5]) ~100-300 cycles ( 160.7 - 30.0 ns ) local DRAM ~60 ns remote DRAM ~100 ns Performance Analysis Guide for Intel® Core™ i7 Processor and Intel® Xeon™ 5500 processors: http://intel.ly/2cV1ZFZ Cache latency
  • 26. Weak vs. Strong hardware Memory Models Weak vs. Strong Memory Models: http://bit.ly/2cC4avk
  • 27. x86/x64 processor memory model R-R R-W W-R W-W Intel® 64 and IA-32 Architectures Software Developer’s Manual: http://intel.ly/2csMyB2 Processor P can read B before it’s write to A is seen by all processors (processor can move its own reads in front of its own writes)
  • 28. x86/x64 processor memory model R-R R-W W-R W-W Intel® 64 and IA-32 Architectures Software Developer’s Manual: http://intel.ly/2csMyB2 Processor P can read B before it’s write to A is seen by all processors (processor can move its own reads in front of its own writes)
  • 29. How Java compiler works? javac Source code Byte code Bytecode verifier Class loader JIT JVM OS Native code Byte code
  • 30. JIT •Profile guided, •Speculatively optimizing, •Backup strategies, •Optimizes code for us, •We don’t have to care so much about cache-wise operations
  • 32. Tiered compilation (interpreter) time throuput startup interpreted C1 C2 sampling full speed deoptimize bail to interpreter Interpreter • extremly slow, • not profiling
  • 33. Tiered compilation (C1 compiler) time throuput startup interpreted C1 C2 sampling full speed deoptimize bail to interpreter C1 • client, • fast but dummy, • does the profiling, • e.g: branches, typechecks,
  • 34. Tiered compilation (C2 compiler) time throuput startup interpreted C1 C2 sampling full speed deoptimize bail to interpreter C2 • server, • slow but clever, • aggresively optimizing, • based on profile, • e.g.: loop optimizations (unswitching, unrolling), Implicit Null Checking
  • 35. Why do we need a JMM? • Different platform memory models (none of them match the JMM!!!) • Many JVM implementations, • People don’t know how to program concurrently, • Programmers: write reliable and multithreaded code, • Compiler writers: implement optimization which will be a legal, optimization according to the JLS • Compiler: produce fast and optimal native code,
  • 36. JMM • Action: read and write to variable, lock and unlock of monitor, starting and joining with thread, • Happens-before partial order, • Thread executing action B can see the results of action A (any thread), there must be a happens-before relationship between A and B, • Otherwise JVM is free to reorder,
  • 37. Happens-before orderings • Unlock of a monitor / lock of that monitor, • Write to a volatile variable / read of that variable, • Call to start() / any action in the started thread, • All actions in a thread / any other thread successfully returns from join() on that thread, • Setting default values for variables, setting value to a final field in the constructor / constructor finish, • Write to an Atomic variable / read from that variable, • Many java.util.concurrent methods,
  • 38. JMM • A promise for programmers: sequential consistency must be sacrificed to allow optimizations, but it will still hold for data race free programs. This is the data race free (DRF) guarantee. • A promise for security: even for programs with data races, values should not appear “out of thin air”, preventing unintended information leakage. • A promise for compilers: common hardware and software optimizations should be allowed as far as possible without violating the first two requirements. Java Memory Model Examples: Good, Bad and Ugly: http://bit.ly/2cZfF1I
  • 39. Example @NotThreadSafe class DataRace { int a, b; int x, y; void thread1() { y = a; b = 1; } void thread2() { x = b; a = 2; } } y == 2, x == 1 ???
  • 40. How can this happen? • Processor can reorder statements (out-of-order execution, HT) • Lazy synchronization between caches and main memory, • Compiler can reorder statements (or keep values is registers), • Aggressive optimizations in JIT,
  • 41. Example @NotThreadSafe class DataRace { int a, b; int x, y; void thread1() { y = a; b = 1; } void thread2() { x = b; a = 2; } } time Thread 1 Thread 2 y = a; b = 1; x = b; a = 2;
  • 42. Example @NotThreadSafe class DataRace { int a, b; int x, y; void thread1() { y = a; b = 1; } void thread2() { x = b; a = 2; } } time Thread 1 Thread 2 b = 1; y = a; x = b; a = 2;
  • 43. Example @NotThreadSafe class DataRace { int a, b; int x, y; void thread1() { y = a; b = 1; } void thread2() { x = b; a = 2; } } time Thread 1 Thread 2 b = 1; y = a; a = 2; x = b;
  • 44. Example @NotThreadSafe class DataRace { int a, b; int x, y; void thread1() { y = a; b = 1; } void thread2() { x = b; a = 2; } } time Thread 1 Thread 2 b = 1; a = 2; x = b; y = a; y == 2, x == 1
  • 45. Example of x86/x64 test results
  • 46. Test using jstress @JCStressTest @Description("Data race") @Outcome(id = {"0, 0", "0, 1", "2, 0"}, expect = ACCEPTABLE, desc = "Trivial under sequential consistency") @Outcome(id = {"2, 1"}, expect = ACCEPTABLE, desc = "Racy read of x") @State public class DataRace { int a, b; int x, y; @Actor void thread1(IntResult2 r) { y = a; b = 1; r.r1 = y; } @Actor void thread2(IntResult2 r) { x = b; a = 2; r.r2 = x; } } jcstress: http://bit.ly/2daSL5Q
  • 47. Example of x86/x64 test results R-R R-W W-R W-W
  • 48. Test results interpretation y==0, x==0 y==0, x==1 y==2, x==0 time . . . y = a; b = 1; . . . x = b; a = 2;
  • 49. Test results interpretation y==0, x==0 y==0, x==1 y==2, x==0 time . . . y = a; b = 1; . . . x = b; a = 2;
  • 50. Test results interpretation y==0, x==0 y==0, x==1 y==2, x==0 time . . . y = a; b = 1; . . . x = b; a = 2;
  • 51. Visibility between threads @ThreadSafe public class DataRace { int a, b; int x, y; void thread1() { synchronized (this) { y = a; b = 1; } } void thread2() { synchronized (this) { x = b; a = 2; } } }
  • 52. Visibility between threads time Thread 1 Thread 2 (Th2 starts after Th1) Program order Program order synchronization order Every operation that happens before an unlock (release) Is visible to an operation that happens after a later lock (aquire)happens-before order @ThreadSafe public class DataRace { int a, b; int x, y; void thread1() { synchronized (this) { y = a; b = 1; } } void thread2() { synchronized (this) { x = b; a = 2; } } } . . . <enter this> y = a; b = 1; <exit this> <enter this> x = b; a = 2; <exit this> . . . Possible results: y==0, x == 1 y==2, x == 0
  • 53. Synchronization High level • java.util.concurrent Low level • synchronized() blocks and methods, • java.util.concurrent.locks Low level primitives • volatile variables • java.util.concurrent.atomic
  • 54. Volatile @ThreadUnsafe public class Looper { static boolean done; public static void main(String[] args) throws InterruptedException { new Thread(new Runnable() { @Override public void run() { int count = 0; while (!done) { count++; } System.out.println("Ending this task"); } }).start(); Thread.sleep(1000); System.out.println("Waiting done"); done = true; } }
  • 55. Volatile @ThreadSafe public class Looper { volatile static boolean done; public static void main(String[] args) throws InterruptedException { new Thread(new Runnable() { @Override public void run() { int count = 0; while (!done) { count++; } System.out.println("Ending this task"); } }).start(); Thread.sleep(1000); System.out.println("Waiting done"); done = true; } } Program order Program order synchronization order Thread 1 time Thread 2 . . . done = true; while (!done) . . . happens-before order
  • 56. More about volatile • Volatile reads are very cheep (no locks compared to synchronized) • Volatile increment is not atomic (!!!) • Elements in volatile collection are not volatile (e.g. volatile int[]) • Consider using java.util.concurrent
  • 57. What operations in Java are atomic? • Read/write on variables of primitive types (except of long and double – Word Tearing problem), • Read/write on volatile variables of primitive type (including long and double), • All read/writes to references are always atomic (http://bit.ly/2c8kn8i), • All operations on java.util.concurrent.atomic types,
  • 58. Examples Be careful what you’re doing...
  • 59. Double-checked locking @ThreadSafe public class DoubleCheckedLocking { private volatile Helper helper = null; public Helper getHelper() { if (helper == null) { synchronized (this) { if (helper == null) helper = new Helper(); } } return helper; } } The "Double-Checked Locking is Broken" Declaration: http://bit.ly/2cIDBnA
  • 60. Final @ThreadUnsafe class UnsafePublication { private int a; private static UnsafePublication instance; private UnsafePublication() { a = 1; } void thread1() throws InterruptedException { instance = new UnsafePublication(); } void thread2() { if (instance != null) { System.out.println(instance.a); } } } What state can thread 2 see??? null, 0, 1
  • 61. Final @ThreadSafe class SafePublication { private final int a; private static SafePublication instance; private SafePublication() { a = 1; } void thread1() throws InterruptedException { instance = new SafePublication(); } void thread2() { if (instance != null) { System.out.println(instance.a); } } }
  • 62. Next-JMM • JEP 188, • Improve formalization, • JVM coverage, • Extend scope, • Testing support, • Tool support, • Enh: atomic r/w for long and double,
  • 63. To sum up... • Concurrent programming isn’t easy, • Design your code for concurrency (make it right before you make it fast), • Do not code against the implementation. Code against the specification, • Use high level synchronization wherever possible, • Watch out for useless synchronization, • Use Thread Safe Immutable objects,
  • 64. Further reading • Aleksey Shipilëv: One Stop Page (http://bit.ly/2cqBt4x), • Rafael Winterhalter: The Java Memory Model for Practitioners (http://bit.ly/2cMXklJ), • Brian Goetz: Java Concurrency in Practice (http://amzn.to/2cloe76)