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

Similar documents
Lecture 19: Composing Objects in Java

Sharing Objects. Java and Android Concurrency.

Lecture 17: Sharing Objects in Java

CMSC 433 Programming Language Technologies and Paradigms. Sharing Objects

Sharing Objects Ch. 3

Composing Objects. Java and Android Concurrency.

CMSC 433 Programming Language Technologies and Paradigms. Composing Objects

Advanced concurrent programming in Java Shared objects

Do inner private classes maintain the thread-safety of the outer class

Synchronization SPL/2010 SPL/20 1

Design of Thread-Safe Classes

The Java Memory Model

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

Safety SPL/2010 SPL/20 1

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

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

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Frequently asked questions from the previous class survey

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Frequently asked questions from the previous class survey

Design Patterns & Concurrency. Sebastian Graf, Oliver Haase

The New Java Technology Memory Model

Lecture 32: Volatile variables, Java memory model

CS455: Introduction to Distributed Systems [Spring 2019] Dept. Of Computer Science, Colorado State University

CMSC 433 Programming Language Technologies and Paradigms. Spring 2013

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

Concurrency WS 2010/2011 The Java Memory Model

CS11 Java. Fall Lecture 7

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

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

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

Lab. Lecture 26: Concurrency & Responsiveness. Assignment. Maze Program

Assertions, pre/postconditions

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

Lecture 16: Thread Safety in Java

Lecture 29: Java s synchronized statement

Atomic Variables & Nonblocking Synchronization

CST242 Concurrency Page 1

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

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

Race Conditions & Synchronization

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

Lecture 8: September 30

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

Synchronization. Announcements. Concurrent Programs. Race Conditions. Race Conditions 11/9/17. Purpose of this lecture. A8 released today, Due: 11/21

Concurrent Programming using Threads

CS455: Introduction to Distributed Systems [Spring 2018] Dept. Of Computer Science, Colorado State University

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Synchronization Lecture 24 Fall 2018

Frequently asked questions from the previous class survey

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

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

CS455: Introduction to Distributed Systems [Spring 2018] Dept. Of Computer Science, Colorado State University

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

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [DISTRIBUTED MUTUAL EXCLUSION] Frequently asked questions from the previous class survey

CS4411 Intro. to Operating Systems Exam 1 Fall points 9 pages

Lecture 10 State dependency

Configuration Provider: A Pattern for Configuring Threaded Applications

Introduction to Locks. Intrinsic Locks

Synchronization Lecture 23 Fall 2017

Test the Byte Class 1 / 17

Java Threads and intrinsic locks

Sharing is the Key. Lecture 25: Parallelism. Canonical Example. Bad Interleavings. Common to have: CS 62 Fall 2016 Kim Bruce & Peter Mawhorter

The Java Memory Model

CMSC 330: Organization of Programming Languages

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

Chapter 4 Defining Classes I

CSE332: Data Abstractions Lecture 19: Mutual Exclusion and Locking

Threads and Java Memory Model

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

CMSC 433 Programming Language Technologies and Paradigms. Spring 2013

CSE Lecture 19: Concurrency 11 Nov Nate Nystrom UTA

Need for synchronization: If threads comprise parts of our software systems, then they must communicate.

COMP 322: Fundamentals of Parallel Programming

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018

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

Programming Language Seminar Concurrency 2: Lock-free algorithms

CS370: System Architecture & Software [Fall 2014] Dept. Of Computer Science, Colorado State University

Concurrent and Real-Time Programming in Java

Threads SPL/2010 SPL/20 1

CE221 Programming in C++ Part 1 Introduction

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

Practical Concurrent and Parallel Programming 10

Threads and Parallelism in Java

What is a thread anyway?

Race Conditions & Synchronization

MultiJav: A Distributed Shared Memory System Based on Multiple Java Virtual Machines. MultiJav: Introduction

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

Results of prelim 2 on Piazza

Functional Objects. Christopher Simpkins CS 3693, Fall Chris Simpkins (Georgia Tech) CS 3693 Scala / 1

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

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018

Fortgeschrittene objektorientierte Programmierung (Advanced Object-Oriented Programming)

Ticket Machine Project(s)

CMSC 433 Spring 2013 Exam 1

Classes, interfaces, & documentation. Review of basic building blocks

Java Platform Concurrency Gotchas

Frequently asked questions from the previous class survey

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Atomicity CS 2110 Fall 2017

L13: Synchronization

