16-Jun-15 Java Threads Fine grained, shared state.

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.
Concurrent Programming Abstraction & Java Threads
Concurrency The need for speed. Why concurrency? Moore’s law: 1. The number of components on a chip doubles about every 18 months 2. The speed of computation.
Written by: Dr. JJ Shepherd
Concurrency 101 Shared state. Part 1: General Concepts 2.
1 CSC321 §2 Concurrent Programming Abstraction & Java Threads Section 2 Concurrent Programming Abstraction & Java Threads.
Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Designing a thread-safe class  Store all states in public static fields  Verifying thread safety is hard  Modifications to the program hard  Design.
Slides 8d-1 Programming with Shared Memory Specifying parallelism Performance issues ITCS4145/5145, Parallel Programming B. Wilkinson Fall 2010.
Multithreading The objectives of this chapter are:
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.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
DISTRIBUTED AND HIGH-PERFORMANCE COMPUTING CHAPTER 7: SHARED MEMORY PARALLEL PROGRAMMING.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
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.
29-Jun-15 Java Concurrency. Definitions Parallel processes—two or more Threads are running simultaneously, on different cores (processors), in the same.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
Week 9 Building blocks.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
50.003: Elements of Software Construction Week 8 Composing Thread-safe Objects.
8-Oct-15 Threads and Multithreading. 2 Thread s A Thread is a single flow of control When you step through a program, you are following a Thread A Thread.
Threading and Concurrency Issues ● Creating Threads ● In Java ● Subclassing Thread ● Implementing Runnable ● Synchronization ● Immutable ● Synchronized.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
Internet Software Development Controlling Threads Paul J Krause.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Synchronizing threads, thread pools, etc.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Sharing Objects  Synchronization  Atomicity  Specifying critical sections  Memory visibility  One thread’s modification seen by the other  Visibility.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Multithreading [Modified]
Java Thread and Memory Model
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
Written by: Dr. JJ Shepherd
System Programming Practical Session 4: Concurrency / Safety.
L6: Threads “the future is parallel” COMP206, Geoff Holmes and Bernhard Pfahringer.
Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.
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.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
Java Thread Programming
Multithreading / Concurrency
Multi Threading.
Background on the need for Synchronization
Multithreaded Programming in Java
Threads and Multithreading
Multithreading.
Shared Memory Programming
Threads Chapter 4.
Java Concurrency 17-Jan-19.
Fine grained, shared state
Java Concurrency.
Java Concurrency.
Classes, Objects and Methods
Threads and Multithreading
Fine grained, shared state
Programming with Shared Memory Specifying parallelism
Java Concurrency 29-May-19.
Fine grained, shared state
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

16-Jun-15 Java Threads Fine grained, shared state

2 Definitions Parallel processes—two or more Threads are running simultaneously, on different cores (processors), in the same computer Concurrent processes—two or more Threads are running asynchronously, on different cores (processors), in the same computer Asynchronous means that you cannot tell whether operation A in Thread #1 happens before, during, or after operation B in Thread #2 Asynchronous processes may be running simultaneously, on different cores, or they may be sharing time on the same core 2

3 Problems Concurrency can lead to data corruption: Race conditions—if two or more processes try to write to the same data space, or one tries to write and one tries to read, it is indeterminate which happens first Concurrency can lead to “freezing up” and other flow problems: Deadlock—two or more processes are each waiting for data from the other, or are waiting for the other to finish Livelock—two or more processes each repeatedly change state in an attempt to avoid deadlock, but in so doing continue to block one another Starvation—a process never gets an opportunity to run, possibly because other processes have higher priority 3

4 Why bother with concurrency? We use concurrency to make programs “faster” “Faster” may mean more responsive We need threads, even on single core machines, to move slow operations out of the GUI “Faster” may mean the computation completes sooner We can: Break a computation into separate parts Distribute these partial computations to several cores Collect the partial results into a single result Thread creation, communication between threads, and thread disposal constitutes overhead, which is not present in the sequential version Due to overhead costs, it is not unusual for first attempts at using concurrency to result in a slower program Really getting much speedup requires lots of experimentation, timing tests, and tuning the code Good performance is not platform independent

