Java Memory Model: Content

Similar documents
Built-in Concurrency Primitives in Java Programming Language. by Yourii Martiak and Mahir Atmis

Outline of this lecture G52CON: Concepts of Concurrency

CS11 Java. Fall Lecture 7

Rigorous Software Development CSCI-GA

SSC - Concurrency and Multi-threading Java multithreading programming - Synchronisation (I)

Java Virtual Machine Locks

Java Interview Questions and Answers

Monitors, Java, Threads and Processes

aicas Technology Multi Core und Echtzeit Böse Überraschungen vermeiden Dr. Fridtjof Siebert CTO, aicas OOP 2011, 25 th January 2011

SYSTEM ecos Embedded Configurable Operating System

Threads & Tasks: Executor Framework

Monitors and Exceptions : How to Implement Java efficiently. Andreas Krall and Mark Probst Technische Universitaet Wien

Checking Access to Protected Members in the Java Virtual Machine

Tutorial: Getting Started

AP Computer Science Java Subset

Threads 1. When writing games you need to do more than one thing at once.

Data races in Java and static analysis

Topics. Producing Production Quality Software. Concurrent Environments. Why Use Concurrency? Models of concurrency Concurrency in Java

Overview Motivating Examples Interleaving Model Semantics of Correctness Testing, Debugging, and Verification

OBJECT ORIENTED PROGRAMMING LANGUAGE

1.1. Over view Example 1: CPU Slot Distribution A example to test the distribution of cpu slot.

Concurrent Programming

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

JAVA - MULTITHREADING

Habanero Extreme Scale Software Research Project

Crash Course in Java

How to create/avoid memory leak in Java and.net? Venkat Subramaniam

Exception Handling. Overloaded methods Interfaces Inheritance hierarchies Constructors. OOP: Exception Handling 1

How to miscompile programs with benign data races

CS 111 Classes I 1. Software Organization View to this point:

Introduction to Java

Building a Multi-Threaded Web Server

JVM Tool Interface. Michal Pokorný

Computing Concepts with Java Essentials

Pemrograman Dasar. Basic Elements Of Java

Java Coding Practices for Improved Application Performance

A Survey of Parallel Processing in Linux

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Massachusetts Institute of Technology 6.005: Elements of Software Construction Fall 2011 Quiz 2 November 21, 2011 SOLUTIONS.

Deadlock Victim. dimanche 6 mai 12

An Implementation Of Multiprocessor Linux

Replication on Virtual Machines

Last Class: Semaphores

Concurrent programming in Java

A Comparison Of Shared Memory Parallel Programming Models. Jace A Mogill David Haglin

Java Concurrency Framework. Sidartha Gracias

An Overview of Java. overview-1

OPERATING SYSTEMS PROCESS SYNCHRONIZATION

Real Time Programming: Concepts