Classes Classes 2 / 36

Transcription:

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREAD SAFETY] Putting the brakes, on impending code breaks Let a reference escape, have you? Misbehave, your code will, out of the blue Get out, you will, of this bind If, your objects, you have confined Shrideep Pallickara Computer Science Colorado State University L9.1 Frequently asked questions from the previous class survey What if A calls B, which is atomic, and B fails? Externally visible behavior of an object? Is a class with private final objects thread safe? L9.2 SLIDES CREATED BY: SHRIDEEP PALLICKARA L9.1

Topics covered in this lecture Sharing & Composing Objects Making a class thread-safe Multivariable invariants and thread-safety Adding functionality to a thread-safe class L9.3 Pitfalls of over synchronization Number of simultaneous invocations? Not limited by processor resources, but is limited by the application structure Poor concurrency L9.4 SLIDES CREATED BY: SHRIDEEP PALLICKARA L9.2

Antidote for poor concurrency Control the scope of the lock Too large: Invocations become sequential Don t make it too small either n Operations that are atomic should not be in a synchronized block L9.5 SHARING OBJECTS L9.6 SLIDES CREATED BY: SHRIDEEP PALLICKARA L9.3

What we will be looking at Techniques for sharing and publishing objects Safe access from multiple threads Together with synchronization, sharing objects lays foundation for thread-safe classes L9.7 Synchronization What we have seen so far: Atomicity and demarcating critical sections But it is also about memory visibility We prevent one thread from modifying object state while another is using it When state of an object is modified, other thread can see the changes that were made L9.8 SLIDES CREATED BY: SHRIDEEP PALLICKARA L9.4

Publication and Escape Publishing an object Makes it available outside current scope Escape n Storing a reference to it, returning from a non-private method, passing it as argument to another method An object that is published when it should not have been L9.9 Pitfalls in publication Publishing internal state variables Makes it difficult to preserve invariants Publishing objects before they are constructed Compromises thread-safety L9.10 SLIDES CREATED BY: SHRIDEEP PALLICKARA L9.5

