SlideShare ist ein Scribd-Unternehmen logo
1 von 49
Downloaden Sie, um offline zu lesen
TS-754, Correct and Efficient Synchronization of Java Threads1
Correct and Efficient
Synchronization of
Java™ Technology-
based Threads
Doug Lea and William Pugh
http://gee.cs.oswego.edu
http://www.cs.umd.edu/~pugh
TS-754, Correct and Efficient Synchronization of Java Threads2
Audience
• Assume you are familiar with basics
of Java™ technology-based threads
(“Java threads”)
– Creating, starting and joining threads
– Synchronization
– wait and notifyAll
• Will talk about things that surprised a lot
of experts
– Including us, James Gosling, Guy Steele, …
(others discovered many of these)
TS-754, Correct and Efficient Synchronization of Java Threads3
Java Thread Specification
• Chapter 17 of the Java Language Spec
– Chapter 8 of the Virtual Machine Spec
• Very, very hard to understand
– Not even the authors understood it
– Has subtle implications
• That forbid standard compiler optimizations
– All existing JVMs violate the specification
• Some parts should be violated
TS-754, Correct and Efficient Synchronization of Java Threads4
Safety Issues in
Multithreaded Systems
• Many intuitive assumptions do not hold
• Some widely used idioms are not safe
– Double-check idiom
– Checking non-volatile flag for
thread termination
• Can’t use testing to check for errors
– Some anomalies will occur only on
some platforms
• e.g., multiprocessors
TS-754, Correct and Efficient Synchronization of Java Threads5
Revising the Thread Spec
• Work is underway to consider revising the
Java Thread Spec
– http://www.cs.umd.edu/~pugh/java/memoryModel
• Goals
– Clear and easy to understand
– Foster reliable multithreaded code
– Allow for high performance JVMs
• Will effect JVMs
– And badly written existing code
• Including parts of Sun’s JDK
TS-754, Correct and Efficient Synchronization of Java Threads6
What To Do Today?
• Guidelines we will provide should
work under both existing and future
thread specs
• Don’t try to read the official specs
• Avoid corner cases of the thread spec
– Not needed for efficient and reliable programs
TS-754, Correct and Efficient Synchronization of Java Threads7
Three Aspects of
Synchronization
• Atomicity
– Locking to obtain mutual exclusion
• Visibility
– Ensuring that changes to object fields made in
one thread are seen in other threads
• Ordering
– Ensuring that you aren’t surprised by the order
in which statements are executed
TS-754, Correct and Efficient Synchronization of Java Threads8
Don’t Be Too Clever
• People worry about the cost of
synchronization
– Try to devise schemes to communicate
between threads
• Without using synchronization
• Very difficult to do correctly
– Inter-thread communication without
synchronization is not intuitive
TS-754, Correct and Efficient Synchronization of Java Threads9
Quiz Time
x = y = 0
x = 1
j = y
Thread 1
y = 1
i = x
Thread 2
Can this result in i = 0 and j = 0?
start threads
TS-754, Correct and Efficient Synchronization of Java Threads10
Answer: Yes!
x = y = 0
x = 1
j = y
Thread 1
y = 1
i = x
Thread 2
How can i = 0 and j = 0?
start threads
TS-754, Correct and Efficient Synchronization of Java Threads11
How Can This Happen?
• Compiler can reorder statements
– Or keep values in registers
• Processor can reorder them
• On multi-processor, values not
synchronized in global memory
• Must use synchronization to enforce
visibility and ordering
– As well as mutual exclusion
TS-754, Correct and Efficient Synchronization of Java Threads12
Synchronization Actions
(approximately)
// block until obtain lock
synchronized(anObject) {
// get main memory value of field1 and field2
int x = anObject.field1;
int y = anObject.field2;
anObject.field3 = x+y;
// commit value of field3 to main memory
}
// release lock
moreCode();
TS-754, Correct and Efficient Synchronization of Java Threads13
When Are Actions Visible
to Other Threads?
x = 1
unlock M
Thread 1
lock M
i = x
Thread 2
TS-754, Correct and Efficient Synchronization of Java Threads14
What Does Volatile Mean?
• C/C++ spec
– There is no implementation independent
meaning of volatile
• Situation a little better with Java technology
– Volatile reads/writes guaranteed to go directly
to main memory
• Can’t be cached in registers or local memory
TS-754, Correct and Efficient Synchronization of Java Threads15
class Animator implements Runnable {
private volatile boolean stop = false;
public void stop() { stop = true; }
public void run() {
while (!stop)
oneStep();
}
private void oneStep() { /*...*/ }
}
Using Volatile
• Volatile used to guarantee visibility
of writes
– stop must be declared volatile
– Otherwise, compiler could keep in register
TS-754, Correct and Efficient Synchronization of Java Threads16
class Future {
private volatile boolean ready = false;
private Object data = null;
public Object get() {
if (!ready) return null;
return data;
}
// only one thread may ever call put
public void put(Object o) {
data = o;
ready = true;
}
}
Using Volatile to Guard
Other Fields Doesn’t Work
• Do not use - Does not work
TS-754, Correct and Efficient Synchronization of Java Threads17
Nobody Implements
Volatile Correctly
• Existing JVM requires sequential
consistency for volatile variables
– In quiz example, if x and y are volatile
– Should be impossible to see i = 0 and j = 0
– Haven’t found any JVMs that enforce it
• Reads/writes of volatile longs/doubles
– Guaranteed to be atomic
(see old or new value, not a mixture)
• Some JVMs ignore volatile flag
TS-754, Correct and Efficient Synchronization of Java Threads18
Volatile Compliance
No Compiler
Optimizations
Sequential
Consistency
Atomic
Longs/Doubles
Solaris Sparc
JDK 1.2.2 EVM
Pass Fail Pass
Solaris Sparc
JDK 1.3.0 beta
Hotspot Client
Fail Fail Fail
Windows
JDK 1.3.0
Hotspot Client
Fail Fail Fail
Solaris Sparc
JDK 1.3.0 beta
Hotspot Server
Pass Fail Fail
Windows
JDK 1.3.0
Hotspot Server
Pass Fail Fail
Windows IBM
JDK 1.1.8
Pass Fail Fail
TS-754, Correct and Efficient Synchronization of Java Threads19
Why Use Volatile?
• Since the semantics are implemented
inconsistently
• Future-proof your code
– Prohibit optimizations compilers might do
in the future
• Works well for flags
– More complicated uses are tricky
• Revising the thread spec...
– Test compliance
– Strengthen to make easier to use
TS-754, Correct and Efficient Synchronization of Java Threads20
Cost of Synchronization
• Few good public multithreaded
benchmarks
– See us if you want to help
• Volano Benchmark
– Most widely used server benchmark
– Multithreaded chat room server
– Client performs 4.8M synchronizations
• 8K useful (0.2%)
– Server 43M synchronizations
• 1.7M useful (4%)
TS-754, Correct and Efficient Synchronization of Java Threads21
Synchronization in
VolanoMark Client
90.3%
5.6%
1.8%
0.9%
0.9%
0.4%
0.2%
java.io.BufferedInputStream
java.io.BufferedOutputStream
java.util.Observable
java.util.Vector
java.io.FilterInputStream
everything else
All shared monitors
7,684 synchronizations on shared monitors
4,828,130 thread local synchronizations
TS-754, Correct and Efficient Synchronization of Java Threads22
Cost of Synchronization
in VolanoMark
• Removed synchronization of
– java.io.BufferedInputStream
– java.io.BufferedOutputStream
• Performance (2 processor Ultra 60)
– Larger is better
– HotSpot (1.3 beta)
• Original: 4788
• Altered: 4923 (+3%)
– Exact VM (1.2.2)
• Original: 6649
• Altered: 6874 (+3%)
TS-754, Correct and Efficient Synchronization of Java Threads23
Most Synchronization is on
Thread Local Objects
• Synchronization on thread local object
– Is useless
– Current spec says it isn’t quite a no-op
• But very hard to use usefully
– Revised spec will likely make it a no-op
• Largely arises from using synchronized
classes
– In places where not required
TS-754, Correct and Efficient Synchronization of Java Threads24
Synchronize When Needed
• Places where threads interact
– Need synchronization
– Need careful thought
– Need documentation
– Cost of required synchronization not significant
• For most applications
• No need to get tricky
• Elsewhere, using a synchronized class can
be expensive
TS-754, Correct and Efficient Synchronization of Java Threads25
Synchronized Classes
• Some classes are synchronized
– Vector, Hashtable, Stack
– Most Input/Output Streams
• Contrast with 1.2 Collection classes
– By default, not synchronized
– Can request synchronized version
• Using synchronized classes
– Often doesn’t suffice for concurrent interaction
TS-754, Correct and Efficient Synchronization of Java Threads26
Synchronized Collections
Aren’t Always Enough
• Transactions (DO NOT USE)
ID getID(String name) {
ID x = (ID) h.get(name);
if (x == null) {
x = new ID();
h.put(name, x);}
return x; }
• Iterators
– Can’t modify collection while another
thread is iterating through it
TS-754, Correct and Efficient Synchronization of Java Threads27
Concurrent Interactions
• Often need entire transactions to
be atomic
– Reading and updating a Map
– Writing a record to an OutputStream
• OutputStreams are synchronized
– Can have multiple threads trying to write to the
same OutputStream
– Output from each thread is nondeterministicly
interleaved
– Essentially useless
TS-754, Correct and Efficient Synchronization of Java Threads28
Cost of Synchronization in
SpecJVM DB Benchmark
• Program in the Spec JVM benchmark
• Does lots of synchronization
– > 53,000,000 syncs
• 99.9% comes from use of Vector
– Benchmark is single threaded, all of it
is useless
• Tried
– Remove synchronizations
– Switching to ArrayList
– Improving the algorithm
TS-754, Correct and Efficient Synchronization of Java Threads29
Execution Time of Spec JVM
_209_db, Hotspot Server
0
10
20
30
40
Original 35.5 32.6 28.5 16.2 12.8
Without Syncs 30.3 32.5 28.5 14.0 12.8
Original
Use
ArrayList
Use
ArrayList
and other
minor
Change
Shell Sort
to Merge
Sort
All
TS-754, Correct and Efficient Synchronization of Java Threads30
Lessons
• Synchronization cost can be substantial
– 10-20% for DB benchmark
– Consider replacing all uses of Vector,
Hashtable and Stack
• Use profiling
• Use better algorithms!
– Cost of stupidity higher than cost of
synchronization
– Used built-in merge sort rather than
hand-coded shell sort
TS-754, Correct and Efficient Synchronization of Java Threads31
Designing Fast Code
• Make it right before you make it fast
• Avoid synchronization
– Avoid sharing across threads
– Don’t lock already-protected objects
– Use immutable fields and objects
– Use volatile
• Avoid contention
– Reduce lock scopes
– Reduce lock durations
TS-754, Correct and Efficient Synchronization of Java Threads32
Isolation in Java™ Foundation
Classes (JFC)/Swing
• JFC/Swing relies entirely on Isolation
– AWT thread owns all Swing components
• No other thread may access them
– Eliminates need for locking
• Still need care during initialization
• Can be fragile
– Every programmer must obey rules
– Rules are usually easy to follow
– Most Swing components accessed in
handlers triggered within AWT thread
TS-754, Correct and Efficient Synchronization of Java Threads33
Accessing Isolated Objects
• Need safe inter-thread communication
– Swing uses via runnable Event objects
• Created by some other thread
• Serviced by AWT thread
SwingUtilities.invokeLater(new Runnable(){
public void run() {
statusMessage.setText("Running");
}});
TS-754, Correct and Efficient Synchronization of Java Threads34
GetX/SetX Access Methods
• Not synchronizing access methods
– int thermometer.getTemperature()
(doesn’t work for references)
• Synchronizing access methods
– account.getTotalBalance()
• Omitting access methods
– queue doesn’t need getSize()
TS-754, Correct and Efficient Synchronization of Java Threads35
Things That Don’t Work
• Double-Check Idiom
– Also, unsynchronized reads/writes of refs
• Non-volatile flags
• Depending on sleep for visibility
TS-754, Correct and Efficient Synchronization of Java Threads36
Initialization Check - v1 - OK
Basic version:
class Service {
Parser parser = null;
public synchronized void command() {
if (parser == null)
parser = new Parser(...);
doCommand(parser.parse(...));
}
// ...
}
TS-754, Correct and Efficient Synchronization of Java Threads37
Initialization checks - v2 - OK
Isolate check:
class ServiceV2 {
Parser parser = null;
synchronized Parser getParser() {
if (parser == null)
parser = new Parser();
return parser;
}
public void command(...) {
doCommand(getParser().parse(...));
}}
TS-754, Correct and Efficient Synchronization of Java Threads38
Single-check - DO NOT USE
Try to do it without synchronization:
class ServiceV3 { // DO NOT USE
Parser parser = null;
Parser getParser() {
if (parser == null)
parser = new Parser();
return parser;
}}
TS-754, Correct and Efficient Synchronization of Java Threads39
Double-check - DO NOT USE
Try to minimize likelihood of synch:
class ServiceV4 { // DO NOT USE
Parser parser = null;
Parser getParser() {
if (parser == null)
synchronized(this) {
if (parser == null)
parser = new Parser();
}
return parser;
}}
TS-754, Correct and Efficient Synchronization of Java Threads40
Problems with Double-check
• Can reorder
– Initialization of Parser object
– Store into parser field
• …Among other reasons
– See JMM web page for gory details
• Can go wrong on uniprocessors
– e.g., Symantic JIT
• Using volatile doesn’t help
– Under current JMM
TS-754, Correct and Efficient Synchronization of Java Threads41
Alternatives to
Double–Check
• Use synchronization
• Double check OK for primitive values
– hashCode caching
(still technically a data race)
• For static singletons
– Put in separate class
– First use of a class forces class initialization
– Later uses guaranteed to see class initialization
– No explicit check needed
TS-754, Correct and Efficient Synchronization of Java Threads42
Rare Heavy New Objects
• Sometimes, need singleton that is
expensive to create
static final Font HELVETICA
= new FONT(“Helvetica”,Font.PLAIN, 24);
Font getFont() {
if (!chinese)
return HELVETICA;
else
return new ChineseFont();
}
TS-754, Correct and Efficient Synchronization of Java Threads43
Using Static Singletons
static final Font HELVETICA
= new Font(“Helvetica”,Font.PLAIN,24);
static class CFSingleton{
static final Font CHINESE
= new ChineseFont(...);
}
Font getFont() {
if (!chinese)
return HELVETICA;
else
return CFSingleton.CHINESE;
}
TS-754, Correct and Efficient Synchronization of Java Threads44
Unsynchronized
Reads/Writes of References
• Beware of unsynchronized getX/setX
methods that return a reference
– Same problems as double check
– Doesn’t help to synchronize only setX
private Color color;
void setColor(int rgb) {
color = new Color(rgb);
}
Color getColor() {
return color;
}
TS-754, Correct and Efficient Synchronization of Java Threads45
Thread blinker = null;
public void start() {
blinker = new Thread(this);
blinker.start();
}
public void stop() {
blinker = null;}
public void run() {
Thread me = Thread.currentThread();
while (blinker == me) {
try {Thread.currentThread().sleep(delay);}
catch (InterruptedException e) {}
repaint();
}
}
Thread Termination in
Sun’s Demo Applets
unsynchronized access to blinker field
confusing but not wrong: sleep is a static method
TS-754, Correct and Efficient Synchronization of Java Threads46
Problems
• Don’t assume another thread will see
your writes
– Just because you did them
• Calling sleep doesn’t guarantee you see
changes made while you slept
– Nothing to force thread that called stop to
push change out of registers/cache
TS-754, Correct and Efficient Synchronization of Java Threads47
Wrap-up
• Cost of synchronization operations
can be significant
– But cost of needed synchronization rarely is
• Thread interaction needs careful thought
– But not too clever
• Need for synchronization...
TS-754, Correct and Efficient Synchronization of Java Threads48
Wrapup - Synchronization
• Communication between threads
– Requires both threads to synchronize
• Or communication through volatile fields
• Synchronizing everything
– Is rarely necessary
– Can be expensive (3%-20% overhead)
– May lead to deadlock
– May not provide enough synchronization
• e.g., transactions
TS-754, Correct and Efficient Synchronization of Java Threads49

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
Thread presentation
Thread presentationThread presentation
Thread presentation
 