Overview. CMSC 330: Organization of Programming Languages. Synchronization Example (Java 1.5) Lock Interface (Java 1.5) ReentrantLock Class (Java 1.

COS 318: Operating Systems

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Spring 2011 Prof. Hyesoon Kim

First-class User Level Threads

Explain the relationship between a class and an object. Which is general and which is specific?

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

DIPLOMADO DE JAVA - OCA

Understanding Hardware Transactional Memory

CSCI E 98: Managed Environments for the Execution of Programs

Stack Allocation. Run-Time Data Structures. Static Structures

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Shared Address Space Computing: Programming

Licensed for viewing only. Printing is prohibited. For hard copies, please purchase from

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

Getting Started with the Internet Communications Engine

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

Java SE 8 Programming

Performance Improvement In Java Application

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

Simple Cooperative Scheduler for Arduino ARM & AVR. Aka «SCoop»

UI Performance Monitoring

Lecture 6: Semaphores and Monitors

Java Programming Fundamentals

The Sun Certified Associate for the Java Platform, Standard Edition, Exam Version 1.0

The Java Virtual Machine and Mobile Devices. John Buford, Ph.D. Oct 2003 Presented to Gordon College CS 311

Moving from CS 61A Scheme to CS 61B Java

1 Posix API vs Windows API

STM32JAVA. Embedded Java Solutions for STM32

Advanced compiler construction. General course information. Teacher & assistant. Course goals. Evaluation. Grading scheme. Michel Schinz

Inner Classes Specification

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Part I. Multiple Choice Questions (2 points each):

Write Barrier Removal by Static Analysis

Design by Contract beyond class modelling

Operating Systems for Parallel Processing Assistent Lecturer Alecu Felician Economic Informatics Department Academy of Economic Studies Bucharest

JAVA Program For Processing SMS Messages

Thread Synchronization and the Java Monitor

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Glossary of Object Oriented Terms

Goldilocks: A Race and Transaction-Aware Java Runtime

GPU Parallel Computing Architecture and CUDA Programming Model

CS5233 Components Models and Engineering

Atomicity for Concurrent Programs Outsourcing Report. By Khilan Gudka Supervisor: Susan Eisenbach

University of Twente. A simulation of the Java Virtual Machine using graph grammars

Concepts and terminology in the Simula Programming Language

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Facing the Challenges for Real-Time Software Development on Multi-Cores

Hoare-Style Monitors for Java

JUnit Howto. Blaine Simpson

Transcription:

Java Memory Model: Content Memory Models Double Checked Locking Problem Java Memory Model: Happens Before Relation Volatile: in depth 16 March 2012 1

Java Memory Model JMM specifies guarantees given by the JVM About when writes to variables become visible to other threads Revised as part of JSR-133 (=> part of JDK 1.5) JMM is an abstraction on top of hardware memory models Java Memory Model Threads read and write to variables Hardware Memory Model Processors read and write in caches Processors read and write to registers Processors read and write in main memory 16 March 2012 2

JMM Key Ideas Key Ideas: All threads share the main memory Each thread uses a local working memory (conceptionally) In reality several cache levels are provided Flushing and Refreshing working memory to and from the main memory must comply with the JMM CPU cache CPU cache main memory 16 March 2012 3

JMM Guarantees Atomicity T1 T2 Which operations are indivisible? Visibility Under which condition are the effects of operations executed by one thread visible to other threads? Ordering Under which conditions can the effects of operations appear out of order to any given thread? Related to visibility issue x = 3; x = 3; y = 4; println(x); println(x+y); // 4 is possible 16 March 2012 4

Memory Consistency Models Sequential Consistency [unrealistic model] Each read of a variable will see the last write in the execution order "... the result of any execution is the same as if the operations of all the processors were executed in some sequential order, and the operations of each individual processor appear in this sequence in the order specified by its program." [Leslie Lamport, "How to Make a Multiprocessor Computer That Correctly Executes Multiprocess Programs", IEEE Trans. Comput. C-28,9 (Sept. 1979), 690-691. ] Unpractical: caching mechanisms would be useless JMM does not guarantee sequential consistency Reordering is generally permitted 16 March 2012 5

Memory Consistency Models JMM Consistency Compilers may generate instructions in a different order than the obvious one suggested by the source code Compilers may store variables in registers Processors may execute instructions in parallel or out of order (pipelining) Caches may vary the order in which writes to variables are committed to main memory Values stored in processor-local caches may not be visible to other processors JLS only requires the JVM to maintain within-thread as-if-serial semantics 16 March 2012 6

Java Memory Model Java Language Specification JLS only requires the JVM to maintain within-thread as-if-serial semantics As long as a thread has the same result as if it were executed in program order in a strictly sequential environment, all these games are permissible Pipelined superscalar execution units Dynamic instruction scheduling Speculative execution Sophisticated multi-level memory caches It is only when multiple threads share data that it is necessary to coordinate their activities 16 March 2012 7

Example x = 0; y = 0 start threads Thread 1 Thread 2 x = 1 y = 1 b = y a = x Which values can fields a and b have after the execution of these statements 16 March 2012 8

Example x = 0; y = 0 x = 0; y = 0 x = 0; y = 0 x = 1 x = 1 y = 1 b = y y = 1 a = x y = 1 b = y x = 1 a = x a = x b = y a = 1 / b = 0 a = 1 / b = 1 a = 0 / b = 1 16 March 2012 9

Example x = 0; y = 0 x = 0; y = 0 a = x cache.x=1 x = 1 b = y b = y y = 1 y = 1 Flush cache a = x Compiler Reordering a = 0 / b = 0 a = 0 / b = 0 Cache Effect 16 March 2012 10

Example: Peterson Mutex public class PetersonMutex implements Mutex { private Map<Thread,Integer> ids=new HashMap<Thread, Integer>(); private boolean[] enter = { false, false ; private int turn = 0; public PetersonMutex(Thread t0, Thread t1) { ids.put(t0, 0); ids.put(t1, 1); public void lock() { int index = ids.get(thread.currentthread()); int otherindex = 1 - index; enter[index] = true; turn = otherindex; while ( enter[otherindex] && turn!= index) { public void unlock() { enter[ids.get(thread.currentthread())] = false; 16 March 2012 11

Example: Peterson Mutex public class Main { private static Mutex pm; private static int v = 0; public static void main(string[] args) { Thread t1 = new Thread(r); Thread t2 = new Thread(r); pm = new PetersonMutex(t1, t2); t1.start(); t2.start(); private static Runnable r = new Runnable() { public void run() { while (true) { criticalsection(); ;... 16 March 2012 12

Example: Peterson Mutex... private static void criticalsection() { pm.lock(); v++; try { Thread.sleep(500); catch (InterruptedException e){ System.out.println(v); // should always print 1 try { Thread.sleep(500); catch (InterruptedException e){ v--; pm.unlock(); 16 March 2012 13

Java Memory Model: Content Memory Models Double Checked Locking Problem Java Memory Model: Happens Before Relation Volatile: in depth 16 March 2012 14

Double Checked Locking Problem Singleton with eager initialization public class Singleton { private static Singleton instance = new Singleton(); public static Singleton getinstance(){ return instance; private Singleton(){ /* initialization */ // other methods Instance is created upon loading of the class 16 March 2012 15

Double Checked Locking Problem Singleton with lazy initialization public class Singleton { private static Singleton instance; public synchronized static Singleton getinstance(){ if(instance == null){ instance = new Singleton(); return instance; private Singleton(){ /* initialization */ // other methods Instance is only created upon access Disadvantage: Synchronization of getinstance 16 March 2012 16

Double Checked Locking Problem Singleton with double checked locking public class Singleton { private static Singleton instance; public static Singleton getinstance(){ if(instance == null){ synchronized(singleton.class){ if(instance == null){ instance = new Singleton(); return instance; private Singleton(){ /* initialization */ // other methods 16 March 2012 17

Double Checked Locking Problem Problems Due to possible instruction reordering it may happen, that a second thread gets access to the singleton before the first thread finished initialization (writes to instance and write to singleton instance fields may be reordered) It doesn t work in Java <= 1.4 There is no way to make the DCL work without requiring each thread that accesses the instance object to perform some kind of synchronization: Explicit synchronization Access to volatile fields for synchronization => Don't try to be too clever 16 March 2012 18

Java Memory Model: Content Memory Models Double Checked Locking Problem Java Memory Model: Happens Before Relation Volatile: in depth 16 March 2012 19

Java Memory Model: Atomicity Explicit synchronization Operations in a synchronized block are atomic Operations between access / release of a lock are atomic x++ is not atomic Unsynchronized field access Access to variables of primitive type (except long/double) is atomic Access to references is atomic (does not include access to referenced object) Access to volatile variables (including long/double) is atomic Access to atomic variables is atomic (=> lock free programming) 16 March 2012 20

Java Memory Model: Atomicity Unsynchronized field access Atomicy does not mean that we get the most recent value! => visibility Atomicy only means that we will not get arbitrary bits long volatile id = long 0; id = 0; // Thread 1 id = 0x1111111122222222L; // Thread 2 id = 0x3333333344444444L; Possible results: // Thread 3 System.out.printf("%x\n", id); 0000000000000000 1111111144444444 1111111122222222 3333333322222222 3333333344444444 16 March 2012 21

Java Memory Model: Visibility & Ordering JMM defines partial ordering called happens-before on actions Actions = variable read/write, monitor lock/unlock, thread start/join To guarantee that a thread executing action B can see the results of action A (same or different thread) there must be a happens-before relationship between A and B, otherwise there is no guarantee! Rules Each action in a thread happens-before every action in that thread that comes later in the program order An unlock on a monitor lock happens-before every subsequent lock on that same monitor lock A write to a volatile field happens-before every subsequent read of that same field A call to Thread.start on a thread happens-before every subsequent action in the started thread The happens-before order is transitive (A <= B && B <= C => A <= C) 16 March 2012 22

Java Memory Model: Visibility & Ordering Thread1 ref1.x = 1 lock M glo = ref1 unlock M everything before an unlock (release) is visible to everything after a later lock (acquire) on the same object lock M ref2 = glo unlock M j = ref2.x Thread2 16 March 2012 23

Java Memory Model: Visibility & Ordering Synchronization Synchronization guarantees that changes to object fields made in one thread are visible by other threads Obtaining a lock forces the thread to get fresh values from memory Releasing a lock forces the thread to flush out all pending writes Thread-Start Changes made in the starting thread are visible in the started thread The start of a thread implies a flush of pending writes in the starting thread Started thread will get fresh values from memory before run is executed Thread-Stop Changes made in a thread are visible by (actively) waiting threads The end of a thread implies a flush A thread which is informed about the end of another thread (join, isalive) sees the changes performed by the terminated thread on shared fields 16 March 2012 24

Java Memory Model: Visibility & Ordering Volatile Fields volatile int x = 0, y = 0; Guarantees visibility of writes (i.e. volatile variables are never cached) Read access to a volatile implies to get fresh values from memory Write access to a volatile forces the thread to flush out all pending writes reads/writes of volatile longs/doubles are guaranteed to be atomic Volatile in Java >= 1.5 Declaring a variable x "volatile" is equivalent to wrapping all read/write access on x in synchronized blocks sharing the same lock object, except that "volatile" doesn't enforce mutual exclusion Reads & writes which happen before a volatile access are visible by other threads accessing the same volatile field 16 March 2012 25

Stopping a Thread stop() do not call stop: deprecated upon stop all locks are released stopping through cooperation class Test extends Thread { private boolean stopped = false ; public void stopthread(){stopped = true; public void run( ) { while(!stopped ){ // do work // clean up 16 March 2012 26

Stack Method size class Stack { private int[] array = new int[10]; private int tos = 0; // top of stack public synchronized void push(int x) { array[tos++] = x; public synchronized int pop() { return array[--tos]; public int size() { return tos; Size need not be declared as synchronized for data consistency Int access is atomar, so a valid value is read 16 March 2012 28

Volatile: Visibility 1 Thread 1 Thread 2 Thread 3 int value = 0; volatile boolean ready = false; value = 77; ready = true; if(ready) println(value); 16 March 2012 30

Volatile: Visibility 2 Thread 1 Thread 2 Thread 3 volatile Person p = null; p = new Person(); p.setname("meier"); if(p!= null){ println(p.getname()); 16 March 2012 32

Java Memory Model: Content Memory Models Double Checked Locking Problem Java Memory Model: Happens Before Relation Volatile: in depth 16 March 2012 33

Volatile: Perfomance Costs of volatile access Compared to synchronized no management of wait queues and locks Read & Write access from memory point of view comparable to monitorenter / monitorexit operations Access to a volatile variable inside a loop can be more expensive than synchronization of whole loop Java 1.6 "No cost" for reading a volatile variable (compared to non-volatile) Cost for writing a volatile variable 16 March 2012 34

Volatile: Side effects to other variables Example of a (simple) Exchanger (without synchronization!) class Exchanger { private volatile boolean ready = false; private Object data = null; public Object getresult() { if(ready); return data; public void putresult(object obj) { // only called once! if(ready) throw IllegalStateException(); data = obj; ready = true; Access to volatile fields also has effects to the visibility of other fields Compiler cannot optimize away access to volatile fields 16 March 2012 35

Volatile: Reference variables Similar exchanger example, but with a reference class Pair { private Object first, second; public void addfirst(object first){this.first=first; public void addsecond(object second){this.second=second; public Object[] toarray() { return new Object[]{first, second; Exchanger Pair data Pair Object first Object second 16 March 2012 36

Volatile: Reference variables Similar exchanger example, but with a reference class Exchanger { private volatile Pair data = null; public Object[] getresult(){ return data==null? null : data.toarray(); public boolean isready(){ return data!= null; public void putresult(object first, Object second){ Pair tmp = new Pair(); tmp.addfirst(first); tmp.addsecond(second); data = tmp 16 March 2012 37

Volatile: Reference variables Discussion Fields first/second are visible as they are written before the write to the volatile data field The following implementation would be wrong public void putresult(object first, Object second){ data = new Pair(); data.addfirst(first); data.addsecond(second); 16 March 2012 38

Volatile: Rules for the use of volatile Rule 1: Independence of values The new value to be written on a volatile does not depend on the old value If the new value depends on the old value we have a read-modify-write sequence, atomicity can only be achieved with synchronization Rule 2: Independence of other values If a variable depends on other variables, then several statements have to be executed in order to change an object from one consistent state to another consistent state 16 March 2012 39

Volatile: Typical Applications One-time events (latch) Variables which are changed only once can be defined as volatile Latch / Barrier Stopping of threads Multiple Publication (blackboard) New information object completely replaces old one volatile double temperature Lock / Volatile Combination Atomicy guaranteed with synchronization Visibility guaranteed with volatile 16 March 2012 40

Volatile: Double Checked Locking Problem Singleton with double checked locking public class Singleton { private volatile static Singleton instance; public static Singleton getinstance(){ if(instance == null){ synchronized(singleton.class){ if(instance == null){ instance = new Singleton(); return instance; private Singleton(){ /* initialization */ // other methods 16 March 2012 41

Double Checked Locking Problem Singleton with eager initialization [Revisited] public class Singleton { private static Singleton instance = new Singleton(); public static Singleton getinstance(){ return instance; private Singleton(){ /* initialization */ // other methods Is it correct? T1 is invoking Singleton.getInstance() and loads class T2 is invoking Singleton.getInstance() does it see the initialized value? 16 March 2012 42

Excursus: Class Initialization A class (or interface) of type T will be initialized immediately before the first occurrence of any of the following: [JLS 12.4.1] T is a class and an instance of T is created T is a class and a static method declared by T is invoked A static field declared by T is assigned A static field declared by T is used and the field is not a constant variable T is a top-level class, and an assert statement lexically nested within T is executed 16 March 2012 43

Excursus: Class Initialization Initialization Procedure [JLS 12.4.2] 1. Synchronize on the Class object that represents the class or interface to be initialized. This involves waiting until the current thread can obtain the lock for that object. 2. If initialization is in progress for the class or interface by some other thread, then wait on this Class object (which temporarily releases the lock). When the current thread awakens from the wait, repeat this step. 3. If initialization is in progress for the class or interface by the current thread, then this must be a recursive request for initialization. Release the lock on the Class object and complete normally. 4. If the class or interface has already been initialized, then no further action is required. Release the lock on the Class object and complete normally (but the lock establishes a happens-before relation) 5. If the Class object is in an erroneous state, then initialization is not possible. Release the lock on the Class object and throw a NoClassDefFoundError. 16 March 2012 44

Excursus: Class Initialization 6. Otherwise, record the fact that initialization of the Class object is now in progress by the current thread and release the lock on the Class object. 7. Next, if the Class object represents a class rather than an interface, and the superclass of this class has not yet been initialized, then recursively perform this entire procedure for the superclass. If necessary, verify and prepare the superclass first. If the initialization of the superclass completes abruptly because of a thrown exception, then lock this Class object, label it erroneous, notify all waiting threads, release the lock, and complete abruptly, throwing the same exception that resulted from initializing the superclass. 8. Next, determine whether assertions are enabled for this class by querying its defining class loader. 9. Next, execute either the class variable initializers and static initializers of the class, or the field initializers of the interface, in textual order, as though they were a single block, except that final class variables and fields of interfaces whose values are compile-time constants are initialized first. 16 March 2012 45

Excursus: Class Initialization 10. If the execution of the initializers completes normally, then lock this Class object, label it fully initialized, notify all waiting threads, release the lock, and complete this procedure normally. 11. Otherwise, the initializers must have completed abruptly by throwing some exception E. If the class of E is not Error or one of its subclasses, then create a new instance of the class ExceptionInInitializerError, with E as the argument, and use this object in place of E in the following step. But if a new instance of ExceptionInInitializerError cannot be created because an OutOfMemoryError occurs, then instead use an OutOfMemoryError object in place of E in the following step. 12. Lock the Class object, label it erroneous, notify all waiting threads, release the lock, and complete this procedure abruptly with reason E or its replacement as determined in the previous step. 16 March 2012 46

Double Checked Locking Problem Singleton with eager initialization [Revisited] public class Singleton { private static Singleton instance = new Singleton(); public static Singleton getinstance(){ return instance; private Singleton(){ /* initialization */ // other methods Is it correct? T1 is invoking Singleton.getInstance() and loads class T2 is invoking Singleton.getInstance() does it see the initialized value? Also correct under the old memory model (< 1.5) 16 March 2012 47

Summary Java Memory Model Guarantees about visibility of shared memory Requires the JVM to maintain only within-thread as-if-serial semantics Defines happens-before relation across threads Locking / Unlocking (using synchronized-blocks or java.util.concurrent.locks) Volatile read / write... and some other rules 16 March 2012 48