Most blatant form of publication Storing a reference in a public static field public static Set<Secrets> knownsecrets; public void initialize() { knownsecrets = new HashSet<Secret>(); If you add a Secret to knownsecrets? You also end up publishing that Secret L9.11 Allowing internal mutable state to escape public class PublishingState { private String[] states = new String[] { AK, AL, ; public String[] getstates() {return states; states has escaped its intended scope What should have been private is now public Any caller can modify its contents L9.12 SLIDES CREATED BY: SHRIDEEP PALLICKARA L9.6

Another way to publish internal state public class ThisEscape { public ThisEscape(EventSource source) { source.registerlistener( new EventListener() { public void onevent(event e) { dosomething(e); ); When EventListener is published, it publishes the enclosing ThisEscape instance Inner class instances contain hidden reference to enclosing instance L9.13 Safe construction practices An object is in a predictable, consistent state only after its constructor returns Publishing an object within its constructor? You are publishing an incompletely constructed object Even if you are doing so in the last line of the constructor RULE: Don t allow this to escape during construction L9.14 SLIDES CREATED BY: SHRIDEEP PALLICKARA L9.7

A common mistake is to start a thread from a constructor When an object creates a thread in its constructor Almost always shares its this reference with the new thread n Explicitly: Passing it to the constructor n Implicitly: The Thread or Runnable is an inner class of the owning object Nothing wrong with creating a thread in a constructor Just don t start the Thread Expose an initialize() method L9.15 THREAD CONFINEMENT L9.16 SLIDES CREATED BY: SHRIDEEP PALLICKARA L9.8

Thread confinement Accessing shared, mutable data requires synchronization Avoid this by not sharing If data is only accessed from a single thread? No synchronization is needed When an object is confined to a thread? Usage is thread-safe even if the object is not L9.17 Thread confinement Language has no means of confining a object to a thread Thread confinement is an element of program s design Enforced by implementation Language and core libraries provide mechanisms to help with this Local variables and the ThreadLocal class L9.18 SLIDES CREATED BY: SHRIDEEP PALLICKARA L9.9

Stack confinement Object can only be reached through local variables Local variables are intrinsically confined to the executing thread Exist on executing thread s stack Not accessible to other threads L9.19 Thread confinement of reference variables public int loadtheark() { SortedSet<Animal> animals; // animals confined to method don t let // them escape return numpairs; If you were to publish a reference to animals, stack confinement would be violated L9.20 SLIDES CREATED BY: SHRIDEEP PALLICKARA L9.10

ThreadLocal Allows you to associate a per-thread value with a value-holding object Provides set and get accessor methods Maintains a separate copy of value for each thread that uses it get returns the most recent value passed to set n From the currently executing thread L9.21 Using ThreadLocal for thread confinement private static ThreadLocal<Connection> connectionholder = new ThreadLocal<Connection>() { public Connection initialvalue() { return DriverManager.getConnection(DB_URL); ; public static Connection getconnection() { return connectionholder.get(); Each thread will have its own connection When thread calls ThreadLocal.get for the first time? initialvalue() provides the initial value L9.22 SLIDES CREATED BY: SHRIDEEP PALLICKARA L9.11

Common use of ThreadLocal Used when a frequently used operation requires a temporary object Wish to avoid reallocating temporary object on each invocation Integer.toString() Before 5.0 used ThreadLocal to store a 12-byte buffer for formatting result L9.23 IMMUTABLE OBJECTS L9.24 SLIDES CREATED BY: SHRIDEEP PALLICKARA L9.12

Immutable objects State cannot be modified after construction All its fields are final Properly constructed The this reference does not escape during construction L9.25 Immutable objects public final class ThreeStooges { private final Set<String> stooges = new HashSet<String>(); public ThreeStooges() { stooges.add( Moe ); stooges.add( Larry ); stooges.add( Curly ); public boolean isstooge() {return stooges.contains(name); Design makes it impossible to modify after construction The stooges reference is final All object state reached through a final field L9.26 SLIDES CREATED BY: SHRIDEEP PALLICKARA L9.13

Safe publication of objects Storing reference to an object into a public field is not enough to publish that object safely public Holder holder; public void initialize() { holder = new Holder(42); Holder could appear to be in an inconsistent state Even though invariants may have been established by constructor L9.27 Class at risk of failure if not published properly public class Holder { private int n; public Holder(int n) {this.n == n public void assertsanity() { if (n!= n) { throw new AssertionError( Statement is false ); Thread may see a stale value first time it reads the field and an up-to-date value the next time L9.28 SLIDES CREATED BY: SHRIDEEP PALLICKARA L9.14

COMPOSING OBJECTS L9.29 Composing Objects We don t want to have to analyze each memory access to ensure program is thread-safe We wish to take thread-safe components and compose them into larger components or programs L9.30 SLIDES CREATED BY: SHRIDEEP PALLICKARA L9.15

Basic elements of designing a thread-safe class Identify variables that form the object s state Identify invariants that constrain the state variables Establish a policy for managing concurrent access to the object s state L9.31 Synchronization policy Defines how object coordinates access to its state Without violating its invariants or post-conditions Specifies a combination of: Immutability Thread confinement Locking To maintain Thread Safety L9.32 SLIDES CREATED BY: SHRIDEEP PALLICKARA L9.16

Looking at a counter public final class Counter { private long value=0; public synchronized long getvalue() { return value; public synchronized long increment() { if (value == Long.MAX_VALUE) { throw new IllegalStateException( Counter Overflow ); value++; return value; L9.33 Making a class thread-safe Ensure that invariants hold under concurrent access We need to reason about state Object and variables have state space Range of possible states Keep this small so that it is easier to reason about L9.34 SLIDES CREATED BY: SHRIDEEP PALLICKARA L9.17

Classes have invariants that tag certain states as valid or invalid Looking back at our Counter example The value field is a long The state space ranges from Long.MIN_VALUE to Long.MAX_VALUE The class places constraints on value Negative values are not allowed L9.35 Operations may have post conditions that tag state transitions as invalid Looking back at our Counter example If the current state of Counter is 17 The only valid next state is 18 When the next state is derived from the current state? n Compound action Not all operations impose state transition constraints For e.g. if a variable tracks current temperature? Previous state doesn t impact current state L9.36 SLIDES CREATED BY: SHRIDEEP PALLICKARA L9.18

Constraints and synchronization requirements If certain states are invalid? Underlying state variables should be encapsulated n If not, client code can put it in an inconsistent state If an operation has invalid state transitions? It must be made atomic L9.37 Looking at a case where invariants constrain multiple state variables public class NumberRange { private final AtomicInteger lower = new AtomicInteger(0); private final AtomicInteger upper = new AtomicInteger(0); public void setlower(int i) { if (i > upper.get() ) throw IllegalArgumentException( lower > upper! ); lower.set(i); public void setupper(int i) { if (i < lower.get() ) throw IllegalArgumentException( upper < lower! ); upper.set(i); public boolean isinrange(int i){ return (i >= lower.get() && i <= upper.get()); L9.38 SLIDES CREATED BY: SHRIDEEP PALLICKARA L9.19

Problems with NumberRange Does not preserve invariant that constrains lower and upper The methods setlower and setupper attempt this preservation But they do so poorly! They are check-then-act sequences that use insufficient locking that precludes atomicity L9.39 Problems with NumberRange If the number range (0, 10) holds One thread calls setlower(5) while another calls setupper(4) With unlucky timing? Both calls will pass checks in the setters Both modifications will be applied Range is now (5,4) an invalid state AtomicInteger is thread-safe, the composite class is not L9.40 SLIDES CREATED BY: SHRIDEEP PALLICKARA L9.20

Multivariable invariants Related variables must be fetched or updated in an atomic operation Don t: Update one Release and reacquire lock, and Then update others The lock that guards the variables Must be held for the duration of any operation that accesses them L9.41 State-dependent operations Objects may have state-based pre-conditions E.g., cannot remove item from an empty queue In a single-threaded program Operations simply fail In a concurrent program Precondition may be true later because of the actions of another thread L9.42 SLIDES CREATED BY: SHRIDEEP PALLICKARA L9.21

State dependent operations: Mechanisms wait()/notify() Supported by the JVM and closely tied with intrinsic locking Other possibilities Use classes such as blocking queues or semaphores L9.43 STATE OWNERSHIP L9.44 SLIDES CREATED BY: SHRIDEEP PALLICKARA L9.22

State ownership Defining which variables form an object s state We wish to consider only that which the object owns Ownership Not explicitly specified in the language Element of program design L9.45 State ownership: Encapsulation and ownership go together Object encapsulates the state it owns Owns the state it encapsulates Owner gets to decide on the locking protocol If you publish a reference to a mutable object? You no longer have exclusive control L9.46 SLIDES CREATED BY: SHRIDEEP PALLICKARA L9.23

Instance confinement Object may not be thread-safe But we could still use it in a thread-safe fashion Ensure that: It is accessed by only one thread All accesses guarded by a lock L9.47 Confinement and locking working together public class PersonSet { private final Set<Person> myset = new HashSet<Person>(); public synchronized void addperson(person p) { myset.add(p); public synchronized boolean containsperson(person p) { return myset.contains(p); L9.48 SLIDES CREATED BY: SHRIDEEP PALLICKARA L9.24

Looking at our previous example State of PersonSet managed by HashSet, which is not threadsafe But myset is Private Not allowed to escape Confined to PersonSet L9.49 But we have made no assumptions about Person If it is mutable, additional synchronization is needed When accessing Person from PersonSet Reliable way to achieve this? Make Person thread-safe Less-reliable way? Guard Person objects with a lock Ensure that clients follow protocol of acquiring appropriate lock, before accessing Person L9.50 SLIDES CREATED BY: SHRIDEEP PALLICKARA L9.25

Instance confinement is the easiest way to build thread-safe classes Class that confines it state can be analyzed for thread-safety Without having to examine the whole program L9.51 GUARDING STATE WITH PRIVATE LOCKS L9.52 SLIDES CREATED BY: SHRIDEEP PALLICKARA L9.26

Guarding state with a private lock public class PrivateLock { private final Object mylock = new Object(); private Widget widget; //guarded by mylock public void somemethod() { synchronized(mylock) { //Access and modify the state of the widget L9.53 Why guard state with a private lock? Doing so encapsulates the lock Client code cannot acquire it! Publicly accessible lock allows client code to participate in its synchronization policy Correctly or incorrectly Clients that improperly acquire an object s lock cause liveness issues Verifying correctness with public locks requires examining the entire program not just a class L9.54 SLIDES CREATED BY: SHRIDEEP PALLICKARA L9.27

The contents of this slide-set are based on the following references Java Concurrency in Practice. Brian Goetz, Tim Peierls, Joshua Bloch, Joseph Bowbeer, David Holmes, and Doug Lea. Addison-Wesley Professional. ISBN: 0321349601/978-0321349606. [Chapters 3 and 4] L9.55 SLIDES CREATED BY: SHRIDEEP PALLICKARA L9.28