Thread
ThreadThread
Thread
 
Concurrency in java
Concurrency in javaConcurrency in java
Concurrency in java
 
Java
JavaJava
Java
 
Chap2 2 1
Chap2 2 1Chap2 2 1
Chap2 2 1
 
Multithreading Concepts
Multithreading ConceptsMultithreading Concepts
Multithreading Concepts
 
multithreading
multithreadingmultithreading
multithreading
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Java Multi Thead Programming
Java Multi Thead ProgrammingJava Multi Thead Programming
Java Multi Thead Programming
 
Java
JavaJava
Java
 
Multithreading 101
Multithreading 101Multithreading 101
Multithreading 101
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Multithread Programing in Java
Multithread Programing in JavaMultithread Programing in Java
Multithread Programing in Java
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
 
.NET Multithreading/Multitasking
.NET Multithreading/Multitasking.NET Multithreading/Multitasking
.NET Multithreading/Multitasking
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
multi threading
multi threadingmulti threading
multi threading
 
Multithreading in-java
Multithreading in-javaMultithreading in-java
Multithreading in-java
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
 

Andere mochten auch

Java Code For Sample Projects I/O
Java Code For Sample Projects I/OJava Code For Sample Projects I/O
Java Code For Sample Projects I/Ojwjablonski
 
