Java Platform Concurrency Gotchas

Similar documents
Common Java Concurrency pitfalls. Presented by : Kunal Sinha Austin Java Users Group 03/26/2019

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

CMSC 330: Organization of Programming Languages. The Dining Philosophers Problem

The Java Memory Model

CMSC 330: Organization of Programming Languages. Threads Classic Concurrency Problems

Problems with Concurrency. February 19, 2014

Threads and Locks, Part 2. CSCI 5828: Foundations of Software Engineering Lecture 08 09/18/2014

Java Threads Vs Processes. CS Concurrent Programming. Java Threads. What can a thread do? Java Concurrency

Exercise Session Week 8

Practical Concurrency. Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

The New Java Technology Memory Model

Sharing Objects Ch. 3

Exercise Session Week 8

CMSC 433 Programming Language Technologies and Paradigms. Spring 2013

CMSC 433 Programming Language Technologies and Paradigms. Sharing Objects

More thread safety and Concurrent collections. Nasser Giacaman CompSci 230: Software Design and Construction

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

COMPSCI 230 Threading Week8. Figure 1 Thread status diagram [

Buffer & File Optimization. Cloud Databases DataLab CS, NTHU

Recap. Contents. Reenterancy of synchronized. Explicit Locks: ReentrantLock. Reenterancy of synchronise (ctd) Advanced Thread programming.

Threads Questions Important Questions

CS 351 Design of Large Programs Singleton Pattern

Overview. Processes vs. Threads. Computation Abstractions. CMSC 433, Fall Michael Hicks 1

Java Concurrency. Towards a better life By - -

Programming Paradigms for Concurrency Lecture 3 Concurrent Objects

Concurrency and High Performance Reloaded

CMSC 433 Spring 2013 Exam 1

A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 5 Programming with Locks and Critical Sections

What is it? CMSC 433 Programming Language Technologies and Paradigms Spring Approach 1. Disadvantage of Approach 1

CSE332: Data Abstractions Lecture 23: Programming with Locks and Critical Sections. Tyler Robison Summer 2010

Thread Safety. Review. Today o Confinement o Threadsafe datatypes Required reading. Concurrency Wrapper Collections

Northeastern University - Seattle CS5510 Professor Ian Gorton

Chair of Software Engineering. Java and C# in Depth. Prof. Dr. Bertrand Meyer. Exercise Session 8. Nadia Polikarpova

Principles of Software Construction: Objects, Design and Concurrency. The Perils of Concurrency, Part 2 Can't live with it. Cant live without it.

CMSC 132: Object-Oriented Programming II. Threads in Java

CMSC 330: Organization of Programming Languages

Midterm #1. CMSC 433: Programming Language Technologies and Paradigms. October 14, 2013

G52CON: Concepts of Concurrency

Principles of Software Construction: Objects, Design and Concurrency. The Perils of Concurrency, Part 2 (Can't live with it, can't live without it.

ECE 587 Hardware/Software Co-Design Lecture 07 Concurrency in Practice Shared Memory I

Atomicity CS 2110 Fall 2017

CS180 Review. Recitation Week 15

Threads Chate Patanothai

Techniques of Java Programming: Concurrent Programming in Java

Threads and Java Memory Model

Design of Thread-Safe Classes

F. Tip and M. Weintraub CONCURRENCY

Chair of Software Engineering. Languages in Depth Series: Java Programming. Prof. Dr. Bertrand Meyer. Exercise Session 10

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

Synchronization in Java

Interview Questions I received in 2017 and 2018

Principles of Software Construction: Concurrency, Part 2

CS 159: Parallel Processing

Lecture 17: Sharing Objects in Java

+ Today. Lecture 26: Concurrency 3/31/14. n Reading. n Objectives. n Announcements. n P&C Section 7. n Race conditions.

Persistent Data Structures and Managed References

Introduction to Programming Using Java (98-388)

JAVA MOCK TEST JAVA MOCK TEST II

Advanced concurrent programming in Java Shared objects

Races. Example. A race condi-on occurs when the computa-on result depends on scheduling (how threads are interleaved)

The Singleton Pattern. Design Patterns In Java Bob Tarr

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

IntelliJ IDEA Static Code Analysis Hamlet D'Arcy

Threads and Locks. CSCI 5828: Foundations of Software Engineering Lecture 09 09/22/2015

CS 2112 Lecture 20 Synchronization 5 April 2012 Lecturer: Andrew Myers

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Get out, you will, of this bind If, your objects, you have confined

Efficient Java (with Stratosphere) Arvid Heise, Large Scale Duplicate Detection

MultiThreading 07/01/2013. Session objectives. Introduction. Introduction. Advanced Java Programming Course

Dr.-Ing. Michael Eichberg

Menu. In general, what are possible advantages of one design over another? Class 27: Exam 2. Exam 2 Parenthesizing the Expression. Design A.

Advanced Java Programming Course. MultiThreading. By Võ Văn Hải Faculty of Information Technologies Industrial University of Ho Chi Minh City

Advanced Java Multithreading Techniques

Concurrency WS 2010/2011 The Java Memory Model

Principles of Software Construction: Concurrency, Part 2

Singleton. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 29

The Java Memory Model

CS18000: Programming I

Models of concurrency & synchronization algorithms

Computation Abstractions. CMSC 330: Organization of Programming Languages. So, What Is a Thread? Processes vs. Threads. A computer.

SOLID + CleanCode. Design objektorientierter Systeme

Domain-Driven Design Activity

Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Marco Piccioni, Bertrand Meyer. Java: concurrency

Enough java.lang.string to Hang Ourselves...

Java HashMap Interview Questions

Learning from Bad Examples. CSCI 5828: Foundations of Software Engineering Lecture 25 11/18/2014

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

Identity, State and Values

CMSC 433 Programming Language Technologies and Paradigms. Spring 2013

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

CS 1302 Chapter 9 (Review) Object & Classes

CMSC 330: Organization of Programming Languages

Problems with Concurrency

Programmazione di sistemi multicore

Effective Concurrent Java. Brian Goetz Sr. Staff Engineer, Sun Microsystems

Lecture 7 Java SE Multithreading

Question Points Score Total 100

Josh Bloch Charlie Garrod Darya Melicher

Concurrency & Parallelism. Threads, Concurrency, and Parallelism. Multicore Processors 11/7/17

Advanced MEIC. (Lesson #18)

Transcription:

Java Platform Concurrency Gotchas Alex Miller Terracotta

Questions to answer > What are common concurrency problems? > Why are they problems? > How do I detect these problems? > How do I correct these problems? 2

Taxonomy of Concurrency Gotchas > Shared Data > Coordination > Performance 3

Shared Data > Locking > Visibility > Atomicity > Safe Publication 4

What happens if we modify data without locking? 5

What happens if we modify data without locking? Hint: itʼs not good. 5

6

Mutable Statics public class MutableStatics { private static final DateFormat FORMAT = DateFormat.getDateInstance(DateFormat.MEDIUM); public static Date parse(string str) throws ParseException { return FORMAT.parse(str); FORMAT is mutable...and this mutates it outside synchronization public static void main(string arg[]) { MutableStatics.parse( Jan 1, 2000 ); 7

Mutable Statics - instance per call public class MutableStatics { public static Date parse(string str) throws ParseException { DateFormat format = DateFormat.getDateInstance(DateFormat.MEDIUM); return format.parse(str); public static void main(string arg[]) { MutableStatics.parse( Jan 1, 2000 ); 8

Synchronization private int myfield; synchronized( What goes here? ) { myfield = 0; 9

DO NOT synchronize on null MyObject obj = null; synchronized( obj ) { // stuff NullPointerException! 10

DO NOT change instance MyObject obj = new MyObject(); synchronized( obj ) { obj = new MyObject(); No longer synchronizing on the same object! 11

12

DO NOT synchronize on string literals private static final String LOCK = LOCK ; synchronized( LOCK ) { // work What is the scope of LOCK? 13

DO NOT synchronize on autoboxed instances private static final Integer LOCK = 0; synchronized( LOCK ) { // work What is the scope of LOCK? 14

DO NOT synchronize on ReentrantLock final Lock lock = new ReentrantLock(); synchronized( lock ) { // work Probably not what you meant here final Lock lock = new ReentrantLock(); lock.lock(); try { //... finally { lock.unlock(); Probably should be this instead 15

What should I lock on? // The field you are protecting private final Map map =... synchronized(map) { //...access map // Or an explicit lock object private final Object lock = new Object(); synchronized(lock) { //... modify state 16

Visibility 17

Inconsistent Synchronization public class SomeData { private final Map data = new HashMap(); public void put(string key, String value) { synchronized(data) { data.put(key, value); Modified under synchronization public String get(string key) { return data.get(key); Read without synchronization 18

Double-checked locking public final class Singleton { private static Singleton instance; public static Singleton getinstance() { if(instance == null) { synchronized(singleton.class) { if(instance == null) { instance = new Singleton(); return instance; Attempt to avoid synchronization 19

Double-checked locking public final class Singleton { private static Singleton instance; public static Singleton getinstance() { if(instance == null) { synchronized(singleton.class) { if(instance == null) { instance = new Singleton(); return instance; READ READ WRITE 19

Double-checked locking - volatile public class Singleton { private static volatile Singleton instance; public static Singleton getinstance() { if(instance == null) { synchronized(singleton.class) { if(instance == null) { instance = new Singleton(); return INSTANCE; 20

Double-checked locking - initialize on demand public class Singleton { private static class SingletonHolder { private static final Singleton instance = new Singleton(); public static Singleton getinstance() { return SingletonHolder.instance; 21

Racy single-check public final class String { private int hash; // default to 0 private final char[] value; // immutable public int hashcode() { int h = hash; if(h == 0) { //... compute value for h from data hash = h; return h; 22

volatile arrays public final class VolatileArray { private volatile boolean[] vals; public void flip(int i) { vals[i] = true; Is the value of vals[i] visible to other threads? public boolean flipped(int i) { return vals[i]; 23

Atomicity 24

Volatile counter public class Counter { private volatile int count; public int next() { return count++; Looks atomic to me! 25

AtomicInteger counter public class Counter { private final AtomicInteger count = new AtomicInteger(); public int next() { return count.getandincrement(); Really atomic by encapsulating multiple actions 26

Composing atomic actions public Object putifabsent( Hashtable table, Object key, Object value) { if(table.containskey(key)) { // already present, return existing value return table.get(key); else { // doesn t exist, create and return new value Hashtable is thread-safe table.put(key, value); return value; READ READ WRITE 27

Composing atomic actions public Object putifabsent( Hashtable table, Object key, Object value) { if(table.containskey(key)) { // already present, return existing value return table.get(key); else { // doesn t exist, create and return new value Hashtable is thread-safe table.put(key, value); return value; READ READ WRITE 27

Participate in lock public Object putifabsent( Hashtable table, Object key, Object value) { synchronized(table) { if(table.containskey(key)) { return table.get(key); else { table.put(key, value); return value; Protect with synchronization 28

Encapsulated compound actions public Object putifabsent( ConcurrentHashMap table, Object key, Object value) { table.putifabsent(key, value); 29

Assignment of 64 bit values public class LongAssignment { private long x; public void setlong(long val) { x = val; Looks atomic to me - but is it? 30

Assignment of 64 bit values - volatile public class LongAssignment { private volatile long x; public void setlong(long val) { x = val; 31

Safe publication Intentionally left blank. 32

Listener in constructor public interface DispatchListener { void newfare(customer customer); public class Taxi implements DispatchListener { public Taxi(Dispatcher dispatcher) { dispatcher.registerlistener(this); // other initialization We just published a reference to this - oops! public void newfare(customer customer) { // go to new customer's location 33

Starting thread in constructor public class Cache { private final Thread cleanerthread; this escapes again! public Cache() { cleanerthread = new Thread(new Cleaner(this)); cleanerthread.start(); // Cleaner calls back to this method public void cleanup() { // clean up Cache 34

Static factory method public class Cache { //... public static Cache newcache() { Cache cache = new Cache(); cache.startcleanerthread(); return cache; 35

Coordination > Threads > wait/notify 36

Threads > DO NOT: Call Thread.stop() Call Thread.suspend() or Thread.resume() Call Thread.destroy() Call Thread.run() Use ThreadGroups 37

wait/notify // Thread 1 synchronized(lock) { while(! somecondition()) { lock.wait(); You must synchronize. Always wait in a loop. // Thread 2 synchronized(lock) { satisfycondition(); lock.notifyall(); Synchronize here too. Update condition! 38

Performance > Deadlock > Spin wait > Lock contention 39

Deadlock // Thread 1 synchronized(lock1) { synchronized(lock2) { // stuff Classic deadlock. // Thread 2 synchronized(lock2) { synchronized(lock1) { // stuff 40

Deadlock avoidance > Lock splitting > Lock ordering > Lock timeout > trylock 41

Spin wait // Not efficient private volatile boolean flag = false; public void waittillchange() { while(! flag) { Thread.sleep(100); Spin on flag, waiting for change public void change() { flag = true; 42

Replace with wait/notify private final Object lock = new Object(); private boolean flag = false; public void waittillchange() { synchronized(lock) { while(! flag) lock.wait(); public void change() { synchronized(lock) { flag = true; lock.notifyall(); Wait/notify is far more efficient than spin wait. 43

Lock contention x Hash f(x) Bucket 0 Bucket 1 Bucket 2 Bucket 3 44

Lock striping x Hash g(x) Hash f(x) Hash f(x) Bucket 0 Bucket 1 Bucket 0 Bucket 1 45

Final Exam public class StatisticsImpl implements Statistics, StatisticsImplementor { private long queryexecutioncount; public synchronized void queryexecuted(string hql, int rows, long time) { queryexecutioncount++; //... other stat collection public long getqueryexecutioncount() { return queryexecutioncount; public synchronized void clear() { queryexecutioncount = 0; //... clear all other stats 46

Final Exam public class StatisticsImpl implements Statistics, StatisticsImplementor { private long queryexecutioncount; public synchronized void queryexecuted(string hql, int rows, long time) { queryexecutioncount++; //... other stat collection public long getqueryexecutioncount() { return queryexecutioncount; Read of shared value without synchronization public synchronized void clear() { queryexecutioncount = 0; //... clear all other stats 46

Final Exam public class StatisticsImpl implements Statistics, StatisticsImplementor { private long queryexecutioncount; public synchronized void queryexecuted(string hql, int rows, long time) { queryexecutioncount++; //... other stat collection public long getqueryexecutioncount() { return queryexecutioncount; Read of shared value without synchronization Non-atomic read of long value public synchronized void clear() { queryexecutioncount = 0; //... clear all other stats 46

Final Exam public class StatisticsImpl implements Statistics, StatisticsImplementor { private long queryexecutioncount; public synchronized void queryexecuted(string hql, int rows, long time) { queryexecutioncount++; //... other stat collection public long getqueryexecutioncount() { return queryexecutioncount; Read of shared value without synchronization Non-atomic read of long value public synchronized void clear() { queryexecutioncount = 0; //... clear all other stats Race condition if reading stat and clearing - could be compound action 46

Final Exam public class StatisticsImpl implements Statistics, StatisticsImplementor { private long queryexecutioncount; public synchronized void queryexecuted(string hql, int rows, long time) { Single shared lock for ALL stat values queryexecutioncount++; //... other stat collection public long getqueryexecutioncount() { return queryexecutioncount; Read of shared value without synchronization Non-atomic read of long value public synchronized void clear() { queryexecutioncount = 0; //... clear all other stats Race condition if reading stat and clearing - could be compound action 46

Alex Miller Terracotta Blog: http://tech.puredanger.com Twitter: @puredanger