Sharing Objects. Java and Android Concurrency.

Similar documents
Lecture 17: Sharing Objects in Java

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

CMSC 433 Programming Language Technologies and Paradigms. Sharing Objects

Sharing Objects Ch. 3

Concurrency WS 2010/2011 The Java Memory Model

Advanced concurrent programming in Java Shared objects

Design Patterns & Concurrency. Sebastian Graf, Oliver Haase

Lecture 16: Thread Safety in Java

CMSC 433 Programming Language Technologies and Paradigms. Spring 2013

Composing Objects. Java and Android Concurrency.

The Java Memory Model

The New Java Technology Memory Model

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

CMSC 433 Programming Language Technologies and Paradigms. Synchronization

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

System Programming. Practical Session 4: Threads and Concurrency / Safety

Design of Thread-Safe Classes

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

Advanced MEIC. (Lesson #18)

CMSC 132: Object-Oriented Programming II. Effective Java. Department of Computer Science University of Maryland, College Park

Synchronization SPL/2010 SPL/20 1

Concurrency in Object Oriented Programs 1. Object-Oriented Software Development COMP4001 CSE UNSW Sydney Lecturer: John Potter

Introduction to Programming Using Java (98-388)

Threads Questions Important Questions

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Threads and Java Memory Model

Introduction to Locks. Intrinsic Locks

Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multit

Safety SPL/2010 SPL/20 1

The Java Memory Model

Multithreaded Programming Part II. CSE 219 Stony Brook University, Department of Computer Science

Synchronization in Java

CMSC 433 Spring 2013 Exam 1

Audience. Revising the Java Thread/Memory Model. Java Thread Specification. Revising the Thread Spec. Proposed Changes. When s the JSR?

Programmazione di sistemi multicore

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

A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 4 Shared-Memory Concurrency & Mutual Exclusion

CMSC 132: Object-Oriented Programming II

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

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

Linked List Nodes (reminder)

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Modular Reasoning about Aliasing using Permissions

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

Josh Bloch Charlie Garrod Darya Melicher

Page 1

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

CMSC 433 Programming Language Technologies and Paradigms. Spring 2013

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

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

CSE 374 Programming Concepts & Tools

Lecture 32: Volatile variables, Java memory model

A Java Framework for Smart Contracts

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

CSE 332: Data Structures & Parallelism Lecture 17: Shared-Memory Concurrency & Mutual Exclusion. Ruth Anderson Winter 2019

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

CSE332: Data Abstractions Lecture 22: Shared-Memory Concurrency and Mutual Exclusion. Tyler Robison Summer 2010

Synchronising Threads

CST242 Concurrency Page 1

Classes Classes 2 / 36

Threads, Concurrency, and Parallelism

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

15CS45 : OBJECT ORIENTED CONCEPTS

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

CS 231 Data Structures and Algorithms, Fall 2016

Java Platform Concurrency Gotchas

L13: Synchronization

CMSC 433 Programming Language Technologies and Paradigms. Composing Objects

Quiz 2 (April 22, 2016)

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

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

Concurrent Programming in the D Programming Language. by Walter Bright Digital Mars

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

Program Graph. Lecture 25: Parallelism & Concurrency. Performance. What does it mean?

Motivation & examples Threads, shared memory, & synchronization

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

Grafting Functional Support on Top of an Imperative Language

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

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

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance?

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

Semantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University

Java Memory Model. Jian Cao. Department of Electrical and Computer Engineering Rice University. Sep 22, 2016

Declarations and Access Control SCJP tips

SUMMARY INTRODUCTION CONCURRENT PROGRAMMING THREAD S BASICS. Introduction Thread basics. Thread states. Sequence diagrams

Principles of Software Construction: Concurrency, Part 1

For this section, we will implement a class with only non-static features, that represents a rectangle

Introduction. Assessment Test. Part I The Programmer s Exam 1

5/23/2015. Core Java Syllabus. VikRam ShaRma

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Using Java 8 Lambdas And Stampedlock To Manage Thread Safety

Question Points Score Total 100

Synchronization via Transactions

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

Thread-Local. Lecture 27: Concurrency 3. Dealing with the Rest. Immutable. Whenever possible, don t share resources

Software Development & Education Center. Java Platform, Standard Edition 7 (JSE 7)

COMP 346 WINTER Tutorial 2 SHARED DATA MANIPULATION AND SYNCHRONIZATION

Threads and Parallelism in Java

Lecture 19: Composing Objects in Java

Transcription:

Java and Android Concurrency Sharing Objects fausto.spoto@univr.it git@bitbucket.org:spoto/java-and-android-concurrency.git git@bitbucket.org:spoto/java-and-android-concurrency-examples.git Fausto Spoto Università di Verona, Italy - 1 / 32

Visibility Synchronization has two goals: ensure mutual exclusion (everybody knows this) ensure visibility (nobody knows this) What does this print? public class NoVisibility { private static boolean ready; private static int number; private static class ReaderThread extends Thread { public void run() { while (!ready) Thread.yield(); System.out.println(number); public static void main(string[] args) { new ReaderThread().start(); number = 42; ready = true; Fausto Spoto Università di Verona, Italy - 2 / 32

The Heisenberg Principle of Concurrent Programming In the absence of synchronization, the compiler, processor, and runtime can do some downright weird things to the order in which operations appear to execute. Attempts to reason about the order in which memory actions must happen in insufficiently synchronized multithreaded programs will almost certainly be incorrect A field might contain any of the values ever written into the field, but not necessarily the last one (out-of-thin-air safety). For long and double fields, even a value never written into the field might be seen! Fausto Spoto Università di Verona, Italy - 3 / 32

Synchronization Guarantees Visibility @NotThreadSafe public class MutableInteger { private int value; public int get() { return value; public void set(int value) { this.value = value; Fausto Spoto Università di Verona, Italy - 4 / 32

Synchronization Guarantees Visibility @NotThreadSafe public class MutableInteger { private int value; public int get() { return value; public synchronized void set(int value) { this.value = value; Fausto Spoto Università di Verona, Italy - 5 / 32

Synchronization Guarantees Visibility @ThreadSafe public class SynchronizedInteger { private @GuardedBy("this") int value; public synchronized int get() { return value; public synchronized void set(int value) { this.value = value; Fausto Spoto Università di Verona, Italy - 6 / 32

Locking, Visibility and volatile Fields Locking guarantees visibility When thread A executes a synchronized block, and subsequently thread B enters a synchronized block guarded by the same lock, the values of variables that were visible to A prior to realeasing the lock are guaranteed to be visible to B upon acquiring the lock volatile guarantees visibility When thread A writes to a volatile field and subsequently thread B reads the same field, the values of all variables that were visible to A prior to writing become visible to B after reading volatile boolean asleep; void trytosleep() { while (!asleep) countsomesheep(); Fausto Spoto Università di Verona, Italy - 7 / 32

Use volatile Variables Sparingly they have higher access cost compound operations are not atomic: count++ Locking can guarantee both visibility and atomicity; volatile variables can only guarantee visibility Use volatile variables only if 1 writes do not depend on previous value or only one thread performs writes 2 the variable does not participatre in invariants with other variables 3 locking is not needed for any other reason Fausto Spoto Università di Verona, Italy - 8 / 32

Publication and Escape Publication Publishing an object means making it available to code outside of its current scope: publishing internal state can compromise encapsulation and thread-safety publishing not fully constructed objects can compromise thread-safety Escape An object that is published when it should not have been is said to have escaped its intended scope Fausto Spoto Università di Verona, Italy - 9 / 32

Examples of Escape Escape through public fields public static Set<Secret> knownsecrets; public void initialize() { knownsecrets = new HashSet<Secret>(); Escape through return value private String[] states = new String[]{ "AK", "AL"... ; public String[] getstates() { return states; Fausto Spoto Università di Verona, Italy - 10 / 32

Examples of Escape Escape through overriddable methods private String[] states = new String[]{ "AK", "AL"... ;...dosomething(states); protected void dosomething(string[] ss) {... Escape through non-static inner classes public class ThisEscape { public ThisEscape(EventSource source) { source.registerlistener(new EventListener() { public void onevent(event e) { dosomething(e); );... Fausto Spoto Università di Verona, Italy - 11 / 32

Escape of Raw Objects The examples with overriddable methods and non-static inner classes are particularly bad since they can allow a partially initialized object (raw) to escape its constructor Partially initialized objects might not be usable from other threads, even if the class seems thread-safe Do not allow the this reference to escape during construction Fausto Spoto Università di Verona, Italy - 12 / 32

Thread Confinement If an object is confined to a thread, that is, it can only be accessed by that single thread, then there is no need to make it thread-safe, since all its uses are automatically thread-safe Ad-hoc thread confinement Java s Swing graphical library Android graphical library database connections from a connection pool Stack confinement Local variables are intrinsically confined to the executing thread always for primitive values if not published, for reference values Fausto Spoto Università di Verona, Italy - 13 / 32

Thread Confinement through ThreadLocal ThreadLocals are a sort of per-thread static fields. Calls to get yield the value associated to the currently executing thread, either initialized through initialvalue or subsequently modified through set private ThreadLocal<Connection> connectionholder = new ThreadLocal<>() { public Connection initialvalue() { return DriverManager.getConnection(DB URL); ; public Connection getconnection() { return connectionholder.get(); Fausto Spoto Università di Verona, Italy - 14 / 32

Immutability Immutable objects are good Immutable objects are thread-safe and can be shared and published without synchronization An object is immutable if 1 its state cannot be modified after construction 2 all its fields are final 3 it is properly constructed (this does not escape during construction) Fausto Spoto Università di Verona, Italy - 15 / 32

An Immutable Object Can Well Use Modifiable Objects @Immutable public final class ThreeStooges { private final Set<String> stooges = new HashSet<>(); public ThreeStooges() { stooges.add("moe"); stooges.add("larry"); stooges.add("curly"); public boolean isstooge(string name) { return stooges.contains(name); public String getstoogenames() { return stooges.tostring(); Fausto Spoto Università di Verona, Italy - 16 / 32

Immutable Objects and Collections in State Frequently, one is tempted to return a modifiable collection from the state of an object: this breaks encapsulation, makes the state escape and makes the object mutable: @Mutable public class C { private final Set<Element> set = new HashSet<>();... public Set<Element> getelements() { return set; Fausto Spoto Università di Verona, Italy - 17 / 32

Immutable Objects and Collections in State Bad solution: return a copy of the set the user might modify the copy and think this actually does something @Immutable public class C { private final Set<Element> set = new HashSet<>();... public Set<Element> getelements() { return new HashSet<>(set); Fausto Spoto Università di Verona, Italy - 18 / 32

Immutable Objects and Collections in State Bad solution: return a Collections.unmodifiableSet the user might modify the copy, because he thinks this actually does something, and get an exception, but only at runtime @Immutable public class C { private final Set<Element> set = new HashSet<>();... public Set<Element> getelements() { return Collections.unmodifiableSet(set); Fausto Spoto Università di Verona, Italy - 19 / 32

Immutable Objects and Collections in State Good solution: make the class iterable or return a new Iterable instead of a Set in 99% of the cases, iterations is all the user wants to do @Immutable public class C implements Iterable<Element> { private final Set<Element> set = new HashSet<>();... public Iterator<Element> iterator() { return set.iterator(); Fausto Spoto Università di Verona, Italy - 20 / 32

Immutable Objects and Collections in State Good solution: use internal iteration, passing a task as an interface or lambda-expression @Immutable public class C { private final Set<Element> set = new HashSet<>(); public interface Task { void process(element e);... public void foreach(task task) { for (Element e: set) task.process(e); Fausto Spoto Università di Verona, Italy - 21 / 32

Immutable Objects and Collections in State Good solution: define and return a new ImmutableSet copy for the 1% of the cases, when the user wants to do more than iterate public interface ImmutableSet<E> extends Iterable<E> { int size(); @Immutable public class C { private final Set<Element> set = new HashSet<>();... public ImmutableSet<Element> getelements() { return new ImmutableSet<Element>() { public int size() { return set.size(); public Iterator<Element> iterator() { return set.iterator(); ; Fausto Spoto Università di Verona, Italy - 22 / 32

Don t Be Afraid of Immutability An immutable object has only a single state, but references to an immutable object can be updated! Moreover: no locking cost no defensive copies instances can be shared instead of duplicated reduced generational garbage-collection Fausto Spoto Università di Verona, Italy - 23 / 32

An Immutable Object for the Factorization Cache @Immutable public class OneValueCache { private final BigInteger lastnumber; private final BigInteger[] lastfactors; public OneValueCache(BigInteger i, BigInteger[] factors) { lastnumber = i; lastfactors = Arrays.copyOf(factors, factors.length); public BigInteger[] getfactors(biginteger i) { if (lastnumber == null!lastnumber.equals(i)) return null; else return Arrays.copyOf(lastFactors, lastfactors.length); Fausto Spoto Università di Verona, Italy - 24 / 32

Cached Factorizer without Synchronization @ThreadSafe public class VolatileCachedFactorizer extends StatelessFactorizer { private volatile OneValueCache cache = new OneValueCache(null, null); @Override protected void dopost(httpservletrequest request, HttpServletResponse response) { BigInteger i = extractfromrequest(request); BigInteger[] factors = cache.getfactors(i); if (factors == null) { factors = factor(i); cache = new OneValueCache(i, factors); encodeintoresponse(response, factors); Fausto Spoto Università di Verona, Italy - 25 / 32

Unsafe Publication Very often, we do want to share objects. But publication must be done in a safe way, or otherwise very weird things might happen public class StuffIntoPublic { public Holder holder; public void initialize() { holder = new Holder(42); public class Holder { private int n; public Holder(int n) { this.n = n; public void assertsanity() { if (n!= n) throw new AssertionError("This is false"); 1 holder might be seen to contain null or a stale, old Holder 2 n s value might suddenly change, making the assertion fail Fausto Spoto Università di Verona, Italy - 26 / 32

Safe Publication public class StuffIntoPublic { public volatile Holder holder; public void initialize() { holder = new Holder(42); public class Holder { private final int n; public Holder(int n) { this.n = n; public void assertsanity() { if (n!= n) throw new AssertionError("This is false"); Making just n final would only satisfy the assertion: the object is published, but the reference to it might be stale Making holder volatile solves all problems Fausto Spoto Università di Verona, Italy - 27 / 32

Safe Publication Idioms Immutable objects can be published in any way. Mutable objects must be safely published, so that their internal state is guaranteed to be visible. This can happen in many ways: by storing the object into a volatile field by storing the object into a final field of a properly constructed object by initializing the object inside a class static initializer by storing the object into a properly guarded field by passing the object to a synchronized collection class from the standard Java library Fausto Spoto Università di Verona, Italy - 28 / 32

Exercise 1: Parallel Matrix Multiplication Make the matrix multiplication constructor in the following code parallel, through the use of multithreading how much faster do you think it will run then on a n-core machine? once the multiplication is divided across different threads, why is the resulting matrix safely published to the calling thread? Fausto Spoto Università di Verona, Italy - 29 / 32

Exercise 1: Sequential Matrix Multiplication public class Matrix { private final double[][] elements; private final static Random random = new Random(); public Matrix(int m, int n) { this.elements = new double[m][n]; for (int x = 0; x < n; x++) for (int y = 0; y < m; y++) elements[y][x] = random.nextdouble() * 100.0-50.0; public int getm() { return elements.length; public int getn() { return elements[0].length; Fausto Spoto Università di Verona, Italy - 30 / 32

Exercise 1: Sequential Matrix Multiplication // modify this constructor, make it run in parallel! private Matrix(Matrix left, Matrix right) { int m = left.getm(); int p = left.getn(); int n = right.getn(); this.elements = new double[m][n]; for (int x = 0; x < n; x++) for (int y = 0; y < m; y++) { double sum = 0.0; for (int k = 0; k < p; k++) sum += left.elements[y][k] * right.elements[k][x]; this.elements[y][x] = sum;... Fausto Spoto Università di Verona, Italy - 31 / 32

Esercise 2: Implement a Swing GUI for the Chat Servlets Build a Swing GUI for the chat servlets it should include a button that allows one to send a new chat message (author and text) and a text area that can be refreshed to show the last 20 messages from the chat Fausto Spoto Università di Verona, Italy - 32 / 32