SlideShare a Scribd company logo
Java Concurrency, A(nother) Peek Under the Hood [JavaOne 2016 CON1497]
Java Concurrency, A(nother) Peek Under the Hood [JavaOne 2016 CON1497]
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Java Concurrency
A(nother) Peek Under the Hood
David Buck
Principal Member of Technical Staff
Java SE Sustaining Engineering
September, 2016
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Safe Harbor Statement
The following is intended to outline our general product direction. It is intended for
information purposes only, and may not be incorporated into any contract. It is not a
commitment to deliver any material, code, or functionality, and should not be relied upon
in making purchasing decisions. The development, release, and timing of any features or
functionality described for Oracle’s products remains at the sole discretion of Oracle.
4
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
About Me
David Buck
• Java SE Sustaining Engineering
• Mostly JRockit fixes
• OpenJDK 8 Updates
Project Maintainer
• Hobbies: Programming
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Program Agenda
Introduction
Background
Tangent 1: Special Relativity
History
Implementation
Tangent 2: HSDIS
Future
Conclusions
1
2
3
4
5
6
6
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Introduction
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Motivation
“But I don’t write multithreaded code…”
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Web Server
picture: Coolcaesar at the English language Wikipedia [GFDL (http://www.gnu.org/copyleft/fdl.html) or CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/)]
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
GUI
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Libraries
picture: David Vignoni / ICON KING (http://icon-king.com) [LGPL (http://www.gnu.org/licenses/lgpl.html) or LGPL (http://www.gnu.org/licenses/lgpl.html)]
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Batch Processing
picture: US Social Security Administration (http://www.ssa.gov/history/acalcs.html) [Public domain]
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
“But I don’t write multithreaded code…”
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
“But I don’t write multithreaded code…”
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Race Conditions
picture: Sakurambo at English Wikipedia [GFDL (http://www.gnu.org/copyleft/fdl.html) or CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/)]
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Heisenbugs
Bug Heisenberg
Heisenbug
picture: Bundesarchiv, Bild 183-R57262 / Unknown / CC-BY-SA 3.0 [CC BY-SA 3.0 de (http://creativecommons.org/licenses/by-sa/3.0/de/deed.en)]
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Observer Effect
picture: Christian Schirm (Own work) [CC0]
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Avoiding Heisenbugs
• Java Memory Model
• synchronized keyword
• java.util.concurrent
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Avoiding Heisenbugs
• Java Memory Model
• synchronized keyword
• java.util.concurrent
• Don’t do XXX
• You must do YYY
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
a² + b² == c²
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |picture: WTF Public License, Version 2
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Background
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Memory Model
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Memory Model
Definition 1
Specification that explains when we can guarantee that a written value may
be read correctly
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
void hogeMethod1() {
int localA = 42;
assert localA == 42;
}
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
static int staticA;
void hogeMethod2() {
staticA = 42;
assert staticA == 42;
}
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
int data = 0;
boolean ready = false;
void hoge3() {
while (!ready) {};
assert data == 42;
}
void hoge4() {
data = 42;
ready = true;
}
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Culprit #1
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Culprit #2
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Memory Ordering
void hoge5() {
a = 1;
b = 2;
c = 3;
d = 4;
e = 5;
a = a + 1;
}
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Memory Ordering
void hoge5() {
a = 1;
b = 2;
c = 3;
d = 4;
e = 5;
a = a + 1;
}
void hoge5() {
a = 2;
b = 2;
c = 3;
d = 4;
e = 5;
}
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Memory Ordering
void hoge5() {
a = 1;
b = 2;
c = 3;
d = 4;
e = 5;
a = a + 1;
}
void hoge5() {
b = 2;
c = 3;
d = 4;
e = 5;
a = 2;
}
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Rule
Single threaded behavior must never change
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Out of Order Execution
picture: Amit6, original version (File:Superscalarpipeline.png) by User:Poil (Own work) [CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0)]
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
CPU Differences
Type Alpha ARMv7
PA-
RISC
POWER
SPARC
RMO
SPARC
PSO
SPARC
TSO
x86
x86
oostore
AMD64 IA-64 zSeries
load-
load
Y Y Y Y Y Y Y
load-
store
Y Y Y Y Y Y Y
store-
store
Y Y Y Y Y Y Y Y
store-
load
Y Y Y Y Y Y Y Y Y Y Y Y
Atomic
(loads)
Y Y Y Y Y
Atomic
(stores)
Y Y Y Y Y Y
Depend
ent
loads
Y
instruct
ion
cache
Y Y Y Y Y Y Y Y Y Y
chart source: https://en.wikipedia.org/wiki/Memory_ordering
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
CPU Differences
Loose Strict
Alpha
X86
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
CPU Differences
Loose Strict
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Memory Barriers
int data = 0;
boolean ready = false;
void hoge3() {
while (!ready) {};
assert data == 42;
}
void hoge4() {
data = 42;
ready = true;
}
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Memory Barriers
int data = 0;
boolean ready = false;
void hoge3() {
while (!ready) {};
assert data == 42;
}
void hoge4() {
data = 42;
ready = true;
}
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Memory Barriers
int data = 0;
boolean ready = false;
void hoge3() {
while (!ready) {};
assert data == 42;
}
void hoge4() {
data = 42;
ready = true;
}
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Memory Barriers Types
• store-store
• store-load
• load-store
• load-load
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Compiler Barriers
• GCC
– __asm__ __volatile__("":::"memory");
• VC++
– _ReadBarrier
– _WriteBarrier
– _ReadWriteBarrier
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Tangent 1: Special Relativity
picture: Sakurambo (Own work) [Public domain]
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Tangent 1: Special Relativity
A
B
C
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Tangent 1: Special Relativity
A
B
C
A, B, C
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Tangent 1: Special Relativity
A
B
C
A, B, C C, B, A
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Tangent 1: Special Relativity
Observed order depends on viewer's frame of reference
A
B
C
A, B, C C, B, A
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
No single “correct” timeline
• Special Relativity
– Observed order of events depends on frame of reference
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
No single “correct” timeline
• Special Relativity
– Observed order of events depends on frame of reference
• Multithreaded Code
– Observed order of events depends on thread
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Memory Model
Definition 2
• Clarify what multithreaded behavior developers may depend on
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Memory Model
Definition 2
• Clarify what multithreaded behavior developers may depend on
• Limits what types of optimizations the runtime may do
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
History
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Java Memory Model (JMM)
• Write once, run anywhere
• 1995
• Part of the formal language
specification
• Happened-before
picture: Peter Campbell [GFDL (http://www.gnu.org/copyleft/fdl.html) or CC BY-SA 4.0-3.0-2.5-2.0-1.0 (http://creativecommons.org/licenses/by-sa/4.0-3.0-2.5-2.0-1.0)]
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
happened-before
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Leslie Lamport
• LaTex
• happened-before
picture: Leslie Lamport [GFDL (http://en.wikipedia.org/wiki/GFDL]
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
happened-before
void hoge5() {
a = 1;
b = 2;
c = 3;
d = 4;
e = 5;
a = a + 1;
}
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
happened-before
void hoge4() {
data = 42;
ready = true;
}
void hoge3() {
while (!ready) {};
assert data == 42;
}
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
happened-before
void hoge4() {
data = 42;
ready = true;
}
void hoge3() {
while (!ready) {};
assert data == 42;
}
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
happened-before
void synchronized hoge4()
{
data = 42;
ready = true;
}
void synchronized hoge3()
{
while (!ready) {};
assert data == 42;
}
Warning: if hoge3 is executed first, the above code will deadlock
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
happened-before
void synchronized hoge4()
{
data = 42;
ready = true;
}
void synchronized hoge3()
{
while (!ready) {};
assert data == 42;
}
Warning: if hoge3 is executed first, the above code will deadlock
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
JMM Specification
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Original JMM Keywords
• synchronized
– mutual exclusion
– happened-before
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Original JMM Keywords
• synchronized
– mutual exclusion
– happened-before
• volatile
– Visibility
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Problems with the Original JMM
• volatile does not establish a happened-before relationship
• final values can change
• Many important runtime optimization were not allowed
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Doug Lea
• Author of the Java Multithreaded
bible
• Introduced his own OSS high-level
multithreaded library
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Java SE 5.0
• Modern JMM introduced
• Lea’s library added to official JDK
picture: Zvi Roger [CC BY 3.0 (http://creativecommons.org/licenses/by/3.0)]
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Modern JMM
• JSR-133
• Fixed shortcomings in original JMM
– volatile does establish happens-before
– final values will never change
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Modern JMM Keywords
• synchronized
– mutual exclusion
– happened-before
• volatile
– visibility
– happened-before
• final
– immutability
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
JSR-133 の volatile
void hoge4() {
data = 42;
ready = true;
}
void hoge3() {
while (!ready)
{};
assert data ==
42;
}
Warning: if hoge3 is executed first, the above code will deadlock
volatile boolean ready = false;
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
volatile != atomic
volatile int id = 0;
int incID() {
return id++;
}
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
volatile != atomic
volatile int id = 0;
int incID() {
return id++;
}
reg = [id]
reg++
[id] = reg
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
volatile != atomic
volatile int id = 0;
synchronized int incID() {
return id++;
}
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
happened-before
• Monitor (acquisition / release)
• Volatile (read / write)
• final “freeze” (constructor)
• JNI
– System.out.println()
– Input / Output
• thread start()/join()
• operations on j.u.concurrent collections
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
java.util.concurency
• JSR-166
• Doug Lee’s OSS multithreading library
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Multithreaded Control
• java.util.concurency
• synchronized
– wait()/notify()
• volatile / final
Abstract
Low-level
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Fork/Join (JDK 7)
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Functional Programming (JDK 8)
• Lambda Forms
• Stream API
λ
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Implementation
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Tangent 2: HSDIS (HotSpot Disassembler)
• Lets us see code generated by JIT
• Requires a disassembler plugin
– GNU binutils-based plugin
– base-hsdis
• Command line options
– +PrintAssembly
– +CompileCommand=print,*MyClass.myMethod
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Demos
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
ARM Assembly Crash Course
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
ARM Assembly Crash Course
Op Target Source
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
ARM Assembly Crash Course
MOV r02 #42
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
ARM Assembly Crash Course
MOV r02 #42
r02 = 42
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
ARM Assembly Crash Course
Op Target Source
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
ARM Assembly Crash Course
Op Target Source
Exception
STR Source Target
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
ARM Assembly Crash Course
MOV
Copies registers
MOV r02 r03
Writes Literals
MOV r02 #42
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
ARM Assembly Crash Course
LDx
Loads data from memory to a register
ldr r03, [r06]
r03 = *r06
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
ARM Assembly Crash Course
CMP / BEQ
Compare and branch if equal
cmp r0, #0
beq 0x74178d08
if (r0 == 0) goto 0x74178d08
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
ARM Assembly Crash Course
DMB
Data Memory Barrier
dmb st
dmb sy
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
One more thing…
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Safe Points
• Methods proactively poll
• JVM makes page unreadable to stop world
movw ip, #45056 ; 0xb000
movt ip, #30461 ; 0x76fd
ldr ip, [ip] ; {poll_return}
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Long Value
static long val = 0;
public static void main(String[] args) {
new Thread(() -> { while(true) val = -1L; } ).start();
new Thread(() -> { while(true) val = 0L; } ).start();
while (true) {
long temp = val;
if ( temp != -1 && temp != 0 ) {
System.out.println("temp (DEC): " + temp);
System.out.println("temp (BIN): " + Long.toBinaryString(temp));
System.exit(-1);
}
}
}
}
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Object Initialization
public class ObjInit {
static ObjInit globalRef = new ObjInit();
long number;
public ObjInit() {
number = 42;
}
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Object Initialization
public static void main(String[] args) {
new Thread(()
-> { while(true) {
long temp = globalRef.number;
if (temp != 42) {
System.out.println(temp);
System.exit(-1);
}
}
} ).start();
new Thread(()
-> { while(true) globalRef = new ObjInit(); } ).start(); }}
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
public class Loop extends Thread {
static boolean done = false;
public static void main(String[] args) {
Thread childThread = new Loop();
childThread.start();
System.out.println("Starting loop");
spinWait();
System.out.println("Exited loop");
}
private static void spinWait() {
while (!done) ;
}
public void run() {
try {
Thread.sleep(5 * 1000);
} catch (Exception e) {}
done = true;
}
} // class Loop
Loop
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Future
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
JEP-188/JMM9
• Not planned for JDK 9 (JMM9 != JDK 9)
• JVM-level specification
• Compatibility with C11/C++11
• Cover new features added since JDK 5
– (e.g. AtomicX.weakCompareAndSet())
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Conclusions
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Avoid low-level API
• java.util.concurency
• synchronized
– wait()/notify()
• volatile / final
Abstract
Low-level
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Code against the specification
• Intermittent bugs are common
• Behavior changes with JRE / platform
• Stuff to avoid
– synchronized (new Object()) { }
– Randomly adding volatile declarations until it magically starts working
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Testing
• As close to production environment as possible
– Same hardware
– Same JRE (including version)
– Same settings
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Java Concurrency in Practice
• Java Concurrency Bible
• Covers JSR-133 / JSR-166
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Summary
• Avoid low-level APIs
• Code against the standard, not the implementation
• Testing is always indispensible
• Must read Java Concurrency in Practice (JCiP)
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Thank You!!!
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
• [ JSR 133 (Java Memory Model) FAQ ]
https://www.cs.umd.edu/~pugh/java/memoryMo
del/jsr-133-faq.html
• [ Java Concurrency in Practice ]
http://jcip.net/
• [ Concurrent Programming in Java ]
http://gee.cs.oswego.edu/dl/cpj/
• [ The JSR-133 Cookbook for Compiler Writers ]
http://gee.cs.oswego.edu/dl/jmm/cookbook.html
• [ PrintAssembly ]
https://wiki.openjdk.java.net/display/HotSpot/Pri
ntAssembly
• [ BASIC DISASSEMBLER PLUGIN FOR HOTSPOT
DOWNLOADS ]
https://kenai.com/projects/base-
hsdis/downloads
References
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Safe Harbor Statement
The preceding is intended to outline our general product direction. It is intended for
information purposes only, and may not be incorporated into any contract. It is not a
commitment to deliver any material, code, or functionality, and should not be relied upon
in making purchasing decisions. The development, release, and timing of any features or
functionality described for Oracle’s products remains at the sole discretion of Oracle.
108
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 109
Java Concurrency, A(nother) Peek Under the Hood [JavaOne 2016 CON1497]
Java Concurrency, A(nother) Peek Under the Hood [JavaOne 2016 CON1497]
Ad

Recommended

Nashorn in the future (English)
Nashorn in the future (English)
Logico
 
Nashorn: JavaScript Running on Java VM (English)
Nashorn: JavaScript Running on Java VM (English)
Logico
 
What’s new in JSR 367 Java API for JSON Binding
What’s new in JSR 367 Java API for JSON Binding
Dmitry Kornilov
 
Nashorn : JavaScript Running on Java VM (Japanese)
Nashorn : JavaScript Running on Java VM (Japanese)
Logico
 
Java EE 7 et ensuite pourquoi pas JavaScript sur le serveur!
Java EE 7 et ensuite pourquoi pas JavaScript sur le serveur!
David Delabassee
 
“Quantum” Performance Effects: beyond the Core
“Quantum” Performance Effects: beyond the Core
C4Media
 
12 Things About 12c Release 2 for Developers
12 Things About 12c Release 2 for Developers
Connor McDonald
 
Polymorphic Table Functions in SQL
Polymorphic Table Functions in SQL
Chris Saxon
 
Java Concurrency, A(nother) Peek Under the Hood [Code One 2019]
Java Concurrency, A(nother) Peek Under the Hood [Code One 2019]
David Buck
 
Java Memory Model
Java Memory Model
Łukasz Koniecki
 
Introducing Parallel Pixie Dust
Introducing Parallel Pixie Dust
Jason Hearne-McGuiness
 
Does Java Have a Future After Version 8? (Belfast JUG April 2014)
Does Java Have a Future After Version 8? (Belfast JUG April 2014)
Garth Gilmour
 
Facts about multithreading that'll keep you up at night - Guy Bar on, Vonage
Facts about multithreading that'll keep you up at night - Guy Bar on, Vonage
Codemotion Tel Aviv
 
The Java memory model made easy
The Java memory model made easy
Rafael Winterhalter
 
Concurrency
Concurrency
Isaac Liao
 
Java Concurrency in Practice
Java Concurrency in Practice
Alina Dolgikh
 
Loom Virtual Threads in the JDK 19
Loom Virtual Threads in the JDK 19
José Paumard
 
Loom promises: be there!
Loom promises: be there!
Jean-Francois James
 
Java concurrency
Java concurrency
Scheidt & Bachmann
 
Java under the hood
Java under the hood
Vachagan Balayan
 
Java Core | Modern Java Concurrency | Martijn Verburg & Ben Evans
Java Core | Modern Java Concurrency | Martijn Verburg & Ben Evans
JAX London
 
Modern Java Concurrency (OSCON 2012)
Modern Java Concurrency (OSCON 2012)
Martijn Verburg
 
C++ Data-flow Parallelism sounds great! But how practical is it? Let’s see ho...
C++ Data-flow Parallelism sounds great! But how practical is it? Let’s see ho...
Jason Hearne-McGuiness
 
Parallel Programming
Parallel Programming
Roman Okolovich
 
Jvm memory model
Jvm memory model
Yoav Avrahami
 
HotSpot Synchronization, A Peek Under the Hood [JavaOne 2015 CON7570]
HotSpot Synchronization, A Peek Under the Hood [JavaOne 2015 CON7570]
David Buck
 
Comparing different concurrency models on the JVM
Comparing different concurrency models on the JVM
Mario Fusco
 
4Developers 2015: Java Memory Consistency Model or intro to multithreaded pro...
4Developers 2015: Java Memory Consistency Model or intro to multithreaded pro...
PROIDEA
 
JDK 13 New Features [MeetUp with Java Experts! @Gaienmae/Dojima 2019]
JDK 13 New Features [MeetUp with Java Experts! @Gaienmae/Dojima 2019]
David Buck
 
JDK Mission Control: Where We Are, Where We Are Going [Groundbreakers APAC 20...
JDK Mission Control: Where We Are, Where We Are Going [Groundbreakers APAC 20...
David Buck
 

More Related Content

Similar to Java Concurrency, A(nother) Peek Under the Hood [JavaOne 2016 CON1497] (20)

Java Concurrency, A(nother) Peek Under the Hood [Code One 2019]
Java Concurrency, A(nother) Peek Under the Hood [Code One 2019]
David Buck
 
Java Memory Model
Java Memory Model
Łukasz Koniecki
 
Introducing Parallel Pixie Dust
Introducing Parallel Pixie Dust
Jason Hearne-McGuiness
 
Does Java Have a Future After Version 8? (Belfast JUG April 2014)
Does Java Have a Future After Version 8? (Belfast JUG April 2014)
Garth Gilmour
 
Facts about multithreading that'll keep you up at night - Guy Bar on, Vonage
Facts about multithreading that'll keep you up at night - Guy Bar on, Vonage
Codemotion Tel Aviv
 
The Java memory model made easy
The Java memory model made easy
Rafael Winterhalter
 
Concurrency
Concurrency
Isaac Liao
 
Java Concurrency in Practice
Java Concurrency in Practice
Alina Dolgikh
 
Loom Virtual Threads in the JDK 19
Loom Virtual Threads in the JDK 19
José Paumard
 
Loom promises: be there!
Loom promises: be there!
Jean-Francois James
 
Java concurrency
Java concurrency
Scheidt & Bachmann
 
Java under the hood
Java under the hood
Vachagan Balayan
 
Java Core | Modern Java Concurrency | Martijn Verburg & Ben Evans
Java Core | Modern Java Concurrency | Martijn Verburg & Ben Evans
JAX London
 
Modern Java Concurrency (OSCON 2012)
Modern Java Concurrency (OSCON 2012)
Martijn Verburg
 
C++ Data-flow Parallelism sounds great! But how practical is it? Let’s see ho...
C++ Data-flow Parallelism sounds great! But how practical is it? Let’s see ho...
Jason Hearne-McGuiness
 
Parallel Programming
Parallel Programming
Roman Okolovich
 
Jvm memory model
Jvm memory model
Yoav Avrahami
 
HotSpot Synchronization, A Peek Under the Hood [JavaOne 2015 CON7570]
HotSpot Synchronization, A Peek Under the Hood [JavaOne 2015 CON7570]
David Buck
 
Comparing different concurrency models on the JVM
Comparing different concurrency models on the JVM
Mario Fusco
 
4Developers 2015: Java Memory Consistency Model or intro to multithreaded pro...
4Developers 2015: Java Memory Consistency Model or intro to multithreaded pro...
PROIDEA
 
Java Concurrency, A(nother) Peek Under the Hood [Code One 2019]
Java Concurrency, A(nother) Peek Under the Hood [Code One 2019]
David Buck
 
Does Java Have a Future After Version 8? (Belfast JUG April 2014)
Does Java Have a Future After Version 8? (Belfast JUG April 2014)
Garth Gilmour
 
Facts about multithreading that'll keep you up at night - Guy Bar on, Vonage
Facts about multithreading that'll keep you up at night - Guy Bar on, Vonage
Codemotion Tel Aviv
 
Java Concurrency in Practice
Java Concurrency in Practice
Alina Dolgikh
 
Loom Virtual Threads in the JDK 19
Loom Virtual Threads in the JDK 19
José Paumard
 
Java Core | Modern Java Concurrency | Martijn Verburg & Ben Evans
Java Core | Modern Java Concurrency | Martijn Verburg & Ben Evans
JAX London
 
Modern Java Concurrency (OSCON 2012)
Modern Java Concurrency (OSCON 2012)
Martijn Verburg
 
C++ Data-flow Parallelism sounds great! But how practical is it? Let’s see ho...
C++ Data-flow Parallelism sounds great! But how practical is it? Let’s see ho...
Jason Hearne-McGuiness
 
HotSpot Synchronization, A Peek Under the Hood [JavaOne 2015 CON7570]
HotSpot Synchronization, A Peek Under the Hood [JavaOne 2015 CON7570]
David Buck
 
Comparing different concurrency models on the JVM
Comparing different concurrency models on the JVM
Mario Fusco
 
4Developers 2015: Java Memory Consistency Model or intro to multithreaded pro...
4Developers 2015: Java Memory Consistency Model or intro to multithreaded pro...
PROIDEA
 

More from David Buck (20)

JDK 13 New Features [MeetUp with Java Experts! @Gaienmae/Dojima 2019]
JDK 13 New Features [MeetUp with Java Experts! @Gaienmae/Dojima 2019]
David Buck
 
JDK Mission Control: Where We Are, Where We Are Going [Groundbreakers APAC 20...
JDK Mission Control: Where We Are, Where We Are Going [Groundbreakers APAC 20...
David Buck
 
Java Bytecode Crash Course [Code One 2019]
Java Bytecode Crash Course [Code One 2019]
David Buck
 
CSI (Crash Scene Investigation) HotSpot: Common JVM Crash Causes and Solution...
CSI (Crash Scene Investigation) HotSpot: Common JVM Crash Causes and Solution...
David Buck
 
invokedynamic for Mere Mortals [Code One 2019]
invokedynamic for Mere Mortals [Code One 2019]
David Buck
 
Hangs, Slowdowns, Starvation—Oh My! A Deep Dive into the Life of a Java Threa...
Hangs, Slowdowns, Starvation—Oh My! A Deep Dive into the Life of a Java Threa...
David Buck
 
JDK Mission Control: Where We Are, Where We Are Going [Code One 2019]
JDK Mission Control: Where We Are, Where We Are Going [Code One 2019]
David Buck
 
Z Garbage Collector
Z Garbage Collector
David Buck
 
Valhalla Update JJUG CCC Spring 2019
Valhalla Update JJUG CCC Spring 2019
David Buck
 
Var handles jjug_ccc_spring_2018
Var handles jjug_ccc_spring_2018
David Buck
 
JDK 10 へようこそ
JDK 10 へようこそ
David Buck
 
Java SE 8におけるHotSpotの進化 [Java Day Tokyo 2014 C-2]
Java SE 8におけるHotSpotの進化 [Java Day Tokyo 2014 C-2]
David Buck
 
HotSpot のロック: A Peek Under the Hood [JJUG ナイトセミナ JVM 特集 2015年8月]
HotSpot のロック: A Peek Under the Hood [JJUG ナイトセミナ JVM 特集 2015年8月]
David Buck
 
Bytecode Verification, the Hero That Java Needs [JavaOne 2016 CON1500]
Bytecode Verification, the Hero That Java Needs [JavaOne 2016 CON1500]
David Buck
 
Java Debuggers: A Peek Under the Hood [JavaOne 2016 CON1503]
Java Debuggers: A Peek Under the Hood [JavaOne 2016 CON1503]
David Buck
 
Lambda: A Peek Under The Hood [Java Day Tokyo 2015 6-3]
Lambda: A Peek Under The Hood [Java Day Tokyo 2015 6-3]
David Buck
 
Java Concurrency, A(nother) Peek Under the Hood [Java Day Tokyo 2016 3-C]
Java Concurrency, A(nother) Peek Under the Hood [Java Day Tokyo 2016 3-C]
David Buck
 
Ahead-of-Time Compilation with JDK 9 [Java Day Tokyo 2017 D1-A1]
Ahead-of-Time Compilation with JDK 9 [Java Day Tokyo 2017 D1-A1]
David Buck
 
InvokeDynamic for Mere Mortals [JavaOne 2015 CON7682]
InvokeDynamic for Mere Mortals [JavaOne 2015 CON7682]
David Buck
 
Let’s Write Our Own Chip-8 Interpreter! [JavaOne 2017 CON3584]
Let’s Write Our Own Chip-8 Interpreter! [JavaOne 2017 CON3584]
David Buck
 
JDK 13 New Features [MeetUp with Java Experts! @Gaienmae/Dojima 2019]
JDK 13 New Features [MeetUp with Java Experts! @Gaienmae/Dojima 2019]
David Buck
 
JDK Mission Control: Where We Are, Where We Are Going [Groundbreakers APAC 20...
JDK Mission Control: Where We Are, Where We Are Going [Groundbreakers APAC 20...
David Buck
 
Java Bytecode Crash Course [Code One 2019]
Java Bytecode Crash Course [Code One 2019]
David Buck
 
CSI (Crash Scene Investigation) HotSpot: Common JVM Crash Causes and Solution...
CSI (Crash Scene Investigation) HotSpot: Common JVM Crash Causes and Solution...
David Buck
 
invokedynamic for Mere Mortals [Code One 2019]
invokedynamic for Mere Mortals [Code One 2019]
David Buck
 
Hangs, Slowdowns, Starvation—Oh My! A Deep Dive into the Life of a Java Threa...
Hangs, Slowdowns, Starvation—Oh My! A Deep Dive into the Life of a Java Threa...
David Buck
 
JDK Mission Control: Where We Are, Where We Are Going [Code One 2019]
JDK Mission Control: Where We Are, Where We Are Going [Code One 2019]
David Buck
 
Z Garbage Collector
Z Garbage Collector
David Buck
 
Valhalla Update JJUG CCC Spring 2019
Valhalla Update JJUG CCC Spring 2019
David Buck
 
Var handles jjug_ccc_spring_2018
Var handles jjug_ccc_spring_2018
David Buck
 
JDK 10 へようこそ
JDK 10 へようこそ
David Buck
 
Java SE 8におけるHotSpotの進化 [Java Day Tokyo 2014 C-2]
Java SE 8におけるHotSpotの進化 [Java Day Tokyo 2014 C-2]
David Buck
 
HotSpot のロック: A Peek Under the Hood [JJUG ナイトセミナ JVM 特集 2015年8月]
HotSpot のロック: A Peek Under the Hood [JJUG ナイトセミナ JVM 特集 2015年8月]
David Buck
 
Bytecode Verification, the Hero That Java Needs [JavaOne 2016 CON1500]
Bytecode Verification, the Hero That Java Needs [JavaOne 2016 CON1500]
David Buck
 
Java Debuggers: A Peek Under the Hood [JavaOne 2016 CON1503]
Java Debuggers: A Peek Under the Hood [JavaOne 2016 CON1503]
David Buck
 
Lambda: A Peek Under The Hood [Java Day Tokyo 2015 6-3]
Lambda: A Peek Under The Hood [Java Day Tokyo 2015 6-3]
David Buck
 
Java Concurrency, A(nother) Peek Under the Hood [Java Day Tokyo 2016 3-C]
Java Concurrency, A(nother) Peek Under the Hood [Java Day Tokyo 2016 3-C]
David Buck
 
Ahead-of-Time Compilation with JDK 9 [Java Day Tokyo 2017 D1-A1]
Ahead-of-Time Compilation with JDK 9 [Java Day Tokyo 2017 D1-A1]
David Buck
 
InvokeDynamic for Mere Mortals [JavaOne 2015 CON7682]
InvokeDynamic for Mere Mortals [JavaOne 2015 CON7682]
David Buck
 
Let’s Write Our Own Chip-8 Interpreter! [JavaOne 2017 CON3584]
Let’s Write Our Own Chip-8 Interpreter! [JavaOne 2017 CON3584]
David Buck
 
Ad

Recently uploaded (20)

Key Challenges in Troubleshooting Customer On-Premise Applications
Key Challenges in Troubleshooting Customer On-Premise Applications
Tier1 app
 
HPE Reseller in uae by numerosystom.pptx
HPE Reseller in uae by numerosystom.pptx
aadibva452
 
IObit Driver Booster Pro 12 Crack Latest Version Download
IObit Driver Booster Pro 12 Crack Latest Version Download
pcprocore
 
Introduction to Agile Frameworks for Product Managers.pdf
Introduction to Agile Frameworks for Product Managers.pdf
Ali Vahed
 
Foundations of Marketo Engage - Programs, Campaigns & Beyond - June 2025
Foundations of Marketo Engage - Programs, Campaigns & Beyond - June 2025
BradBedford3
 
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
BradBedford3
 
Canva Pro Crack Free Download 2025-FREE LATEST
Canva Pro Crack Free Download 2025-FREE LATEST
grete1122g
 
Microsoft Business-230T01A-ENU-PowerPoint_01.pptx
Microsoft Business-230T01A-ENU-PowerPoint_01.pptx
soulamaabdoulaye128
 
NVIDIA GPU Technologies for AI and High-Performance Computing
NVIDIA GPU Technologies for AI and High-Performance Computing
SandeepKS52
 
Porting Qt 5 QML Modules to Qt 6 Webinar
Porting Qt 5 QML Modules to Qt 6 Webinar
ICS
 
Making significant Software Architecture decisions
Making significant Software Architecture decisions
Bert Jan Schrijver
 
Who will create the languages of the future?
Who will create the languages of the future?
Jordi Cabot
 
Download Adobe Illustrator Crack free for Windows 2025?
Download Adobe Illustrator Crack free for Windows 2025?
grete1122g
 
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
Hassan Abid
 
Modern Platform Engineering with Choreo - The AI-Native Internal Developer Pl...
Modern Platform Engineering with Choreo - The AI-Native Internal Developer Pl...
WSO2
 
Shell Skill Tree - LabEx Certification (LabEx)
Shell Skill Tree - LabEx Certification (LabEx)
VICTOR MAESTRE RAMIREZ
 
openSAP_s4h27_Week_1_2025_All_Slides.pdf
openSAP_s4h27_Week_1_2025_All_Slides.pdf
Thomas Qiao
 
OpenChain Webinar - AboutCode - Practical Compliance in One Stack – Licensing...
OpenChain Webinar - AboutCode - Practical Compliance in One Stack – Licensing...
Shane Coughlan
 
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
Maharshi Mallela
 
Threat Modeling a Batch Job Framework - Teri Radichel - AWS re:Inforce 2025
Threat Modeling a Batch Job Framework - Teri Radichel - AWS re:Inforce 2025
2nd Sight Lab
 
Key Challenges in Troubleshooting Customer On-Premise Applications
Key Challenges in Troubleshooting Customer On-Premise Applications
Tier1 app
 
HPE Reseller in uae by numerosystom.pptx
HPE Reseller in uae by numerosystom.pptx
aadibva452
 
IObit Driver Booster Pro 12 Crack Latest Version Download
IObit Driver Booster Pro 12 Crack Latest Version Download
pcprocore
 
Introduction to Agile Frameworks for Product Managers.pdf
Introduction to Agile Frameworks for Product Managers.pdf
Ali Vahed
 
Foundations of Marketo Engage - Programs, Campaigns & Beyond - June 2025
Foundations of Marketo Engage - Programs, Campaigns & Beyond - June 2025
BradBedford3
 
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
Women in Tech: Marketo Engage User Group - June 2025 - AJO with AWS
BradBedford3
 
Canva Pro Crack Free Download 2025-FREE LATEST
Canva Pro Crack Free Download 2025-FREE LATEST
grete1122g
 
Microsoft Business-230T01A-ENU-PowerPoint_01.pptx
Microsoft Business-230T01A-ENU-PowerPoint_01.pptx
soulamaabdoulaye128
 
NVIDIA GPU Technologies for AI and High-Performance Computing
NVIDIA GPU Technologies for AI and High-Performance Computing
SandeepKS52
 
Porting Qt 5 QML Modules to Qt 6 Webinar
Porting Qt 5 QML Modules to Qt 6 Webinar
ICS
 
Making significant Software Architecture decisions
Making significant Software Architecture decisions
Bert Jan Schrijver
 
Who will create the languages of the future?
Who will create the languages of the future?
Jordi Cabot
 
Download Adobe Illustrator Crack free for Windows 2025?
Download Adobe Illustrator Crack free for Windows 2025?
grete1122g
 
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
On-Device AI: Is It Time to Go All-In, or Do We Still Need the Cloud?
Hassan Abid
 
Modern Platform Engineering with Choreo - The AI-Native Internal Developer Pl...
Modern Platform Engineering with Choreo - The AI-Native Internal Developer Pl...
WSO2
 
Shell Skill Tree - LabEx Certification (LabEx)
Shell Skill Tree - LabEx Certification (LabEx)
VICTOR MAESTRE RAMIREZ
 
openSAP_s4h27_Week_1_2025_All_Slides.pdf
openSAP_s4h27_Week_1_2025_All_Slides.pdf
Thomas Qiao
 
OpenChain Webinar - AboutCode - Practical Compliance in One Stack – Licensing...
OpenChain Webinar - AboutCode - Practical Compliance in One Stack – Licensing...
Shane Coughlan
 
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
Maharshi Mallela
 
Threat Modeling a Batch Job Framework - Teri Radichel - AWS re:Inforce 2025
Threat Modeling a Batch Job Framework - Teri Radichel - AWS re:Inforce 2025
2nd Sight Lab
 
Ad

Java Concurrency, A(nother) Peek Under the Hood [JavaOne 2016 CON1497]

  • 3. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Java Concurrency A(nother) Peek Under the Hood David Buck Principal Member of Technical Staff Java SE Sustaining Engineering September, 2016
  • 4. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Safe Harbor Statement The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. 4
  • 5. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | About Me David Buck • Java SE Sustaining Engineering • Mostly JRockit fixes • OpenJDK 8 Updates Project Maintainer • Hobbies: Programming
  • 6. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Program Agenda Introduction Background Tangent 1: Special Relativity History Implementation Tangent 2: HSDIS Future Conclusions 1 2 3 4 5 6 6
  • 7. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Introduction
  • 8. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Motivation “But I don’t write multithreaded code…”
  • 9. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Web Server picture: Coolcaesar at the English language Wikipedia [GFDL (http://www.gnu.org/copyleft/fdl.html) or CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/)]
  • 10. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | GUI
  • 11. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Libraries picture: David Vignoni / ICON KING (http://icon-king.com) [LGPL (http://www.gnu.org/licenses/lgpl.html) or LGPL (http://www.gnu.org/licenses/lgpl.html)]
  • 12. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Batch Processing picture: US Social Security Administration (http://www.ssa.gov/history/acalcs.html) [Public domain]
  • 13. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | “But I don’t write multithreaded code…”
  • 14. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | “But I don’t write multithreaded code…”
  • 15. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Race Conditions picture: Sakurambo at English Wikipedia [GFDL (http://www.gnu.org/copyleft/fdl.html) or CC-BY-SA-3.0 (http://creativecommons.org/licenses/by-sa/3.0/)]
  • 16. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Heisenbugs Bug Heisenberg Heisenbug picture: Bundesarchiv, Bild 183-R57262 / Unknown / CC-BY-SA 3.0 [CC BY-SA 3.0 de (http://creativecommons.org/licenses/by-sa/3.0/de/deed.en)]
  • 17. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Observer Effect picture: Christian Schirm (Own work) [CC0]
  • 18. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Avoiding Heisenbugs • Java Memory Model • synchronized keyword • java.util.concurrent
  • 19. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Avoiding Heisenbugs • Java Memory Model • synchronized keyword • java.util.concurrent • Don’t do XXX • You must do YYY
  • 20. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | a² + b² == c²
  • 21. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |picture: WTF Public License, Version 2
  • 22. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Background
  • 23. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Memory Model
  • 24. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Memory Model Definition 1 Specification that explains when we can guarantee that a written value may be read correctly
  • 25. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | void hogeMethod1() { int localA = 42; assert localA == 42; }
  • 26. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | static int staticA; void hogeMethod2() { staticA = 42; assert staticA == 42; }
  • 27. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | int data = 0; boolean ready = false; void hoge3() { while (!ready) {}; assert data == 42; } void hoge4() { data = 42; ready = true; }
  • 28. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Culprit #1
  • 29. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Culprit #2
  • 30. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Memory Ordering void hoge5() { a = 1; b = 2; c = 3; d = 4; e = 5; a = a + 1; }
  • 31. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Memory Ordering void hoge5() { a = 1; b = 2; c = 3; d = 4; e = 5; a = a + 1; } void hoge5() { a = 2; b = 2; c = 3; d = 4; e = 5; }
  • 32. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Memory Ordering void hoge5() { a = 1; b = 2; c = 3; d = 4; e = 5; a = a + 1; } void hoge5() { b = 2; c = 3; d = 4; e = 5; a = 2; }
  • 33. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Rule Single threaded behavior must never change
  • 34. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Out of Order Execution picture: Amit6, original version (File:Superscalarpipeline.png) by User:Poil (Own work) [CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0)]
  • 35. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | CPU Differences Type Alpha ARMv7 PA- RISC POWER SPARC RMO SPARC PSO SPARC TSO x86 x86 oostore AMD64 IA-64 zSeries load- load Y Y Y Y Y Y Y load- store Y Y Y Y Y Y Y store- store Y Y Y Y Y Y Y Y store- load Y Y Y Y Y Y Y Y Y Y Y Y Atomic (loads) Y Y Y Y Y Atomic (stores) Y Y Y Y Y Y Depend ent loads Y instruct ion cache Y Y Y Y Y Y Y Y Y Y chart source: https://en.wikipedia.org/wiki/Memory_ordering
  • 36. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | CPU Differences Loose Strict Alpha X86
  • 37. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | CPU Differences Loose Strict
  • 38. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Memory Barriers int data = 0; boolean ready = false; void hoge3() { while (!ready) {}; assert data == 42; } void hoge4() { data = 42; ready = true; }
  • 39. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Memory Barriers int data = 0; boolean ready = false; void hoge3() { while (!ready) {}; assert data == 42; } void hoge4() { data = 42; ready = true; }
  • 40. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Memory Barriers int data = 0; boolean ready = false; void hoge3() { while (!ready) {}; assert data == 42; } void hoge4() { data = 42; ready = true; }
  • 41. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Memory Barriers Types • store-store • store-load • load-store • load-load
  • 42. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Compiler Barriers • GCC – __asm__ __volatile__("":::"memory"); • VC++ – _ReadBarrier – _WriteBarrier – _ReadWriteBarrier
  • 43. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
  • 44. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Tangent 1: Special Relativity picture: Sakurambo (Own work) [Public domain]
  • 45. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Tangent 1: Special Relativity A B C
  • 46. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Tangent 1: Special Relativity A B C A, B, C
  • 47. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Tangent 1: Special Relativity A B C A, B, C C, B, A
  • 48. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Tangent 1: Special Relativity Observed order depends on viewer's frame of reference A B C A, B, C C, B, A
  • 49. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | No single “correct” timeline • Special Relativity – Observed order of events depends on frame of reference
  • 50. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | No single “correct” timeline • Special Relativity – Observed order of events depends on frame of reference • Multithreaded Code – Observed order of events depends on thread
  • 51. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Memory Model Definition 2 • Clarify what multithreaded behavior developers may depend on
  • 52. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Memory Model Definition 2 • Clarify what multithreaded behavior developers may depend on • Limits what types of optimizations the runtime may do
  • 53. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | History
  • 54. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Java Memory Model (JMM) • Write once, run anywhere • 1995 • Part of the formal language specification • Happened-before picture: Peter Campbell [GFDL (http://www.gnu.org/copyleft/fdl.html) or CC BY-SA 4.0-3.0-2.5-2.0-1.0 (http://creativecommons.org/licenses/by-sa/4.0-3.0-2.5-2.0-1.0)]
  • 55. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | happened-before
  • 56. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Leslie Lamport • LaTex • happened-before picture: Leslie Lamport [GFDL (http://en.wikipedia.org/wiki/GFDL]
  • 57. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | happened-before void hoge5() { a = 1; b = 2; c = 3; d = 4; e = 5; a = a + 1; }
  • 58. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | happened-before void hoge4() { data = 42; ready = true; } void hoge3() { while (!ready) {}; assert data == 42; }
  • 59. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | happened-before void hoge4() { data = 42; ready = true; } void hoge3() { while (!ready) {}; assert data == 42; }
  • 60. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | happened-before void synchronized hoge4() { data = 42; ready = true; } void synchronized hoge3() { while (!ready) {}; assert data == 42; } Warning: if hoge3 is executed first, the above code will deadlock
  • 61. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | happened-before void synchronized hoge4() { data = 42; ready = true; } void synchronized hoge3() { while (!ready) {}; assert data == 42; } Warning: if hoge3 is executed first, the above code will deadlock
  • 62. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | JMM Specification
  • 63. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Original JMM Keywords • synchronized – mutual exclusion – happened-before
  • 64. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Original JMM Keywords • synchronized – mutual exclusion – happened-before • volatile – Visibility
  • 65. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Problems with the Original JMM • volatile does not establish a happened-before relationship • final values can change • Many important runtime optimization were not allowed
  • 66. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Doug Lea • Author of the Java Multithreaded bible • Introduced his own OSS high-level multithreaded library
  • 67. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Java SE 5.0 • Modern JMM introduced • Lea’s library added to official JDK picture: Zvi Roger [CC BY 3.0 (http://creativecommons.org/licenses/by/3.0)]
  • 68. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Modern JMM • JSR-133 • Fixed shortcomings in original JMM – volatile does establish happens-before – final values will never change
  • 69. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Modern JMM Keywords • synchronized – mutual exclusion – happened-before • volatile – visibility – happened-before • final – immutability
  • 70. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | JSR-133 の volatile void hoge4() { data = 42; ready = true; } void hoge3() { while (!ready) {}; assert data == 42; } Warning: if hoge3 is executed first, the above code will deadlock volatile boolean ready = false;
  • 71. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | volatile != atomic volatile int id = 0; int incID() { return id++; }
  • 72. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | volatile != atomic volatile int id = 0; int incID() { return id++; } reg = [id] reg++ [id] = reg
  • 73. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | volatile != atomic volatile int id = 0; synchronized int incID() { return id++; }
  • 74. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | happened-before • Monitor (acquisition / release) • Volatile (read / write) • final “freeze” (constructor) • JNI – System.out.println() – Input / Output • thread start()/join() • operations on j.u.concurrent collections
  • 75. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | java.util.concurency • JSR-166 • Doug Lee’s OSS multithreading library
  • 76. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Multithreaded Control • java.util.concurency • synchronized – wait()/notify() • volatile / final Abstract Low-level
  • 77. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Fork/Join (JDK 7)
  • 78. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Functional Programming (JDK 8) • Lambda Forms • Stream API λ
  • 79. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Implementation
  • 80. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Tangent 2: HSDIS (HotSpot Disassembler) • Lets us see code generated by JIT • Requires a disassembler plugin – GNU binutils-based plugin – base-hsdis • Command line options – +PrintAssembly – +CompileCommand=print,*MyClass.myMethod
  • 81. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Demos
  • 82. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | ARM Assembly Crash Course
  • 83. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | ARM Assembly Crash Course Op Target Source
  • 84. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | ARM Assembly Crash Course MOV r02 #42
  • 85. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | ARM Assembly Crash Course MOV r02 #42 r02 = 42
  • 86. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | ARM Assembly Crash Course Op Target Source
  • 87. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | ARM Assembly Crash Course Op Target Source Exception STR Source Target
  • 88. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | ARM Assembly Crash Course MOV Copies registers MOV r02 r03 Writes Literals MOV r02 #42
  • 89. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | ARM Assembly Crash Course LDx Loads data from memory to a register ldr r03, [r06] r03 = *r06
  • 90. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | ARM Assembly Crash Course CMP / BEQ Compare and branch if equal cmp r0, #0 beq 0x74178d08 if (r0 == 0) goto 0x74178d08
  • 91. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | ARM Assembly Crash Course DMB Data Memory Barrier dmb st dmb sy
  • 92. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | One more thing…
  • 93. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Safe Points • Methods proactively poll • JVM makes page unreadable to stop world movw ip, #45056 ; 0xb000 movt ip, #30461 ; 0x76fd ldr ip, [ip] ; {poll_return}
  • 94. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Long Value static long val = 0; public static void main(String[] args) { new Thread(() -> { while(true) val = -1L; } ).start(); new Thread(() -> { while(true) val = 0L; } ).start(); while (true) { long temp = val; if ( temp != -1 && temp != 0 ) { System.out.println("temp (DEC): " + temp); System.out.println("temp (BIN): " + Long.toBinaryString(temp)); System.exit(-1); } } } }
  • 95. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Object Initialization public class ObjInit { static ObjInit globalRef = new ObjInit(); long number; public ObjInit() { number = 42; }
  • 96. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Object Initialization public static void main(String[] args) { new Thread(() -> { while(true) { long temp = globalRef.number; if (temp != 42) { System.out.println(temp); System.exit(-1); } } } ).start(); new Thread(() -> { while(true) globalRef = new ObjInit(); } ).start(); }}
  • 97. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | public class Loop extends Thread { static boolean done = false; public static void main(String[] args) { Thread childThread = new Loop(); childThread.start(); System.out.println("Starting loop"); spinWait(); System.out.println("Exited loop"); } private static void spinWait() { while (!done) ; } public void run() { try { Thread.sleep(5 * 1000); } catch (Exception e) {} done = true; } } // class Loop Loop
  • 98. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Future
  • 99. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | JEP-188/JMM9 • Not planned for JDK 9 (JMM9 != JDK 9) • JVM-level specification • Compatibility with C11/C++11 • Cover new features added since JDK 5 – (e.g. AtomicX.weakCompareAndSet())
  • 100. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Conclusions
  • 101. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Avoid low-level API • java.util.concurency • synchronized – wait()/notify() • volatile / final Abstract Low-level
  • 102. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Code against the specification • Intermittent bugs are common • Behavior changes with JRE / platform • Stuff to avoid – synchronized (new Object()) { } – Randomly adding volatile declarations until it magically starts working
  • 103. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Testing • As close to production environment as possible – Same hardware – Same JRE (including version) – Same settings
  • 104. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Java Concurrency in Practice • Java Concurrency Bible • Covers JSR-133 / JSR-166
  • 105. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Summary • Avoid low-level APIs • Code against the standard, not the implementation • Testing is always indispensible • Must read Java Concurrency in Practice (JCiP)
  • 106. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Thank You!!!
  • 107. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | • [ JSR 133 (Java Memory Model) FAQ ] https://www.cs.umd.edu/~pugh/java/memoryMo del/jsr-133-faq.html • [ Java Concurrency in Practice ] http://jcip.net/ • [ Concurrent Programming in Java ] http://gee.cs.oswego.edu/dl/cpj/ • [ The JSR-133 Cookbook for Compiler Writers ] http://gee.cs.oswego.edu/dl/jmm/cookbook.html • [ PrintAssembly ] https://wiki.openjdk.java.net/display/HotSpot/Pri ntAssembly • [ BASIC DISASSEMBLER PLUGIN FOR HOTSPOT DOWNLOADS ] https://kenai.com/projects/base- hsdis/downloads References
  • 108. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | Safe Harbor Statement The preceding is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. 108
  • 109. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | 109