5 Threads There are two ways to create a Thread: Define a class that extends Thread Supply a public void run() method Create an object o of that class Tell the object to start: o.start(); Define a class that implements Runnable (hence it is free to extend some other class) Supply a public void run() method Create an object o of that class Create a Thread that “knows” o : Thread t = new Thread(o); Tell the Thread to start: t.start(); 5

6 Thread pools A thread pool is a collection of resuable threads This can save a lot of the overhead of creating and disposing of threads Very basic introduction (Java 5+): import java.util.concurrent.*;... ExecutorService exec = Executors.newFixedThreadPool(20); Create some Runnable objects (objects that implement public void run() ) exec.execute( Some Runnable object )

7 Mutable and immutable objects If an object is immutable (cannot be changed), then any number of Threads may read this object (or different portions of this object) at any time Sun provides a number of immutable objects You can create an ad hoc immutable object by simply not providing any way to change it All fields must be final ( private may not be enough) No methods may change any of the object’s data You must ensure no access to the object until after it is completely constructed If an object is mutable (can be changed), and accessible by more than one Thread, then every access (write or read) to it must be synchronized Don’t try to find clever reasons to think you can avoid synchronization 7

8 The synchronized statement in Java Synchronization is a way of providing exclusive access to data You can synchronize on any Object, of any type If two Threads try to execute code that is synchronized on the same object, only one of them can execute at a time; the other has to wait synchronized (someObject) { /* some code */ } This works whether the two Threads try to execute the same block of code, or different blocks of code that synchronize on the same object Often, the object you synchronize on bears some relationship to the data you wish to manipulate, but this is not at all necessary Fundamental rule: If a mutable data item can be accessed by more than one thread, then every access to it, everywhere, must be synchronized. No exceptions! 8

9 synchronized methods in Java Instance methods can be synchronized: synchronized public void myMethod( /* arguments */) { /* some statements */ } This is equivalent to public void myMethod( /* arguments */) { synchronized(this) { /* some statements */ } } Static methods can also be synchronized They are synchronized on the class object (a built-in object that represents the class) 9

