5. YOU SHOULD READ THE FINE PRINT.
TLDR: “AS IF”
AS IF THERE’S ONE THREAD,
UNLESS YOU SAY OTHERWISE.
6. The behavior of threads, particularly
when not correctly synchronized, can
be confusing and counterintuitive.
“
“
Java 8 Language Specification
§ 17.4 pp 631
7. Immutable objects have a very compelling
list of positive qualities. Without question,
they are among the simplest and most robust
kinds of classes you can possibly build. When
you create immutable classes, entire
categories of problems simply disappear.
“ “
www.javapractices.com
13. NO GARBAGE VALUES
NO OUT OF “THIN AIR” VALUES
WILL SEE A ZERO-ISH VALUE
OR
AN ASSIGNED VALUE
14. NO *COMPLETELY* GARBAGE VALUES
TWO SEPARATE HALVES OF LONG OR DOUBLE
BUT
BOTH HALVES ARE ZERO OR ASSIGNED
15. ONLY ASSIGNED VALUES FOR FINALS
sharedPoint = new Point(x, y);
class Point {
final int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
*
*After constructor return
JLS8 § 17.5 pp 652
16. NOT *ALL* VALUES ARE STORED
sharedX = 20;
sharedX = 40;
print(sharedX);
Dead Store
27. IMPORTANT OPTIMIZATION
for ( int x: array ) {
… x …
}
for ( int i = 0; i < this.array.length; ++i ) {
… this.array[i] …
}
Object[] local = this.array;
for ( int i = 0; i < local.length; ++i ) {
… local[i] …
}
29. MAYBE NOT…
while ( !done ); local1 = !done;
while ( local1 );
Load False &
Loop Forever
30. To some programmers, this behavior
may seem broken. However, it should
be noted that this code is improperly
synchronized.
“
“
Java 8 Language Specification
§ 17.4 pp 638
31. MEMORY COHERENCE
Coherence is a guarantee that a
thread cannot see an old value for
the same memory location after
seeing a newer value.
32. TIME PARADOX
// sharedPoint1 might equal
// sharedPoint2
int i = sharedPoint1.x;
// another thread can change x
int j = sharedPoint2.x;
// allow old value or
// cannot optimize
int k = sharedPoint3.x;
Point
x
y
100
100
125
sharedPoint1
sharedPoint2
JLS8 § 17.4-C pp 638
33. The semantics of the Java programming
language allow compilers and micro-
processors to perform optimizations that
can interact with incorrectly synchronized
code in ways that can produce behaviors
that seem paradoxical.
“ “
Java 8 Language Specification
§ 17.4 pp 637
38. INTEL KABY LAKE
https://en.wikichip.org/wiki/intel/microarchitectures/kaby_lake
FETCH
RENAME & ALLOCATE
DECODE
ALLOCATION QUEUE
SCHEDULERS
ALU
VECT ALU
VECT SHIFT
VECT ADD
VECT MUL
FMA
DIV
BRANCH
ALU
FAST LEA
VECT ALU
VECT SHIFT
VECT ADD
VECT MUL
FMA
SLOW INT
SLOW LEA
ALU
FAST LEA
VECT ALU
VECT SHUFFLE
ALU & SHIFT
BRANCH
AGU
LOAD DATA
AGU
LOAD DATA
STORE DATA AGU
LOAD
BUFFER
STORE
BUFFER
add reg0, 1 add reg1, 2
49. RISC / ARM CISC / x86 Java
Loads After Loads YES YES YES
Loads After Stores YES YES YES
Stores After Stores YES NO YES
Stores After Loads YES YES YES
57. RIGHT WAY TO DO LAZY SINGLETON IN JAVA
static Singleton instance() {
return Holder.INSTANCE;
}
static class Holder {
static final class Singleton INSTANCE = new Singleton();
}
CLASS INITIALIZATION IS ALREADY LAZY.
68. Immutable objects have a very compelling
list of positive qualities. Without question,
they are among the simplest and most robust
kinds of classes you can possibly build. When
you create immutable classes, entire
categories of problems simply disappear.
“ “
www.javapractices.com
69. REFERENCES
JAVA CURRENCY IN PRACTICE
Brian Goetz et al
JAVA MEMORY MODEL PRAGMATICS
Aleksey Shipilëv
http://virtualjug.com/java-memory-model-pragmatics/
CLOSE ENCOUNTERS OF THE JMM KIND
Aleksey Shipilëv
https://shipilev.net/blog/2016/close-encounters-of-jmm-kind/
70. HISTORY
JAVA’S MEMORY MODEL IS FATALLY FLAWED
Bill Pugh
http://www.cs.umd.edu/~pugh/java/broken.pdf
Bill Pugh
FIXING JAVA’S MEMORY MODEL
http://www.cs.umd.edu/~pugh/jmm.pdf