java volatile variables: introduction - vanderbilt universityschmidt/cs891s/2020-pdfs/3.3.1... ·...
TRANSCRIPT
![Page 1: Java Volatile Variables: Introduction - Vanderbilt Universityschmidt/cs891s/2020-PDFs/3.3.1... · 2020-02-20 · volatile write is visible to “happens-after” reads Overview of](https://reader034.vdocument.in/reader034/viewer/2022042322/5f0be57b7e708231d432c07b/html5/thumbnails/1.jpg)
Java Volatile Variables:
Introduction
Douglas C. [email protected]
www.dre.vanderbilt.edu/~schmidt
Institute for Software
Integrated Systems
Vanderbilt University
Nashville, Tennessee, USA
![Page 2: Java Volatile Variables: Introduction - Vanderbilt Universityschmidt/cs891s/2020-PDFs/3.3.1... · 2020-02-20 · volatile write is visible to “happens-after” reads Overview of](https://reader034.vdocument.in/reader034/viewer/2022042322/5f0be57b7e708231d432c07b/html5/thumbnails/2.jpg)
2
Learning Objectives in this Part of the Lesson• Understand how Java volatile variables
provide concurrent programs with thread-safe mechanisms to read from & write to single variables
![Page 3: Java Volatile Variables: Introduction - Vanderbilt Universityschmidt/cs891s/2020-PDFs/3.3.1... · 2020-02-20 · volatile write is visible to “happens-after” reads Overview of](https://reader034.vdocument.in/reader034/viewer/2022042322/5f0be57b7e708231d432c07b/html5/thumbnails/3.jpg)
3
Overview of Java Volatile Variables
![Page 4: Java Volatile Variables: Introduction - Vanderbilt Universityschmidt/cs891s/2020-PDFs/3.3.1... · 2020-02-20 · volatile write is visible to “happens-after” reads Overview of](https://reader034.vdocument.in/reader034/viewer/2022042322/5f0be57b7e708231d432c07b/html5/thumbnails/4.jpg)
4
Overview of Java Volatile Variables• When a concurrent program is not
written correctly, the errors tend to fall into three categories: atomicity, visibility, or ordering
See earlier lesson on “Overview of Atomic Operations”
![Page 5: Java Volatile Variables: Introduction - Vanderbilt Universityschmidt/cs891s/2020-PDFs/3.3.1... · 2020-02-20 · volatile write is visible to “happens-after” reads Overview of](https://reader034.vdocument.in/reader034/viewer/2022042322/5f0be57b7e708231d432c07b/html5/thumbnails/5.jpg)
5
Overview of Java Volatile Variables• Volatile ensures that changes to a
variable are always consistent & visible to other threads atomically
See tutorials.jenkov.com/java-concurrency/volatile.html
![Page 6: Java Volatile Variables: Introduction - Vanderbilt Universityschmidt/cs891s/2020-PDFs/3.3.1... · 2020-02-20 · volatile write is visible to “happens-after” reads Overview of](https://reader034.vdocument.in/reader034/viewer/2022042322/5f0be57b7e708231d432c07b/html5/thumbnails/6.jpg)
6
Overview of Java Volatile Variables• Volatile ensures that changes to a
variable are always consistent & visible to other threads atomically
• Reads & writes go directly to main memory (not registers/cache) to avoid read/write conflicts on Java fields storing shared mutable data
![Page 7: Java Volatile Variables: Introduction - Vanderbilt Universityschmidt/cs891s/2020-PDFs/3.3.1... · 2020-02-20 · volatile write is visible to “happens-after” reads Overview of](https://reader034.vdocument.in/reader034/viewer/2022042322/5f0be57b7e708231d432c07b/html5/thumbnails/7.jpg)
7
Overview of Java Volatile Variables• Volatile ensures that changes to a
variable are always consistent & visible to other threads atomically
• Reads & writes go directly to main memory (not registers/cache) to avoid read/write conflicts on Java fields storing shared mutable data
• Volatile reads/writes cannot be reordered
![Page 8: Java Volatile Variables: Introduction - Vanderbilt Universityschmidt/cs891s/2020-PDFs/3.3.1... · 2020-02-20 · volatile write is visible to “happens-after” reads Overview of](https://reader034.vdocument.in/reader034/viewer/2022042322/5f0be57b7e708231d432c07b/html5/thumbnails/8.jpg)
8
Overview of Java Volatile Variables• Volatile ensures that changes to a
variable are always consistent & visible to other threads atomically
• Reads & writes go directly to main memory (not registers/cache) to avoid read/write conflicts on Java fields storing shared mutable data
• Volatile reads/writes cannot be reordered
• The Java compiler automatically transforms reads & writes on a volatile variable into atomic acquire & release pairs
![Page 9: Java Volatile Variables: Introduction - Vanderbilt Universityschmidt/cs891s/2020-PDFs/3.3.1... · 2020-02-20 · volatile write is visible to “happens-after” reads Overview of](https://reader034.vdocument.in/reader034/viewer/2022042322/5f0be57b7e708231d432c07b/html5/thumbnails/9.jpg)
9
Overview of Java Volatile Variables• Volatile is not needed in sequential
programs for several reasons
![Page 10: Java Volatile Variables: Introduction - Vanderbilt Universityschmidt/cs891s/2020-PDFs/3.3.1... · 2020-02-20 · volatile write is visible to “happens-after” reads Overview of](https://reader034.vdocument.in/reader034/viewer/2022042322/5f0be57b7e708231d432c07b/html5/thumbnails/10.jpg)
10
Overview of Java Volatile Variables• Volatile is not needed in sequential
programs for several reasons, e.g.
• Reads & writes of (most) Java primitive variables are atomic
Main Memory
42 13
nv v
write nv=42
…
read nv=42
Main
Thread
If the main thread writes a value to a non-volatile (nv) field the next read of that field will get that value
![Page 11: Java Volatile Variables: Introduction - Vanderbilt Universityschmidt/cs891s/2020-PDFs/3.3.1... · 2020-02-20 · volatile write is visible to “happens-after” reads Overview of](https://reader034.vdocument.in/reader034/viewer/2022042322/5f0be57b7e708231d432c07b/html5/thumbnails/11.jpg)
11
Overview of Java Volatile Variables• Volatile is not needed in sequential
programs for several reasons, e.g.
• Reads & writes of (most) Java primitive variables are atomic
• Although multiple-step operations are performed at the machine code level for variables of types long & double, these operations aren’t interleaved in a single-threaded program
See docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.7
![Page 12: Java Volatile Variables: Introduction - Vanderbilt Universityschmidt/cs891s/2020-PDFs/3.3.1... · 2020-02-20 · volatile write is visible to “happens-after” reads Overview of](https://reader034.vdocument.in/reader034/viewer/2022042322/5f0be57b7e708231d432c07b/html5/thumbnails/12.jpg)
12
Overview of Java Volatile Variables• Volatile is needed in concurrent
Java programs
See en.wikipedia.org/wiki/Volatile_variable
![Page 13: Java Volatile Variables: Introduction - Vanderbilt Universityschmidt/cs891s/2020-PDFs/3.3.1... · 2020-02-20 · volatile write is visible to “happens-after” reads Overview of](https://reader034.vdocument.in/reader034/viewer/2022042322/5f0be57b7e708231d432c07b/html5/thumbnails/13.jpg)
13
Overview of Java Volatile Variables• Volatile is needed in concurrent
Java programs
• One thread may not see the latest value of a variable changed by another thread due to caching
Main Memory
42 13
nv v
Cache 1
42 13
nv v
Cache 2
42 13
nv v
Cache n
42 13
nv v
ThreadnThread1 Thread2
![Page 14: Java Volatile Variables: Introduction - Vanderbilt Universityschmidt/cs891s/2020-PDFs/3.3.1... · 2020-02-20 · volatile write is visible to “happens-after” reads Overview of](https://reader034.vdocument.in/reader034/viewer/2022042322/5f0be57b7e708231d432c07b/html5/thumbnails/14.jpg)
14
Overview of Java Volatile Variables• Volatile is needed in concurrent
Java programs
• One thread may not see the latest value of a variable changed by another thread due to caching
Main Memory
42 13
nv v
Cache 1
7 13
nv v
Cache 2
42 13
nv v
Cache n
42 13
nv v
ThreadnThread1 Thread2
write
nv = 7
Thread1 writes a value to a non-volatile field nv,
which is cached locally in the core for efficiency
![Page 15: Java Volatile Variables: Introduction - Vanderbilt Universityschmidt/cs891s/2020-PDFs/3.3.1... · 2020-02-20 · volatile write is visible to “happens-after” reads Overview of](https://reader034.vdocument.in/reader034/viewer/2022042322/5f0be57b7e708231d432c07b/html5/thumbnails/15.jpg)
15
Overview of Java Volatile Variables• Volatile is needed in concurrent
Java programs
• One thread may not see the latest value of a variable changed by another thread due to caching
Main Memory
42 13
nv v
Cache 1
7 13
nv v
Cache 2
42 13
nv v
Cache n
42 13
nv v
ThreadnThread1 Thread2
write
nv = 7
read
nv = 42
When Thread2 subsequently reads the value of field nv it gets a different result due to caching
![Page 16: Java Volatile Variables: Introduction - Vanderbilt Universityschmidt/cs891s/2020-PDFs/3.3.1... · 2020-02-20 · volatile write is visible to “happens-after” reads Overview of](https://reader034.vdocument.in/reader034/viewer/2022042322/5f0be57b7e708231d432c07b/html5/thumbnails/16.jpg)
16
Overview of Java Volatile Variables• Java defines the volatile keyword
to address these problems
See en.wikipedia.org/wiki/Volatile_variable#In_Java
![Page 17: Java Volatile Variables: Introduction - Vanderbilt Universityschmidt/cs891s/2020-PDFs/3.3.1... · 2020-02-20 · volatile write is visible to “happens-after” reads Overview of](https://reader034.vdocument.in/reader034/viewer/2022042322/5f0be57b7e708231d432c07b/html5/thumbnails/17.jpg)
17
Overview of Java Volatile Variables• Java defines the volatile keyword
to address these problems
• A value written to a volatile variable will always be stored in main memory
Main Memory
7 7
nv v
Cache 1
7 7
nv v
Cache 2
42 13
nv v
Cache n
42 13
nv v
ThreadnThread1 Thread2
write
v = 7
![Page 18: Java Volatile Variables: Introduction - Vanderbilt Universityschmidt/cs891s/2020-PDFs/3.3.1... · 2020-02-20 · volatile write is visible to “happens-after” reads Overview of](https://reader034.vdocument.in/reader034/viewer/2022042322/5f0be57b7e708231d432c07b/html5/thumbnails/18.jpg)
18
Overview of Java Volatile Variables• Java defines the volatile keyword
to address these problems
• A value written to a volatile variable will always be stored in main memory
• A volatile write “happens-before” all following reads of the same variable
Main Memory
7 7
nv v
Cache 1
7 7
nv v
Cache 2
42 13
nv v
Cache n
42 13
nv v
ThreadnThread1 Thread2
write
v = 7
See en.wikipedia.org/wiki/Happened-before
![Page 19: Java Volatile Variables: Introduction - Vanderbilt Universityschmidt/cs891s/2020-PDFs/3.3.1... · 2020-02-20 · volatile write is visible to “happens-after” reads Overview of](https://reader034.vdocument.in/reader034/viewer/2022042322/5f0be57b7e708231d432c07b/html5/thumbnails/19.jpg)
19
Overview of Java Volatile Variables• Java defines the volatile keyword
to address these problems
• A value written to a volatile variable will always be stored in main memory
• An access to a volatile variable will be read from main memory
Main Memory
7 7
nv v
Cache 1
7 7
nv v
Cache 2
7 7
nv v
Cache n
42 13
nv v
ThreadnThread1 Thread2
read
v = 7
volatile reads are cheap & volatile writes are cheaper than synchronized statements
![Page 20: Java Volatile Variables: Introduction - Vanderbilt Universityschmidt/cs891s/2020-PDFs/3.3.1... · 2020-02-20 · volatile write is visible to “happens-after” reads Overview of](https://reader034.vdocument.in/reader034/viewer/2022042322/5f0be57b7e708231d432c07b/html5/thumbnails/20.jpg)
20
Overview of Java Volatile Variables• Volatile guarantees atomicity
See stackoverflow.com/questions/3038203/is-there-any-point-in-using-a-volatile-long
volatile long foo;
final long A = 0xffffffffffffffffl;
final long B = 0;
new Thread(() -> {
for (int i;; i++) {
foo = i % 2 == 0 ? A : B;
}}).start();
new Thread(() -> {
long fooRead = foo;
if (fooRead != A && fooRead != B)
System.err.println
("foo incomplete write "
+ Long.toHexString(fooRead));
}).start();
If volatile is removed here then incomplete writes may occur
(especially on 32 bit machines)
![Page 21: Java Volatile Variables: Introduction - Vanderbilt Universityschmidt/cs891s/2020-PDFs/3.3.1... · 2020-02-20 · volatile write is visible to “happens-after” reads Overview of](https://reader034.vdocument.in/reader034/viewer/2022042322/5f0be57b7e708231d432c07b/html5/thumbnails/21.jpg)
21
Overview of Java Volatile Variables• Volatile guarantees atomicity
• Reads & writes are atomic for all variables declared volatile
See docs.oracle.com/javase/tutorial/essential/concurrency/atomic.html
![Page 22: Java Volatile Variables: Introduction - Vanderbilt Universityschmidt/cs891s/2020-PDFs/3.3.1... · 2020-02-20 · volatile write is visible to “happens-after” reads Overview of](https://reader034.vdocument.in/reader034/viewer/2022042322/5f0be57b7e708231d432c07b/html5/thumbnails/22.jpg)
22
Overview of Java Volatile Variables• Volatile guarantees atomicity
• Reads & writes are atomic for all variables declared volatile
• Reads & writes are alwaysatomic for Java references
See docs.oracle.com/javase/tutorial/essential/concurrency/atomic.html
![Page 23: Java Volatile Variables: Introduction - Vanderbilt Universityschmidt/cs891s/2020-PDFs/3.3.1... · 2020-02-20 · volatile write is visible to “happens-after” reads Overview of](https://reader034.vdocument.in/reader034/viewer/2022042322/5f0be57b7e708231d432c07b/html5/thumbnails/23.jpg)
23
Overview of Java Volatile Variables• Volatile guarantees visibility
See jeremymanson.blogspot.com/2007/08/atomicity-visibility-and-ordering.html
public class MyRunnable
implements Runnable {
private volatile boolean
mIsStopped = false;
public void stopMe() {
mIsStopped = true;
}
public void run() {
while (mIsStopped != true) {
// a long-running operation
}
...
![Page 24: Java Volatile Variables: Introduction - Vanderbilt Universityschmidt/cs891s/2020-PDFs/3.3.1... · 2020-02-20 · volatile write is visible to “happens-after” reads Overview of](https://reader034.vdocument.in/reader034/viewer/2022042322/5f0be57b7e708231d432c07b/html5/thumbnails/24.jpg)
24
volatile write is visible to “happens-after” reads
Overview of Java Volatile Variables• Volatile guarantees visibility
• If an action in thread T1 isvisible to thread T2, the result of that action can be observed by thread T2
public class MyRunnable
implements Runnable {
private volatile boolean
mIsStopped = false;
public void stopMe() {
mIsStopped = true; // T1 write
}
public void run() { // T2 read
while (mIsStopped != true) {
// a long-running operation
}
...
![Page 25: Java Volatile Variables: Introduction - Vanderbilt Universityschmidt/cs891s/2020-PDFs/3.3.1... · 2020-02-20 · volatile write is visible to “happens-after” reads Overview of](https://reader034.vdocument.in/reader034/viewer/2022042322/5f0be57b7e708231d432c07b/html5/thumbnails/25.jpg)
25
Overview of Java Volatile Variables• Volatile guarantees ordering public class MyRunnable
implements Runnable {
private volatile boolean
mIsStopped = false;
public void stopMe() {
mIsStopped = true;
}
public void run() {
while (mIsStopped != true) {
// a long-running operation
}
...
See jeremymanson.blogspot.com/2007/08/atomicity-visibility-and-ordering.html
![Page 26: Java Volatile Variables: Introduction - Vanderbilt Universityschmidt/cs891s/2020-PDFs/3.3.1... · 2020-02-20 · volatile write is visible to “happens-after” reads Overview of](https://reader034.vdocument.in/reader034/viewer/2022042322/5f0be57b7e708231d432c07b/html5/thumbnails/26.jpg)
26
Overview of Java Volatile Variables• Volatile guarantees ordering
• Ordering constraints describe what order operations are seen to occur in different threads
public class MyRunnable
implements Runnable {
private volatile boolean
mIsStopped = false;
public void stopMe() {
mIsStopped = true; // T1 write
}
public void run() { // T2 read
while (mIsStopped != true) {
// a long-running operation
}
...
The write to mIsStopped in T1 must happen-before the T2 read completes
![Page 27: Java Volatile Variables: Introduction - Vanderbilt Universityschmidt/cs891s/2020-PDFs/3.3.1... · 2020-02-20 · volatile write is visible to “happens-after” reads Overview of](https://reader034.vdocument.in/reader034/viewer/2022042322/5f0be57b7e708231d432c07b/html5/thumbnails/27.jpg)
27
Overview of Java Volatile Variables• Incrementing a volatile is not atomic
Thread1 Thread2
volatile value
initialized 0
read value ← 0
read value ← 0
increase value by 2
0
increase value by 1
0
write back write back → 2 or 1?
![Page 28: Java Volatile Variables: Introduction - Vanderbilt Universityschmidt/cs891s/2020-PDFs/3.3.1... · 2020-02-20 · volatile write is visible to “happens-after” reads Overview of](https://reader034.vdocument.in/reader034/viewer/2022042322/5f0be57b7e708231d432c07b/html5/thumbnails/28.jpg)
28
Overview of Java Volatile Variables• Incrementing a volatile is not atomic
• If multiple threads try to increment a volatile at the same time, an update might get lost
Thread1 Thread2
volatile value
initialized 0
read value ← 0
read value ← 0
increase value by 2
0
increase value by 1
0
write back write back → 2 or 1?
Consider using the java.util.concurrent.atomic package, which supports atomic increment/decrement & compare-and-swap (CAS) operations
![Page 29: Java Volatile Variables: Introduction - Vanderbilt Universityschmidt/cs891s/2020-PDFs/3.3.1... · 2020-02-20 · volatile write is visible to “happens-after” reads Overview of](https://reader034.vdocument.in/reader034/viewer/2022042322/5f0be57b7e708231d432c07b/html5/thumbnails/29.jpg)
29
End of Volatile Variables: Introduction