
Thread Safety HOM Introduction Introduction Thread Safety Thread Safety Locking Thread Safety Liveliness and Performance Summary lab Pieter van den Hombergh Fontys Hogeschool voor Techniek en Logistiek February 16, 2016 HOM/FHTenL Thread Safety February 16, 2016 1/22 Thread Safety HOM Introduction Introduction Introduction Introduction Thread Safety Thread Safety Locking Liveliness and Performance Summary Thread Safety lab Thread Safety Locking Liveliness and Performance Summary lab HOM/FHTenL Thread Safety February 16, 2016 2/22 Overview of Part I: Fundamentals Thread Safety HOM Introduction Introduction Introduction to threads Thread Safety Thread Safety Thread safety Locking Liveliness and Performance Sharing objects Summary lab Composing objects Building blocks Summary Links http://java.sun.com/docs/books/tutorial/ java/generics/index.html http://java.sun.com/docs/books/tutorial/ essential/concurrency/index.html HOM/FHTenL Thread Safety February 16, 2016 3/22 Introduction Thread Safety HOM History Introduction Benefits Introduction Risks: safety, liveness, performance Thread Safety Thread Safety safety Unpredictable ordering of operations in Locking Liveliness and Performance multiple threads (in absence of Summary synchronization). lab liveness A concurrent application’s ability to execute in a timely manner. performance Includes: poor service time, responsiveness, throughput, resource consumption, scalability. Threads are everywhere Your java program: a main thread Usefull classes/frameworks made available for you: Timer, Servlets and JavaServer Pages (JSP), Remote Method Invocation (RMI), Swing and AWT (GUI apps.). HOM/FHTenL Thread Safety February 16, 2016 4/22 Thread safety Thread Safety HOM If multiple threads access the same mutable state variable Introduction without appropriate synchronization, your program is broken. Introduction There are three ways to fix it: Thread Safety Thread Safety Locking Don’t share the state variable across threads Liveliness and Performance Make the state variable immutable; or Summary lab Use synchronization whenever accessing the state variable A definition: Thread safety A class is thread-safe if it behaves correctly when accessed from multiple threads, regardless of the scheduling or interleaving of the execution of those threads by running environment, and with no additional synchronization or other coordination on the part of the calling code. HOM/FHTenL Thread Safety February 16, 2016 5/22 An example from SUN: SimpleThreads.java Thread Safety HOM private static class MessageLoop implements Runnable{ public void run(){ Introduction String importantInfo[]={"Mares eat oats", Introduction "Does eat oats", "Little lambsThread eat ivy" Safety, "A kid will eat ivy too"}; Thread Safety try { Locking for (int i=0;i< importantInfo.length;i++){ Liveliness and Performance //Pause for 4 seconds Summary Thread.sleep(4000); lab //Print a message threadMessage(importantInfo[i]); } } catch (InterruptedException e){ threadMessage("I wasn’t done!"); } } } HOM/FHTenL Thread Safety February 16, 2016 6/22 An example from SUN: (2) Thread Safety HOM public static void main(String args[]) throws InterruptedException{ Introduction //Delay, in millis before we interrupt MessageLoop Introduction long patience= 1000*60*60; //thread (default 1 hour). Thread Safety //If command line argument present, prints patience. Thread Safety if (args.length>0){ Locking try { Liveliness and Performance patience= Long.parseLong(args[0])* 1000; Summary } catch (NumberFormatException e){ lab System.err.println("Argument must be an integer."); System.exit(1); } } threadMessage("Starting MessageLoop thread"); long startTime= System.currentTimeMillis(); Thread t= new Thread(new MessageLoop()); t.start(); threadMessage("Waiting for MessageLoop thread to finish"); HOM/FHTenL Thread Safety February 16, 2016 7/22 An example from SUN:(3) Thread Safety HOM while (t.isAlive()){ threadMessage("Still waiting..."); Introduction //Wait maximum of 1 second for MessageLoop thread Introduction //to finish. Thread Safety t.join(1000); Thread Safety if (((System.currentTimeMillis()-startTime)>patience) Locking &&t.isAlive()){ Liveliness and Performance threadMessage("Tired of waiting!"); Summary t.interrupt(); lab //Shouldn’t be long now -- wait indefinitely t.join(); } } threadMessage("Finally!"); } HOM/FHTenL Thread Safety February 16, 2016 8/22 Object orientation Thread Safety HOM Introduction Introduction Thread Safety When designing thread-safe classes, good Thread Safety object-oriented techniques - encapsulation, Locking Liveliness and Performance immutability, and clear specification of invariants - Summary are your best friends. lab Thread-safe classes encapsulate any needed synchronization so that clients need not provide their own. Stateless objects are always thread-safe. This includes memberless classes classes with only final members like java.lang.Integer and java.lang.Long HOM/FHTenL Thread Safety February 16, 2016 9/22 Atomicity Thread Safety HOM Even an increment operation: count++; is not atomic Introduction although it might look as if it is an indivisible operation! Introduction Thread Safety Compound actions: operations A and B are atomic Thread Safety Locking with respect to each other if, from the perspective of a Liveliness and Performance thread excuting A, when another thread executes B, Summary either all of B has executed or none has. An atomic lab operation is one that is atomic with respect to all operations, including itself, that operate on the same state. Where practical, use existing thread-safe objects, like java.util.concurrent.atomic.AtomicLong, to manage your class’s state. It is simpler to reason about the possible states and state transitions for existing thread-safe objects than it is for arbitrary state variables, and this makes it easier to maintain and verify thread safety. HOM/FHTenL Thread Safety February 16, 2016 10/22 Race conditions Thread Safety HOM Introduction A race condition occurs when the correctness of a Introduction Thread Safety computation depends on the relative timing or Thread Safety Locking interleaving of multiple threads by the runtime; in other Liveliness and Performance words: when getting the right answer depends on lucky Summary timing. lab Most encountered race condition: check-then-act: you observe something to be true, (file X doesn’t exist) and then take action based on that observation (create X); but in fact the observation could have become invalid between the time you obeserved it and the time you acted on it (someone else created X in the meantime), causing a problem (unexpected exception, overwritten data, file corruption). HOM/FHTenL Thread Safety February 16, 2016 11/22 Listing 2.3. Race condition in lazy initialization. Thread Safety HOM @NotThreadSafe public class LazyInitRace{ Introduction private ExpensiveObject instance= null; Introduction Thread Safety public ExpensiveObject getInstance(){ Thread Safety if (instance == null) Locking instance= new ExpensiveObject(); Liveliness and Performance return instance; Summary } lab } class ExpensiveObject{} HOM/FHTenL Thread Safety February 16, 2016 12/22 Locking Thread Safety HOM Introduction Introduction Thread Safety Thread Safety Locking Liveliness and Performance When multiple variables determine the state of an Summary object: just make all variables threadsafe??? lab Making all attribute objects threadsafe is not sufficient! ”To preserve state consistency (or invariants), update related state variables in a single atomic operation.” HOM/FHTenL Thread Safety February 16, 2016 13/22 Intrinsic Locks Thread Safety HOM Every object in java can act as a lock for purpose of Introduction synchronization! Introduction These locks are called intrinsic locks or monitor locks. Thread Safety Thread Safety Intrinsic locks in java act as mutexes: mutual Locking Liveliness and Performance , which means that at most one exclusion locks Summary thread may own the lock and can proceed. lab synchronized (lock){ // Access or modify shared state // guarded by lock } HOM/FHTenL Thread Safety February 16, 2016 14/22 Reentrancy Thread Safety HOM Introduction When a thread requests a lock that is already held by Introduction another thread, the requesting thread blocks. Thread Safety Thread Safety The request will however succeed if that same thread Locking holds the lock: locks are acquired on a per-thread basis. Liveliness and Performance Summary Reentrancy is implemented by associating an acquisition lab count and an owning thread with each lock . The lock is released when the count reaches zero, obviously after an exit of a synchronized block. Reentrancy simplifies development of object-oriented concurrent code and can avoid deadlock in situations such as in the code on the next slide. Note that this also works for synchronisation used in both super and subclass. In the object, the lock is the same. HOM/FHTenL Thread Safety February 16, 2016 15/22 Code that would deadlock Thread Safety HOM Code that would deadlock if intrinsic locks were not reentrant. Introduction Introduction public class Widget{ Thread Safety public synchronized void doSomething(){ Thread Safety ... Locking } Liveliness and Performance } Summary lab public class LoggingWidget extends Widget{ public synchronized void doSomething(){ System.out.println(toString()+ ": calling doSomething"); super.doSomething(); } } HOM/FHTenL Thread Safety February 16, 2016 16/22 Guarding states with locks Thread Safety HOM Introduction Introduction Thread Safety For each mutable
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages22 Page
-
File Size-