6장 Thread Synchronization
6장 Thread Synchronization6장 Thread Synchronization
6장 Thread Synchronization김 한도
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Javaparag
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaRaghu nath
 

Andere mochten auch (7)

Java Code For Sample Projects I/O
Java Code For Sample Projects I/OJava Code For Sample Projects I/O
Java Code For Sample Projects I/O
 
Thread lifecycle
Thread lifecycleThread lifecycle
Thread lifecycle
 
Linkedin Search_GK
Linkedin Search_GKLinkedin Search_GK
Linkedin Search_GK
 
6장 Thread Synchronization
6장 Thread Synchronization6장 Thread Synchronization
6장 Thread Synchronization
 
My History
My HistoryMy History
My History
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 

Ähnlich wie Correct and efficient synchronization of java thread

Modern Java Concurrency (OSCON 2012)
Modern Java Concurrency (OSCON 2012)Modern Java Concurrency (OSCON 2012)
Modern Java Concurrency (OSCON 2012)Martijn Verburg
 
Concurrency and Multithreading Demistified - Reversim Summit 2014
Concurrency and Multithreading Demistified - Reversim Summit 2014Concurrency and Multithreading Demistified - Reversim Summit 2014
Concurrency and Multithreading Demistified - Reversim Summit 2014Haim Yadid
 