10 Synchronizing in Scala Same concepts, slightly different syntax To synchronize on an object: myObject.synchronized { // code block } To synchronize a method: def myMethod = synchronized { // code block }

11 Locks When a Thread enters a synchronized code block, it gets a lock on the monitor (the Object that is used for synchronization) The Thread can then enter other code blocks that are synchronized on the same Object That is, if the Thread already holds the lock on a particular Object, it can use any code also synchronized on that Object A Thread may hold a lock on many different Objects One way deadlock can occur is when Thread A holds a lock that Thread B wants, and Thread B holds a lock that Thread A wants

12 Atomic actions An operation, or block of code, is atomic if it happens “all at once,” that is, no other Thread can access the same data while the operation is being performed x++; looks atomic, but at the machine level, it’s actually three separate operations: 1.load x into a register 2.add 1 to the register 3.store the register back in x Suppose you are maintaining a stack as an array: void push(Object item) { this.top = this.top + 1; this.array[this.top] = item; } You need to synchronize this method, and every other access to the stack, to make the push operation atomic Atomic actions that maintain data invariants are thread-safe; compound (non-atomic) actions are not This is another good reason for encapsulating your objects

13 Data invariants Any publicly available method that modifies an object should take it from one valid state to another valid state A data invariant is a logical condition (possibly quite complex) that describes what it means for an object to be valid Any method that “partially” updates an object must be private This is a fundamental rule of all object-oriented programming Any method that modifies a shared object must be atomic Example: Suppose you have a Fraction object with value 10/15 You want to reduce this Fraction to lowest terms: 2/3 It is unsafe to modify the numerator atomically and the denominator atomically; they must both be changed in a single atomic operation

14 Check-then-act A Vector is like an ArrayList, but is synchronized Hence, the following code looks reasonable: if (!myVector.contains(someObject)) { // check myVector.add(someObject); // act } But there is a “gap” between checking the Vector and adding to it During this gap, some other Thread may have added the object to the array Check-then-act code, as in this example, is unsafe You must ensure that no other Thread executes during the gap synchronized(myVector) { if (!myVector.contains(someObject)) { myVector.add(someObject); } } So, what good is it that Vector is synchronized? It means that each call to a Vector operation is atomic 14

15 Synchronization is on an object Synchronization can be done on any object Synchronization is on objects, not on variables Suppose you have synchronized(myVector) { … } Then it is okay to modify myVector —that is, change the values of its fields It is not okay to say myVector = new Vector(); Synchronization is expensive Synchronization entails a certain amount of overhead Synchronization limits parallelism (obviously, since it keeps other Threads from executing) Moral: Don’t synchronize everything! 15

16 Local variables A variable that is strictly local to a method is thread-safe This is because every entry to a method gets a new copy of that variable If a variable is of a primitive type ( int, double, boolean, etc.) it is thread-safe If a variable holds an immutable object (such as a String ) it is thread-safe, because all immutable objects are thread-safe If a variable holds a mutable object, and there is no way to access that variable from outside the method, then it can be made thread- safe An Object passed in as a parameter is not thread-safe (unless immutable) An Object returned as a value is not thread-safe (unless immutable) An Object that has references to data outside the method is not thread-safe

17 Thread deaths A Thread “dies” (finishes) when its run method finishes There are two kinds of Threads: daemon Threads and non- daemon Threads When all non-daemon Threads die, the daemon Threads are automatically terminated If the main Thread quits, the program will appear to quit, but other non- daemon Threads may continue to run These Threads will persist until you reboot your computer The join(someOtherThread) allows “this” Thread to wait for some other thread to finish 17

18 Communication between Threads Threads can communicate via shared, mutable data Since the data is mutable, all accesses to it must be synchronized Example: synchronized(someObj) { flag = !flag; } synchronized(someObj) { if (flag) doSomething(); } The first version of Java provided methods to allow one thread to control another thread: suspend, resume, stop, destroy These methods were not safe and were deprecated almost immediately — never use them! They are still there because Java never throws anything away If you want one Thread to control another Thread, do so via shared data 18

19 Use existing tools There’s no point in trying to make something thread-safe if a carefully crafted thread-safe version exists in the Java libraries java.util.concurrent has (among other goodies): ConcurrentHashMap ConcurrentLinkedQueue ThreadPoolExecutor FutureTask And java.util.concurrent.atomic has thread-safe methods on single variables, such as these in AtomicInteger : int addAndGet(int) int getAndAdd(int) boolean compareAndSet(int) void lazySet(int)

20 Advice Any data that can be made immutable, should be made immutable This applies especially to input data--make sure it’s completely read in before you work with it, then don’t allow changes All mutable data should be carefully encapsulated (confined to the class in which it occurs) All access to mutable data (writing and reading it) must be synchronized All operations that modify the state of data, such that validity conditions may be temporarily violated during the operation, must be made atomic (so that the data is valid both before and after the operation) Be careful not to leave Threads running after the program finishes

21 Debugging “Debugging can show the presence of errors, but never their absence.” -- Edgser Dijkstra Concurrent programs are nondeterministic: Given exactly the same data and the same starting conditions, they may or may not do the same thing It is virtually impossible to completely test concurrent programs; therefore: Test the non-concurrent parts as thoroughly as you can Be extremely careful with concurrency; you have to depend much more on programming discipline, much less on testing Document your concurrency policy carefully, in order to make the program more maintainable in the future

22 The End