Component-Based Software Engineering Understanding Thread Safety Paul Krause.

Slides:



Advertisements
Similar presentations
Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Advertisements

50.003: Elements of Software Construction Week 6 Thread Safety and Synchronization.
Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Locks (Java 1.5) Only one thread can hold a lock at once –Other threads that try to acquire it block (or become suspended) until lock becomes available.
Thread synchronization Example:Producer/Consumer Relationship Buffer –Shared memory region Producer thread –Calls produce method to add item to buffer.
System Programming Practical Session 5 Liveness, Guarded Methods, and Thread Timing.
Chapter 7 Threads  Threads & Processes  Multi Threading  Class Thread  Synchronized Methods  Wait & Notify.
11-Jun-15 Producer-Consumer An example of using Threads.
Object-Oriented Software Engineering Concurrent Programming.
16-Jun-15 Producer-Consumer An example of using Threads.
Synchronization in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Concurrency Java Threads. Fundamentals Concurrency defines parallel activity Synchronization is necessary in order for parallel activities to share results.
Concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional.
1 CSCD 330 Network Programming Lecture 13 More Client-Server Programming Sometime in 2014 Reading: References at end of Lecture.
Lecture 4 : JAVA Thread Programming
Internet Software Development More stuff on Threads Paul Krause.
Locks CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads.
CSE 219 Computer Science III Multithreaded Issues.
Threads some important concepts Simon Lynch
Critical Reference An occurrence of a variable v is defined to be critical reference: a. if it is assigned to in one process and has an occurrence in another.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Online Appointment Book Implement a Client/Server application for an online appointment book. Client should be a Graphical User Interface application.
Lecture 2: Classes and Objects, using Scanner and String.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
1 CMSC 341: Data Structures Nilanjan Banerjee Data Structures University of Maryland Baltimore County
Internet Software Development Controlling Threads Paul J Krause.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Threading Eriq Muhammad Adams J
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Advanced Programming 2004, based on LY Stefanus’s slides slide 8.1 Multithreading : Thread Scheduling ThreadGroup.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Java Thread and Memory Model
Threading and Concurrency COM379T John Murray –
Li Tak Sing COMPS311F. Threads A thread is a single sequential flow of control within a program. Many programming languages only allow you to write programs.
COMPSCI 230 S2C 2015 Software Design and Construction Synchronization (cont.) Lecture 4 of Theme C.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 11: October 5, 2010 Instructor: Bhuvan Urgaonkar.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
Software and Threading Geza Kovacs Maslab Abstract Design: State Machines By using state machine diagrams, you can find flaws in your behavior without.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Threads and Singleton. Threads  The JVM allows multiple “threads of execution”  Essentially separate programs running concurrently in one memory space.
 2002 Prentice Hall, Inc. All rights reserved. Outline HoldIntegerUnsyn chronized.java Line 4 Lines 7-13 Lines // Fig. 15.6: HoldIntegerUnsynchronized.java.
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
1 Java Programming Java Programming II Concurrent Programming: Threads ( I)
1 Java Programming Java Programming II Concurrent Programming: Threads ( II)
Comp1004: Introduction III Java. Content How Java Works: The JVM Writing a Class in Java – Class – Member Variables – Method – Statement Magic incantations.
CSCD 330 Network Programming
Threads Chate Patanothai.
Object-Oriented Software Engineering
Introduction to Computer Programming
CSC 113 Tutorial QUIZ I.
Lecture 9: Process Synchronization
Condition Variables and Producer/Consumer
Condition Variables and Producer/Consumer
More on Thread Safety CSE451 Andrew Whitaker.
Multithreading.
Programming with Shared Memory Java Threads and Synchronization
class PrintOnetoTen { public static void main(String args[]) {
Component-Based Software Engineering
Threads and Multithreading
CSCD 330 Network Programming
Problems with Locks Andrew Whitaker CSE451.
Software Engineering and Architecture
Threads CSE451 Andrew Whitaker TODO: print handouts for AspectRatio.
some important concepts
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Component-Based Software Engineering Understanding Thread Safety Paul Krause

Lecture 9 Contents  A bit more on Locks  Deadlock  Race conditions  Starvation  Non-atomic operations

Locks  Every object has a lock You lock an object by synchronizing on it You lock an object by synchronizing on it public void addElement(Object item) { synchroniized(myArrayList) { // do the stuff }} public boolean elementExists(Object item) { return myArrayList.contains(item) }  To be absolutely accurate, synchronize on access methods too

What to Lock? public class McBurgerPlace { private static Person ceo = new Person(); private Object floor; private Object sodaFountain; public static synchronized Person receiveCeo() { return ceo; } public synchronized void waxFloor() {// do stuff … } public Object getSodaFountain() { synchronized(sodaFountain) { // do stuff … } }}

Non-implicit locking  If threadA is receiving the ceo, can threadB wax the floor?  If threadB is waxing the floor, can threadC get the soda fountain?  The answer is Yes in both cases The JVM doesn’t do nested locking The JVM doesn’t do nested locking

Locking objects not members public class LockObjectNotMemberVariables { private List myList = new ArrayList(); private List myList = new ArrayList(); public static void main(String[] args) { public static void main(String[] args) { LockObjectNotMemberVariables lonmv LockObjectNotMemberVariables lonmv = new LockObjectNotMemberVariables(); lonmv.lockTest(); lonmv.lockTest(); } public synchronized void lockTest() { public synchronized void lockTest() { System.out.println("Is THIS object locked? " + System.out.println("Is THIS object locked? " +Thread.holdsLock(this)); System.out.println("Is the list object locked? " + System.out.println("Is the list object locked? " +Thread.holdsLock(myList)); }}

Locking objects not members init:deps-jar: Created dir: /Users/paulkrause/Java/threading/build/classes Compiling 1 source file to /Users/paulkrause/Java/threading/build/classes compile-single:run-single: Is THIS object locked? true Is the list object locked? false BUILD SUCCESSFUL (total time: 4 seconds)

Locking classes not instances public class ClassLockNotObjectLock { public static void main(String[] args) { public static void main(String[] args) { lockTest(); lockTest(); } public static synchronized void lockTest() { public static synchronized void lockTest() { ClassLockNotObjectLock clnol ClassLockNotObjectLock clnol = new ClassLockNotObjectLock(); System.out.println("Is the Class locked? " System.out.println("Is the Class locked? " + Thread.holdsLock(clnol.getClass())); System.out.println("Is the object instance System.out.println("Is the object instance locked? " + Thread.holdsLock(clnol)); }}

Locking classes not instances init:deps-jar: Compiling 1 source file to /Users/paulkrause/Java/threading/build/classes compile-single:run-single: Is the Class locked? true Is the object instance locked? false BUILD SUCCESSFUL (total time: 4 seconds)

What to lock?  Synchronising on Objects other than “ this ” provides more concurrency  But controlling locks on such a fine scale can be inefficient if a method needs to access several objects  The safest general policy is not to allow unsynchronized access to the resources you need to lock and synchronize methods

Thread Safety Examples  Deadlock First thread gets Lock 1 and then tries to get Lock 2 First thread gets Lock 1 and then tries to get Lock 2 Second thread gets Lock 2 and then tries to get Lock 1 Second thread gets Lock 2 and then tries to get Lock 1  Race Conditions Two threads race for a common object Two threads race for a common object  Starvation A thread is never/rarely allowed to execute A thread is never/rarely allowed to execute

Deadlock public void run() { String name = Thread.currentThread().getName(); synchronized(lockA) { System.out.println(name + ": locked" + lockA); delay(name); System.out.println(name + ": trying to get " + lockB); synchronized(lockB) { System.out.println(name + ": locked" + lockB); } } }

Example run init:deps-jar: Compiling 1 source file to /Users/paulkrause/Java/threading/build/classes compile-single:run-single: Thread-0: lockedLock 1 Thread-0: delaying 1 second Thread-1: lockedLock 2 Thread-1: delaying 1 second Thread-0: trying to get Lock 2 Thread-1: trying to get Lock 1  Both threads now blocked

Race condition example if (Math.random() >.5) { peter.start();paul.start(); } else { } else { paul.start(); paul.start(); peter.start(); peter.start(); }

Race condition example public void run() { System.out.println(getName() + ": trying for lock on " + server); synchronized(server) { System.out.println(getName() + ": has lock on " + server); // wait 2 seconds: show the other thread is really // blocked try { Thread.sleep(2000); } catch (InterruptedException ie) { ie.printStackTrace();} System.out.println(getName() + ": releasing lock "); }

Example run (I) run-single: Peter: trying for lock on the common object Peter: has lock on the common object Paul: trying for lock on the common object Peter: releasing lock Paul: has lock on the common object Paul: releasing lock BUILD SUCCESSFUL (total time: 5 seconds)

Example run (II) run-single: Paul: trying for lock on the common object Paul: has lock on the common object Peter: trying for lock on the common object Paul: releasing lock Peter: has lock on the common object Peter: releasing lock BUILD SUCCESSFUL (total time: 8 seconds)

Starvation Example for (int i = 0; i < 4; i++) { // create a runner Runner r = new Runner(); r.setPriority(Thread.MAX_PRIORITY); // set the first thread to starve if (i == 0) { r.setPriority(Thread.MIN_PRIORITY); r.setName("Starvation Thread"); } // start the thread r.start(); r.yield(); // optional line }

Example run run-single: Starvation Thread: is working 10 Thread-1: is working 10 Thread-2: is working 10 Thread-1: is working 9 Thread-2: is working 9 Thread-1: is working 8 Thread-2: is working 8 Thread-1: is working 7 Thread-2: is working 7 Thread-1: is working 6 Thread-2: is working 6 Thread-1: is working 5 Thread-2: is working 5 Thread-1: is working 4 Thread-2: is working 4 Thread-1: is working 3 Thread-2: is working 3 Thread-1: is working 2 Thread-2: is working 2 Thread-1: is working 1 Thread-2: is working 1 Thread-3: is working 10 Thread-3: is working 9 Thread-3: is working 8 Thread-3: is working 7 Thread-3: is working 6 Thread-3: is working 5 Thread-3: is working 4 Thread-3: is working 3 Thread-3: is working 2 Thread-3: is working 1 Starvation Thread: is working 9 Starvation Thread: is working 8 Starvation Thread: is working 7 Starvation Thread: is working 6 BUILD SUCCESSFUL (total time: 1 second)

Atomic Operations  Synchronization blocks some code as “atomic”  A thread will not be swapped out in the middle of an atomic operation  Be careful in assuming any statements are atomic!

Non-atomic operations  x = 45;  Is atomic if x is an int  Is not atomic if x is double or long One operation for the high 32 bits, one for the low 32 bits One operation for the high 32 bits, one for the low 32 bits

Non-atomic operations  Treat x = 7; y = x++;  As x = 7; int temp = x + 1; x = temp; y = x; A thread swap here could lead to unexpected results

Class NonAtomic.java  Class has a static variable static int x; static int x;  The start ten threads that each do the following: for (int i= 0; i < 10; i++) { int reference = (int) (Math.random() * 100); x = reference; // some calculation to make a slight delay if (x == reference) { validCounts++; } else { } else { invalidCounts++; invalidCounts++;}}

Example run run-single: Thread-1 valid: 10 invalid: 0 Thread-6 valid: 10 invalid: 0 Thread-0 valid: 9 invalid: 1 Thread-3 valid: 7 invalid: 3 Thread-7 valid: 8 invalid: 2 Thread-8 valid: 7 invalid: 3 Thread-4 valid: 5 invalid: 5 Thread-9 valid: 8 invalid: 2 Thread-2 valid: 6 invalid: 4 Thread-5 valid: 6 invalid: 4 BUILD SUCCESSFUL (total time: 3 seconds)

Problem fixed for (int i= 0; i < 10; i++) { synchronized(NonAtomic.class) { int reference = (int) (Math.random() * 100); x = reference; // Do something intensive here if (x == reference) { validCounts++; } else { invalidCounts++;}}

New run run-single: Thread-0 valid: 10 invalid: 0 Thread-7 valid: 10 invalid: 0 Thread-2 valid: 10 invalid: 0 Thread-1 valid: 10 invalid: 0 Thread-5 valid: 10 invalid: 0 Thread-3 valid: 10 invalid: 0 Thread-8 valid: 10 invalid: 0 Thread-6 valid: 10 invalid: 0 Thread-9 valid: 10 invalid: 0 Thread-4 valid: 10 invalid: 0 BUILD SUCCESSFUL (total time: 3 seconds)