Notes from 2016 bay area deep learning school
Notes from 2016 bay area deep learning school Notes from 2016 bay area deep learning school
Notes from 2016 bay area deep learning school Niketan Pansare
 
Multithreaded Programming Part- II.pdf
Multithreaded Programming Part- II.pdfMultithreaded Programming Part- II.pdf
Multithreaded Programming Part- II.pdfHarika Pudugosula
 
Scala in practice
Scala in practiceScala in practice
Scala in practiceTomer Gabel
 
TDD and the Legacy Code Black Hole
TDD and the Legacy Code Black HoleTDD and the Legacy Code Black Hole
TDD and the Legacy Code Black HoleNoam Kfir
 
WebLogic Stability; Detect and Analyse Stuck Threads
WebLogic Stability; Detect and Analyse Stuck ThreadsWebLogic Stability; Detect and Analyse Stuck Threads
WebLogic Stability; Detect and Analyse Stuck ThreadsMaarten Smeets
 
Here comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdfHere comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdfKrystian Zybała
 
Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8Simon Ritter
 
Scala in the Wild
Scala in the WildScala in the Wild
Scala in the WildTomer Gabel
 
JavaOne 2010: Top 10 Causes for Java Issues in Production and What to Do When...
JavaOne 2010: Top 10 Causes for Java Issues in Production and What to Do When...JavaOne 2010: Top 10 Causes for Java Issues in Production and What to Do When...
JavaOne 2010: Top 10 Causes for Java Issues in Production and What to Do When...srisatish ambati
 
