More on Thread Safety CSE451 Andrew Whitaker.

Slides:



Advertisements
Similar presentations
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Advertisements

METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
CS324e - Elements of Graphics and Visualization A Little Java A Little Python.
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 24 th, 2010 The University of Georgia.
Concurrency 101 Shared state. Part 1: General Concepts 2.
Concurrency and Thread Yoshi. Two Ways to Create Thread Extending class Thread – Actually, we need to override the run method in class Thread Implementing.
Threading Part 2 CS221 – 4/22/09. Where We Left Off Simple Threads Program: – Start a worker thread from the Main thread – Worker thread prints messages.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)
Assignment – no class Wednesday All: watch the Google Techtalk “Getting C++ Threads Right” by Hans Boehm at the following link in place of Wednesday’s.
Access to Names Namespaces, Scopes, Access privileges.
1 Where did the synchronized methods go? Yes, there still exists the synchronized keyword. public synchronized void foo() {} public void foo(){ aLock.lock();
Principle 1 If more than one thread accesses a given state variable and one of them might write to it then All accesses to the variable must be correctly.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Sharing Objects – Ch. 3 Visibility What is the source of the issue? Volatile Dekker’s algorithm Publication and Escape Thread Confinement Immutability.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
29-Jun-15 Java Concurrency. Definitions Parallel processes—two or more Threads are running simultaneously, on different cores (processors), in the same.
Multithreading.
Week 9 Building blocks.
Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads.
50.003: Elements of Software Construction Week 8 Composing Thread-safe Objects.
Threading and Concurrency Issues ● Creating Threads ● In Java ● Subclassing Thread ● Implementing Runnable ● Synchronization ● Immutable ● Synchronized.
In the name of Allah The Proxy Pattern Elham moazzen.
Internet Software Development Controlling Threads Paul J Krause.
Sharing Objects  Synchronization  Atomicity  Specifying critical sections  Memory visibility  One thread’s modification seen by the other  Visibility.
1 CSE 373 Introduction to Concurrency reading: Grossman slides created by Marty Stepp
COMPSCI 230 S2C 2015 Software Design and Construction Synchronization (cont.) Lecture 4 of Theme C.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Multiprocessor Cache Consistency (or, what does volatile mean?) Andrew Whitaker CSE451.
Software and Threading Geza Kovacs Maslab Abstract Design: State Machines By using state machine diagrams, you can find flaws in your behavior without.
System Programming Practical Session 4: Concurrency / Safety.
More on Thread Safety CSE451 Andrew Whitaker. Review: Thread Hazards Safety hazards  “Program does the wrong thing” Liveness hazards  “Program never.
System Programming Practical Session 4: Concurrency / Safety.
Component-Based Software Engineering Understanding Thread Safety Paul Krause.
Software Design 13.1 From controller to threads l Threads are lightweight processes (what’s a process?)  Threads are part of a single program, share state.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
pThread synchronization
Monitor Pattern Read only Collection views Synchronized Collections views Concurrent HashMap The Vehicle Tracker Example.
Sun Proprietary/Confidential: Internal Use Only 1 Multi-Threading Primer Byron Nevins December 10, 2007.
Deadlock CSE451 Andrew Whitaker. Review: What Can Go Wrong With Threads? Safety hazards  “Program does the wrong thing” Liveness hazards  “Program never.
Race Conditions & Synchronization
Chapter 10 Thinking in Objects
Multi-processor Scheduling
Chapter 10 Thinking in Objects
Chapter 10 Thinking in Objects
Chapter 9 Thinking in Objects
Synchronization Lecture 23 – Fall 2017.
Deadlock, Illustrated.
Synchronization Lecture 24 – Fall 2018.
Condition Variables and Producer/Consumer
Condition Variables and Producer/Consumer
Race Conditions & Synchronization
Thread Implementation Issues
Concurrency in Java Last Updated: Fall 2010 Paul Ammann SWE 619.
Synchronization Lecture 24 – Fall 2018.
Chapter 9 Thinking in Objects
Java Concurrency 17-Jan-19.
Namespaces, Scopes, Access privileges
Threads in Java James Brucker.
Java Concurrency.
Java Concurrency.
Threads and Multithreading
Lecture 8 Thread Safety.
Synchronization Lecture 24 – Fall 2018.
Java Concurrency 29-May-19.
Problems with Locks Andrew Whitaker CSE451.
CSE 332: Concurrency and Locks
Threads CSE451 Andrew Whitaker TODO: print handouts for AspectRatio.
Synchronization Lecture 24 – Fall 2018.
Threads and concurrency / Safety
Presentation transcript:

More on Thread Safety CSE451 Andrew Whitaker

Safety Rule #1 All shared, mutable state must be properly synchronized Usually with a synchronized block or method

Rule #2 Compound actions must be protected by a single lock Often, we talk about protecting an invariant with a single lock e.g., aspect ratio remains fixed

Why Not Make Every Method Synchronized? Synchronization has a performance cost Each lock access is a cache miss Synchronization limits parallelism Rampant synchronization leads to deadlock Amount of synchronization Available parallelism

Safety Rule #1, Revised All shared, mutable state must be properly synchronized Usually with a synchronized block or method Corollaries: Do not synchronize for non-shared state Accessed by a single thread Do not synchronize on immutable state

This is unnecessary! No need to synchronize on non-shared state. public class Foo { public int addSix(int arg) { synchronized (this) { // grab “this” lock arg += 6; } // release “this” lock return arg; } This is unnecessary! No need to synchronize on non-shared state.

Immutable state does not require synchronization! // Thread-safe public class Integer { private final int x; public Integer(int arg) { this.x = arg; } // hold “this” lock during entire method public synchronized int get() { // hold “this” return x;

Thread-safe Objects aString is thread-safe for any run method public class Bar extends Thread { private final String aString = “Hello”; public void run() { // .. Stuff in here… } aString is thread-safe for any run method The reference is immutable (final) The String class is immutable

Concurrent State in Java Immutable Private, Mutable Shared, Mutable Strings String StringBuilder StringBuffer Lists Collections.unmodifableList ArrayList, LinkedList Vector Methods are not synchronized All methods are synchronized

Documentation Diving: Java.util.ArrayList Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the list. If no such object exists, the list should be "wrapped" using the Collections.synchronizedList method.

Be Careful with Thread-safe Data Structures public class MyClass extends Thread { // Always thread-safe private static final Vector<String> someStrings = new Vector<String>(); // Not necessarily thread safe: StringBuilder is mutable private static final Vector<StringBuilder> someMoreStrings = new Vector<StringBuilder>(); public void run () { // do something with the lists of strings… } All reachable mutable state must be synchronized

Using Non-Thread-Safe Objects with Threads public class MyClass extends Thread { // This data structure is not thread-safe private static final List<String> someStrings = new ArrayList<String>(); public void run () { // But, that’s OK if all accesses are // synchronized on a single lock synchronized(someStrings) { someStrings.add(“fubar”); }

Example from Project #1 Do the execcounts counters require synchronized access? Yes: The counters are shared across multiple processes/threads The counters are mutable Solution: atomic variables See Linux /include/asm-i386/atomic.h

Another Example: AspectRatio

Linked List Example