Java Serialization Facts and Fallacies
Java Serialization Facts and FallaciesJava Serialization Facts and Fallacies
Java Serialization Facts and FallaciesRoman Elizarov
 
Using galera replication to create geo distributed clusters on the wan
Using galera replication to create geo distributed clusters on the wanUsing galera replication to create geo distributed clusters on the wan
Using galera replication to create geo distributed clusters on the wanSakari Keskitalo
 
Using galera replication to create geo distributed clusters on the wan
Using galera replication to create geo distributed clusters on the wanUsing galera replication to create geo distributed clusters on the wan
Using galera replication to create geo distributed clusters on the wanSakari Keskitalo
 
Ruby and Distributed Storage Systems
Ruby and Distributed Storage SystemsRuby and Distributed Storage Systems
Ruby and Distributed Storage SystemsSATOSHI TAGOMORI
 

Ähnlich wie Correct and efficient synchronization of java thread (20)

Modern Java Concurrency (OSCON 2012)
Modern Java Concurrency (OSCON 2012)Modern Java Concurrency (OSCON 2012)
Modern Java Concurrency (OSCON 2012)
 
Concurrency and Multithreading Demistified - Reversim Summit 2014
Concurrency and Multithreading Demistified - Reversim Summit 2014Concurrency and Multithreading Demistified - Reversim Summit 2014
Concurrency and Multithreading Demistified - Reversim Summit 2014
 
Notes from 2016 bay area deep learning school
Notes from 2016 bay area deep learning school Notes from 2016 bay area deep learning school
Notes from 2016 bay area deep learning school
 
Multithreaded Programming Part- II.pdf
Multithreaded Programming Part- II.pdfMultithreaded Programming Part- II.pdf
Multithreaded Programming Part- II.pdf
 
Scala in practice
Scala in practiceScala in practice
Scala in practice
 
Dal deck
Dal deckDal deck
Dal deck
 
TDD and the Legacy Code Black Hole
TDD and the Legacy Code Black HoleTDD and the Legacy Code Black Hole
TDD and the Legacy Code Black Hole
 
Java and the JVM
Java and the JVMJava and the JVM
Java and the JVM
 
WebLogic Stability; Detect and Analyse Stuck Threads
WebLogic Stability; Detect and Analyse Stuck ThreadsWebLogic Stability; Detect and Analyse Stuck Threads
WebLogic Stability; Detect and Analyse Stuck Threads
 
gcdtmp
gcdtmpgcdtmp
gcdtmp
 
Here comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdfHere comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdf
 
Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8
 
Scala in the Wild
Scala in the WildScala in the Wild
Scala in the Wild
 
JavaOne 2010: Top 10 Causes for Java Issues in Production and What to Do When...
JavaOne 2010: Top 10 Causes for Java Issues in Production and What to Do When...JavaOne 2010: Top 10 Causes for Java Issues in Production and What to Do When...
JavaOne 2010: Top 10 Causes for Java Issues in Production and What to Do When...
 
Java Serialization Facts and Fallacies
Java Serialization Facts and FallaciesJava Serialization Facts and Fallacies
Java Serialization Facts and Fallacies
 
Using galera replication to create geo distributed clusters on the wan
Using galera replication to create geo distributed clusters on the wanUsing galera replication to create geo distributed clusters on the wan
Using galera replication to create geo distributed clusters on the wan
 
Using galera replication to create geo distributed clusters on the wan
Using galera replication to create geo distributed clusters on the wanUsing galera replication to create geo distributed clusters on the wan
Using galera replication to create geo distributed clusters on the wan
 
Using galera replication to create geo distributed clusters on the wan
Using galera replication to create geo distributed clusters on the wanUsing galera replication to create geo distributed clusters on the wan
Using galera replication to create geo distributed clusters on the wan
 
Cloud Orchestration is Broken
Cloud Orchestration is BrokenCloud Orchestration is Broken
Cloud Orchestration is Broken
 
Ruby and Distributed Storage Systems
Ruby and Distributed Storage SystemsRuby and Distributed Storage Systems
Ruby and Distributed Storage Systems
 

Kürzlich hochgeladen

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
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
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
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
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 

Kürzlich hochgeladen (20)

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
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
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
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
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 

Correct and efficient synchronization of java thread

  • 1. TS-754, Correct and Efficient Synchronization of Java Threads1 Correct and Efficient Synchronization of Java™ Technology- based Threads Doug Lea and William Pugh http://gee.cs.oswego.edu http://www.cs.umd.edu/~pugh
  • 2. TS-754, Correct and Efficient Synchronization of Java Threads2 Audience • Assume you are familiar with basics of Java™ technology-based threads (“Java threads”) – Creating, starting and joining threads – Synchronization – wait and notifyAll • Will talk about things that surprised a lot of experts – Including us, James Gosling, Guy Steele, … (others discovered many of these)
  • 3. TS-754, Correct and Efficient Synchronization of Java Threads3 Java Thread Specification • Chapter 17 of the Java Language Spec – Chapter 8 of the Virtual Machine Spec • Very, very hard to understand – Not even the authors understood it – Has subtle implications • That forbid standard compiler optimizations – All existing JVMs violate the specification • Some parts should be violated
  • 4. TS-754, Correct and Efficient Synchronization of Java Threads4 Safety Issues in Multithreaded Systems • Many intuitive assumptions do not hold • Some widely used idioms are not safe – Double-check idiom – Checking non-volatile flag for thread termination • Can’t use testing to check for errors – Some anomalies will occur only on some platforms • e.g., multiprocessors
  • 5. TS-754, Correct and Efficient Synchronization of Java Threads5 Revising the Thread Spec • Work is underway to consider revising the Java Thread Spec – http://www.cs.umd.edu/~pugh/java/memoryModel • Goals – Clear and easy to understand – Foster reliable multithreaded code – Allow for high performance JVMs • Will effect JVMs – And badly written existing code • Including parts of Sun’s JDK
  • 6. TS-754, Correct and Efficient Synchronization of Java Threads6 What To Do Today? • Guidelines we will provide should work under both existing and future thread specs • Don’t try to read the official specs • Avoid corner cases of the thread spec – Not needed for efficient and reliable programs
  • 7. TS-754, Correct and Efficient Synchronization of Java Threads7 Three Aspects of Synchronization • Atomicity – Locking to obtain mutual exclusion • Visibility – Ensuring that changes to object fields made in one thread are seen in other threads • Ordering – Ensuring that you aren’t surprised by the order in which statements are executed
  • 8. TS-754, Correct and Efficient Synchronization of Java Threads8 Don’t Be Too Clever • People worry about the cost of synchronization – Try to devise schemes to communicate between threads • Without using synchronization • Very difficult to do correctly – Inter-thread communication without synchronization is not intuitive
  • 9. TS-754, Correct and Efficient Synchronization of Java Threads9 Quiz Time x = y = 0 x = 1 j = y Thread 1 y = 1 i = x Thread 2 Can this result in i = 0 and j = 0? start threads
  • 10. TS-754, Correct and Efficient Synchronization of Java Threads10 Answer: Yes! x = y = 0 x = 1 j = y Thread 1 y = 1 i = x Thread 2 How can i = 0 and j = 0? start threads
  • 11. TS-754, Correct and Efficient Synchronization of Java Threads11 How Can This Happen? • Compiler can reorder statements – Or keep values in registers • Processor can reorder them • On multi-processor, values not synchronized in global memory • Must use synchronization to enforce visibility and ordering – As well as mutual exclusion
  • 12. TS-754, Correct and Efficient Synchronization of Java Threads12 Synchronization Actions (approximately) // block until obtain lock synchronized(anObject) { // get main memory value of field1 and field2 int x = anObject.field1; int y = anObject.field2; anObject.field3 = x+y; // commit value of field3 to main memory } // release lock moreCode();
  • 13. TS-754, Correct and Efficient Synchronization of Java Threads13 When Are Actions Visible to Other Threads? x = 1 unlock M Thread 1 lock M i = x Thread 2
  • 14. TS-754, Correct and Efficient Synchronization of Java Threads14 What Does Volatile Mean? • C/C++ spec – There is no implementation independent meaning of volatile • Situation a little better with Java technology – Volatile reads/writes guaranteed to go directly to main memory • Can’t be cached in registers or local memory
  • 15. TS-754, Correct and Efficient Synchronization of Java Threads15 class Animator implements Runnable { private volatile boolean stop = false; public void stop() { stop = true; } public void run() { while (!stop) oneStep(); } private void oneStep() { /*...*/ } } Using Volatile • Volatile used to guarantee visibility of writes – stop must be declared volatile – Otherwise, compiler could keep in register
  • 16. TS-754, Correct and Efficient Synchronization of Java Threads16 class Future { private volatile boolean ready = false; private Object data = null; public Object get() { if (!ready) return null; return data; } // only one thread may ever call put public void put(Object o) { data = o; ready = true; } } Using Volatile to Guard Other Fields Doesn’t Work • Do not use - Does not work
  • 17. TS-754, Correct and Efficient Synchronization of Java Threads17 Nobody Implements Volatile Correctly • Existing JVM requires sequential consistency for volatile variables – In quiz example, if x and y are volatile – Should be impossible to see i = 0 and j = 0 – Haven’t found any JVMs that enforce it • Reads/writes of volatile longs/doubles – Guaranteed to be atomic (see old or new value, not a mixture) • Some JVMs ignore volatile flag
  • 18. TS-754, Correct and Efficient Synchronization of Java Threads18 Volatile Compliance No Compiler Optimizations Sequential Consistency Atomic Longs/Doubles Solaris Sparc JDK 1.2.2 EVM Pass Fail Pass Solaris Sparc JDK 1.3.0 beta Hotspot Client Fail Fail Fail Windows JDK 1.3.0 Hotspot Client Fail Fail Fail Solaris Sparc JDK 1.3.0 beta Hotspot Server Pass Fail Fail Windows JDK 1.3.0 Hotspot Server Pass Fail Fail Windows IBM JDK 1.1.8 Pass Fail Fail
  • 19. TS-754, Correct and Efficient Synchronization of Java Threads19 Why Use Volatile? • Since the semantics are implemented inconsistently • Future-proof your code – Prohibit optimizations compilers might do in the future • Works well for flags – More complicated uses are tricky • Revising the thread spec... – Test compliance – Strengthen to make easier to use
  • 20. TS-754, Correct and Efficient Synchronization of Java Threads20 Cost of Synchronization • Few good public multithreaded benchmarks – See us if you want to help • Volano Benchmark – Most widely used server benchmark – Multithreaded chat room server – Client performs 4.8M synchronizations • 8K useful (0.2%) – Server 43M synchronizations • 1.7M useful (4%)
  • 21. TS-754, Correct and Efficient Synchronization of Java Threads21 Synchronization in VolanoMark Client 90.3% 5.6% 1.8% 0.9% 0.9% 0.4% 0.2% java.io.BufferedInputStream java.io.BufferedOutputStream java.util.Observable java.util.Vector java.io.FilterInputStream everything else All shared monitors 7,684 synchronizations on shared monitors 4,828,130 thread local synchronizations
  • 22. TS-754, Correct and Efficient Synchronization of Java Threads22 Cost of Synchronization in VolanoMark • Removed synchronization of – java.io.BufferedInputStream – java.io.BufferedOutputStream • Performance (2 processor Ultra 60) – Larger is better – HotSpot (1.3 beta) • Original: 4788 • Altered: 4923 (+3%) – Exact VM (1.2.2) • Original: 6649 • Altered: 6874 (+3%)
  • 23. TS-754, Correct and Efficient Synchronization of Java Threads23 Most Synchronization is on Thread Local Objects • Synchronization on thread local object – Is useless – Current spec says it isn’t quite a no-op • But very hard to use usefully – Revised spec will likely make it a no-op • Largely arises from using synchronized classes – In places where not required
  • 24. TS-754, Correct and Efficient Synchronization of Java Threads24 Synchronize When Needed • Places where threads interact – Need synchronization – Need careful thought – Need documentation – Cost of required synchronization not significant • For most applications • No need to get tricky • Elsewhere, using a synchronized class can be expensive
  • 25. TS-754, Correct and Efficient Synchronization of Java Threads25 Synchronized Classes • Some classes are synchronized – Vector, Hashtable, Stack – Most Input/Output Streams • Contrast with 1.2 Collection classes – By default, not synchronized – Can request synchronized version • Using synchronized classes – Often doesn’t suffice for concurrent interaction
  • 26. TS-754, Correct and Efficient Synchronization of Java Threads26 Synchronized Collections Aren’t Always Enough • Transactions (DO NOT USE) ID getID(String name) { ID x = (ID) h.get(name); if (x == null) { x = new ID(); h.put(name, x);} return x; } • Iterators – Can’t modify collection while another thread is iterating through it
  • 27. TS-754, Correct and Efficient Synchronization of Java Threads27 Concurrent Interactions • Often need entire transactions to be atomic – Reading and updating a Map – Writing a record to an OutputStream • OutputStreams are synchronized – Can have multiple threads trying to write to the same OutputStream – Output from each thread is nondeterministicly interleaved – Essentially useless
  • 28. TS-754, Correct and Efficient Synchronization of Java Threads28 Cost of Synchronization in SpecJVM DB Benchmark • Program in the Spec JVM benchmark • Does lots of synchronization – > 53,000,000 syncs • 99.9% comes from use of Vector – Benchmark is single threaded, all of it is useless • Tried – Remove synchronizations – Switching to ArrayList – Improving the algorithm
  • 29. TS-754, Correct and Efficient Synchronization of Java Threads29 Execution Time of Spec JVM _209_db, Hotspot Server 0 10 20 30 40 Original 35.5 32.6 28.5 16.2 12.8 Without Syncs 30.3 32.5 28.5 14.0 12.8 Original Use ArrayList Use ArrayList and other minor Change Shell Sort to Merge Sort All
  • 30. TS-754, Correct and Efficient Synchronization of Java Threads30 Lessons • Synchronization cost can be substantial – 10-20% for DB benchmark – Consider replacing all uses of Vector, Hashtable and Stack • Use profiling • Use better algorithms! – Cost of stupidity higher than cost of synchronization – Used built-in merge sort rather than hand-coded shell sort
  • 31. TS-754, Correct and Efficient Synchronization of Java Threads31 Designing Fast Code • Make it right before you make it fast • Avoid synchronization – Avoid sharing across threads – Don’t lock already-protected objects – Use immutable fields and objects – Use volatile • Avoid contention – Reduce lock scopes – Reduce lock durations
  • 32. TS-754, Correct and Efficient Synchronization of Java Threads32 Isolation in Java™ Foundation Classes (JFC)/Swing • JFC/Swing relies entirely on Isolation – AWT thread owns all Swing components • No other thread may access them – Eliminates need for locking • Still need care during initialization • Can be fragile – Every programmer must obey rules – Rules are usually easy to follow – Most Swing components accessed in handlers triggered within AWT thread
  • 33. TS-754, Correct and Efficient Synchronization of Java Threads33 Accessing Isolated Objects • Need safe inter-thread communication – Swing uses via runnable Event objects • Created by some other thread • Serviced by AWT thread SwingUtilities.invokeLater(new Runnable(){ public void run() { statusMessage.setText("Running"); }});
  • 34. TS-754, Correct and Efficient Synchronization of Java Threads34 GetX/SetX Access Methods • Not synchronizing access methods – int thermometer.getTemperature() (doesn’t work for references) • Synchronizing access methods – account.getTotalBalance() • Omitting access methods – queue doesn’t need getSize()
  • 35. TS-754, Correct and Efficient Synchronization of Java Threads35 Things That Don’t Work • Double-Check Idiom – Also, unsynchronized reads/writes of refs • Non-volatile flags • Depending on sleep for visibility
  • 36. TS-754, Correct and Efficient Synchronization of Java Threads36 Initialization Check - v1 - OK Basic version: class Service { Parser parser = null; public synchronized void command() { if (parser == null) parser = new Parser(...); doCommand(parser.parse(...)); } // ... }
  • 37. TS-754, Correct and Efficient Synchronization of Java Threads37 Initialization checks - v2 - OK Isolate check: class ServiceV2 { Parser parser = null; synchronized Parser getParser() { if (parser == null) parser = new Parser(); return parser; } public void command(...) { doCommand(getParser().parse(...)); }}
  • 38. TS-754, Correct and Efficient Synchronization of Java Threads38 Single-check - DO NOT USE Try to do it without synchronization: class ServiceV3 { // DO NOT USE Parser parser = null; Parser getParser() { if (parser == null) parser = new Parser(); return parser; }}
  • 39. TS-754, Correct and Efficient Synchronization of Java Threads39 Double-check - DO NOT USE Try to minimize likelihood of synch: class ServiceV4 { // DO NOT USE Parser parser = null; Parser getParser() { if (parser == null) synchronized(this) { if (parser == null) parser = new Parser(); } return parser; }}
  • 40. TS-754, Correct and Efficient Synchronization of Java Threads40 Problems with Double-check • Can reorder – Initialization of Parser object – Store into parser field • …Among other reasons – See JMM web page for gory details • Can go wrong on uniprocessors – e.g., Symantic JIT • Using volatile doesn’t help – Under current JMM
  • 41. TS-754, Correct and Efficient Synchronization of Java Threads41 Alternatives to Double–Check • Use synchronization • Double check OK for primitive values – hashCode caching (still technically a data race) • For static singletons – Put in separate class – First use of a class forces class initialization – Later uses guaranteed to see class initialization – No explicit check needed
  • 42. TS-754, Correct and Efficient Synchronization of Java Threads42 Rare Heavy New Objects • Sometimes, need singleton that is expensive to create static final Font HELVETICA = new FONT(“Helvetica”,Font.PLAIN, 24); Font getFont() { if (!chinese) return HELVETICA; else return new ChineseFont(); }
  • 43. TS-754, Correct and Efficient Synchronization of Java Threads43 Using Static Singletons static final Font HELVETICA = new Font(“Helvetica”,Font.PLAIN,24); static class CFSingleton{ static final Font CHINESE = new ChineseFont(...); } Font getFont() { if (!chinese) return HELVETICA; else return CFSingleton.CHINESE; }
  • 44. TS-754, Correct and Efficient Synchronization of Java Threads44 Unsynchronized Reads/Writes of References • Beware of unsynchronized getX/setX methods that return a reference – Same problems as double check – Doesn’t help to synchronize only setX private Color color; void setColor(int rgb) { color = new Color(rgb); } Color getColor() { return color; }
  • 45. TS-754, Correct and Efficient Synchronization of Java Threads45 Thread blinker = null; public void start() { blinker = new Thread(this); blinker.start(); } public void stop() { blinker = null;} public void run() { Thread me = Thread.currentThread(); while (blinker == me) { try {Thread.currentThread().sleep(delay);} catch (InterruptedException e) {} repaint(); } } Thread Termination in Sun’s Demo Applets unsynchronized access to blinker field confusing but not wrong: sleep is a static method
  • 46. TS-754, Correct and Efficient Synchronization of Java Threads46 Problems • Don’t assume another thread will see your writes – Just because you did them • Calling sleep doesn’t guarantee you see changes made while you slept – Nothing to force thread that called stop to push change out of registers/cache
  • 47. TS-754, Correct and Efficient Synchronization of Java Threads47 Wrap-up • Cost of synchronization operations can be significant – But cost of needed synchronization rarely is • Thread interaction needs careful thought – But not too clever • Need for synchronization...
  • 48. TS-754, Correct and Efficient Synchronization of Java Threads48 Wrapup - Synchronization • Communication between threads – Requires both threads to synchronize • Or communication through volatile fields • Synchronizing everything – Is rarely necessary – Can be expensive (3%-20% overhead) – May lead to deadlock – May not provide enough synchronization • e.g., transactions
  • 49. TS-754, Correct and Efficient Synchronization of Java Threads49