Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Marco Piccioni, Bertrand Meyer. Java: concurrency

Similar documents
Java Threads. COMP 585 Noteset #2 1

Advanced Programming Concurrency

CS 159: Parallel Processing

Phaser volle Energie...

Chapter 32 Multithreading and Parallel Programming

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

-Aditya Bhave CSCI-5448 Graduate Presentation

Programmazione Avanzata e Paradigmi Ingegneria e Scienze Informatiche - UNIBO a.a 2013/2014 Lecturer: Alessandro Ricci

CS 351 Design of Large Programs Threads and Concurrency

Threads Chate Patanothai

Concurrency: a crash course

MultiThreading 07/01/2013. Session objectives. Introduction. Introduction. Advanced Java Programming Course

Advanced Java Programming Course. MultiThreading. By Võ Văn Hải Faculty of Information Technologies Industrial University of Ho Chi Minh City

CST242 Concurrency Page 1

Concurrency Utilities: JSR-166

CMSC 330: Organization of Programming Languages

Threads Questions Important Questions

27/04/2012. We re going to build Multithreading Application. Objectives. MultiThreading. Multithreading Applications. What are Threads?

Exercise Session Week 8

Exercise Session Week 8

Advanced Concepts of Programming

7. MULTITHREDED PROGRAMMING

CMSC 132: Object-Oriented Programming II. Threads in Java

Paradigmas de Computação Paralela

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

Contribution:javaMultithreading Multithreading Prof. Dr. Ralf Lämmel Universität Koblenz-Landau Software Languages Team

Only one thread can own a specific monitor

Threads. Definitions. Process Creation. Process. Thread Example. Thread. From Volume II

CONCURRENT API AND PARALLEL PROGRAMMING

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

UNIT V CONCURRENT PROGRAMMING

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

Threads and Java Memory Model

Parallel Programming Practice

Problems with Concurrency. February 19, 2014

What is a thread anyway?

CMSC 330: Organization of Programming Languages

CS 556 Distributed Systems

Performance Throughput Utilization of system resources

Java s Implementation of Concurrency, and how to use it in our applications.

Threads and Locks, Part 2. CSCI 5828: Foundations of Software Engineering Lecture 08 09/18/2014

COMP 322: Fundamentals of Parallel Programming

Parallel Programming Practice

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

Basics of. Multithreading in Java

Multitasking. Multitasking allows several activities to occur concurrently on the computer Levels of multitasking: Process based multitasking

Java Threads. Introduction to Java Threads

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling

Concurrent Programming using Threads

Introduction to Java Threads

Daemon thread, 7 Deadlock, 27

CONCURRENCY IN JAVA Course Parallel Computing

Part IV Other Systems: I Java Threads

Computation Abstractions. CMSC 330: Organization of Programming Languages. So, What Is a Thread? Processes vs. Threads. A computer.

Java Concurrency. Towards a better life By - -

G Programming Languages Spring 2010 Lecture 13. Robert Grimm, New York University

Multi-Threaded Programming Design CSCI 201 Principles of Software Development

SSC - Concurrency and Multi-threading Java multithreading programming - Synchronisation (II)

Concurrent Programming Benoît Garbinato

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

Concurrent Programming

Overview. Processes vs. Threads. Computation Abstractions. CMSC 433, Fall Michael Hicks 1

CS11 Java. Fall Lecture 7

COMP 322: Fundamentals of Parallel Programming. Lecture 30: Java Synchronizers, Dining Philosophers Problem

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

Threads and Parallelism in Java

An Introduction to Programming with Java Threads Andrew Whitaker University of Washington 9/13/2006. Thread Creation

Multithreading. Dr. Jens Bennedsen, Aarhus University, School of Engineering Aarhus, Denmark

CS193k, Stanford Handout #8. Threads 3

CISC 4700 L01 Network & Client-Server Programming Spring Cowell Chapter 15: Writing Threaded Applications

Project. Threads. Plan for today. Before we begin. Thread. Thread. Minimum submission. Synchronization TSP. Thread synchronization. Any questions?

Concurrent Computing CSCI 201 Principles of Software Development

COMP346 Winter Tutorial 4 Synchronization Semaphores

Parallel & Concurrent Programming

JAVA CONCURRENCY FRAMEWORK. Kaushik Kanetkar

Multithreaded Programming

Programming Language Concepts: Lecture 11

Week 7. Concurrent Programming: Thread Synchronization. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

COMP 213. Advanced Object-oriented Programming. Lecture 23. Shared Variables and Synchronization

Monitors; Software Transactional Memory

CMSC 433 Programming Language Technologies and Paradigms. Concurrency

F. Tip and M. Weintraub CONCURRENCY

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

The New Java Technology Memory Model

Faculty of Computers & Information Computer Science Department

Java Monitors. Parallel and Distributed Computing. Department of Computer Science and Engineering (DEI) Instituto Superior Técnico.

Models of concurrency & synchronization algorithms

CMSC 330: Organization of Programming Languages. Threads Classic Concurrency Problems

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

Programming in Parallel COMP755

Note: Each loop has 5 iterations in the ThreeLoopTest program.

Multiple Inheritance. Computer object can be viewed as

Parallel Task Executor in Java

Concurrency - Topics. Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads

Informatica 3. Marcello Restelli. Laurea in Ingegneria Informatica Politecnico di Milano 9/15/07 10/29/07

CMSC 330: Organization of Programming Languages. The Dining Philosophers Problem

Module - 4 Multi-Threaded Programming

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

Object Oriented Programming. Week 10 Part 1 Threads

Transcription:

Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Java: concurrency

Outline Java threads thread implementation sleep, interrupt, and join threads that return values Thread synchronization implicit locks and synchronized blocks synchronized methods producer/consumer example Other concurrency models executors and thread pools explicit locks and semaphores thread-safe collections fork/join parallelism 2

Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Java threads

Java threads Java s concurrency model is based on threads implemented natively in the JVM Threads are created by instantiating class Thread Each instance is associated with a class providing the code associated with the thread Two ways to provide the class code Write a class that implements interface Runnable Write a class that inherits from class Thread We focus on the first solution, which is a bit more flexible Why? 4

Implementing interface Runnable Any implementation of Runnable must implement method run(). public class DumbThread implements Runnable { String id; public DumbThread(String id) { this.id = id; public void run() { // do something when executed System.out.println("This is thread " + id); 5

Starting a thread Create a Thread object Thread mt = new Thread(new DumbThread( mt )); Start its execution (calls run()) mt.start(); Optionally, wait for it to terminate mt.join(); // wait until mt terminates System.out.println( terminated"); "The thread has 6

Putting a thread to sleep The sleep(int t) static method suspends the thread in which it is invoked for t milliseconds, or until an interrupt is received Thread.sleep(2000); // suspend for 2 seconds sleep throws an InterruptedException if an interrupt occurs even if no interrupt occurs, the timing may be more or less precise according to the real-time guarantees of the running JVM 7

Threads that return values The generic interface Callable<G> is a variant of Runnable for threads returning values of type G. must implement method call() import java.util.concurrent.*; public class CalThread implements Callable<String> { String id; public CalThread(String id) { this.id = id; public String call() { return Thread with id: + id; Callable objects are run using executors (see later) 8

Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Thread synchronization

Implicit locks and synchronized blocks Synchronized blocks (a.k.a. synchronized statements) support synchronization based on locks. blocks of statements guarded by synchronized the lock itself can be any object (including this) locking/unlocking is implicit when entering/exiting the block named intrinsic locks useful to define critical regions and fine-grained synchronization 10

Implicit locks and synchronized blocks Synchronized blocks (a.k.a. synchronized statements) support synchronization based on locks. // s must be accessed in mutual exclusion private int s; // dict is used read-only, so no concurrency problems private LinkedList<String> dict; public String decrement_and_lookup() { // critical region synchronized(this) { if (s > 0) { s = s - 1; // non-critical region return dict.get(s); 11

Synchronized methods Java implements monitors as synchronized methods. When a thread is executing a synchronized method for an object, all other threads executing synchronized methods on the same object wait (i.e., they block execution). it is as if the method acquires an implicit lock on the object and does not release it until it s done 12

Synchronized methods Java implements monitors as synchronized methods synchronized methods coordinate with the primitives wait: suspend and release the lock until some thread does a notify or notifyall notify: resume one suspended thread (chosen nondeterministically), which becomes ready for execution when possible notifyall: resume all suspended threads, which become ready for execution when possible There is no guarantee that notifications to waiting threads are fair 13

Thread states new New Thread start() Ready sleep() wait() notify() notifyall() Waiting computation terminates Dead 14

From running to waiting Running thread wait(); 1 current instruction 2 Ready threads Waiting threads wait(); wait(); wait(); wait(); wait(); x += 1; 3 wait(); wait(); wait(); wait(); x = 3; foo(x); 15

From waiting to ready Running thread notify(); 1 current instruction Ready threads Waiting threads wait(); wait(); wait(); wait(); wait(); x += 1; wait(); wait(); wait(); wait(); x = 3; foo(x); 2 16

The producer-consumer problem Two threads, the Producer and the Consumer, work concurrently on a shared Buffer of bounded size The Producer puts new messages in the buffer if the buffer is full, the Producer must wait until the Consumer takes some messages the Producer also signals the last message The Consumer takes messages from the buffer if the buffer is empty, the Consumer must wait until the Producer puts some new messages the Consumer terminates after the last message Consistent access to the Buffer requires locks and synchronization One way is to make Buffer a monitor class (with synchronized methods) 17

The main class public class ProducerConsumer { public static void Main(String[] args) { // create a buffer of size 3 Buffer b = new Buffer(3); // start the producer (new Thread(new Producer(b, END ))).start(); // start the consumer (new Thread(new Consumer(b, END ))).start(); 18

The shared Buffer (1/3) import java.util.*; public class Buffer { public Buffer(int max_size) { this.max_size = max_size; this.messages = new LinkedList<String>(); // buffer of messages, managed as a queue private LinkedList<String> messages; // maximum number of elements in the buffer private int max_size; 19

The shared Buffer (2/3) public synchronized String take() { while (messages.size() == 0) { wait(); // may throw InterruptedException // now the buffer is not empty // and we have exclusive access to it String m = messages.remove(); // any thread waiting for a slot in the buffer notifyall(); // return the message on top of the buffer return m; 20

The shared Buffer (3/3) public synchronized void put(string msg) { while (messages.size() == max_size) { wait(); // may throw InterruptedException // now the buffer has at least an empty slot // and we have exclusive access to it messages.offer(msg); // any thread waiting for a message to take notifyall(); // end of class Buffer 21

The Producer (1/2) public class Producer implements Runnable { // reference to the shared buffer private Buffer b; // the last message to be sent private String endmsg; // set the reference to the buffer and endmsg public Producer(Buffer b, String endmsg) { this.b = b; this.endmsg = endmsg; 22

The Producer (2/2) public void run() { // work for 20 turns for (int i = 0; i < 20; i++) { // put a message in the buffer b.put(integer.tostring(i)); // last message signals end b.put(endmsg); 23

The Consumer (1/2) public class Consumer implements Runnable { // reference to the shared buffer private Buffer b; // the last message to be sent private String endmsg; // set the reference to the buffer and endmsg public Consumer(Buffer b, String endmsg) { this.b = b; this.endmsg = endmsg; 24

The Consumer (2/2) public void run() { String m = ; // assume endmsg!= // work until endmsg is received for (int i = 0;!m.equals(endMsg); i++) { // take a message from the buffer m = b.take(); System.out.println( "Consumer has consumed message: " + m); 25

Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Other concurrency models

Concurrency and performance Thread creation is time-consuming massive thread creation can annihilate responsiveness Java s solution: executors and thread pools Synchronized blocks and methods are not very efficient Java s solution explicit locks (lightweight) atomic variables (semaphores) Tip: don t forget the efficiency/abstraction trade-off 27

Concurrency and correctness Programming thread-safe data structures is error-prone Java s solution: concurrent collections Threads and monitors are too general for straightforward parallel computation Java s solution: fork/join tasks Tip: don t forget the efficiency/abstraction trade-off 28

Executors and thread pools Executors are object services that run threads Thread pools are an efficient way of implementing executors maintain a pool of worker threads when a client requests a new task to run, preempt one of the available worker threads and assign it to the task no creation overhead upon task invocation Java has three interfaces for the services of an executor Executor: defines executor method for Runnable objects ExecutorService: supports Runnable and Callable objects ScheduledExecutorService: supports scheduled execution (at a given time) 29

Class java.util.concurrent.executors This class is an object factory for several efficient implementations of executors (mostly with thread pools) newfixedthreadpool: returns an executor that uses a thread pool of fixed size newcachedthreadpool: returns an executor that uses a thread pool of variable size use submit to send Runnable or Callable objects to be executed Class Executors also provide implementations of ScheduledExecutionService 30

Executors vs. standard thread creation Without executors With executors Thread t1 = new Thread(new T_a()); Thread t2 = new Thread(new T_a()); Thread t3 = new Thread(new T_b()); t1.start(); t2.start(); t3.start(); ExecutorService e = Executors.newCachedThre adpool(); t1 = new T_a(); t2 = new T_a(); t3 = new T_b(); e.submit(t1); e.submit(t2); e.submit(t3); 31

Executing a Callable object You can submit a Callable object to an executor The executor returns a Future object, used to read the result of the execution returned by the thread ExecutorService e = new CachedThreadPool(); Callable<G> t = new acallableclassreturningg(); Future<G> f = e.submit(t); G result = f.get(); // may throw an exception 32

Explicit locks Java locks in package java.util.concurrent.lock provide: explicit locking mechanisms: lock: acquire the lock if available, and wait until it becomes available otherwise lockinterruptibly: try to lock, but waiting can be interrupted trylock: if lock available, acquire it immediately and return true if lock not available, return false (and don t wait) unlock: release the lock more complex reentrant locking mechanisms wait for a specific signal or condition query the lock to know how many threads are waiting 33

Atomic variables Java s implementation of semaphore-like objects in java.util.concurrent.atomic // shared variable, initialized to 0 AtomicInteger s = new AtomicInteger(0); // this is equivalent to an atomic s++ s.incrementandget(); // this is equivalent to an atomic s-- s.decrementandget(); 34

Concurrent collections Java provides several implementations of data structures that are thread-safe LinkedBlockingQueue ArrayBlockingQueue ConcurrentHashMap A thread-safe list implementation is also provided among the standard collections in java.utils.collections public static List synchronizedlist(list list) 35

Fork/join parallelism Fork/join is a straightforward model of parallel computation suitable to implement divide and conquer algorithms exploiting parallelism. X = instance to be solved; if ( X is small ) { solve X; else { split X into X1 and X2; spawn a new thread T and launch it on X1; // fork recursively solve X2; wait until T is done; combine the solutions for X1 and X2 into a solution for X; // join T T 36

Fork/join parallelism Java 7 introduced a library for fork/join parallelism (in java.util.concurrent). ForkJoinPool is a specialized executor service, which handles tasks that can fork and join. Its main purpose is making sure that no thread is idle ( work stealing schedule). RecursiveAction and RecursiveTask<T> are the two main abstract classes to define tasks that can fork and join. RecursiveAction for tasks that don t return any value. RecursiveTask<T> for tasks that return values of type T. Inherit and override T compute() to implement specific tasks (T is void for RecursiveAction). 37

Fork/join parallelism Main methods of RecursiveAction and RecursiveTask<T> (T is void for RecursiveAction ): fork(): schedule task for asynchronous parallel execution. T join(): await for task termination and return result. T invoke(): arrange parallel execution, await for termination, and return result. invokeall(collection<t> tasks): spawn multiple tasks and wait for all of them to terminate (works on tasks in the collection passed as argument). 38

Fork/join parallelism: example Divide and conquer algorithm to sum the content of an array: 1. If the array is small, iterate over its values. 2. Otherwise, split it in two, sum the two halves in parallel, and then combine the two partial sums. public class ParSum extends RecursiveTask<Integer> { int [] values; // values to be summed int low, high; // range to be summed public ParSum (int [] values, int low, int high) { this.values = values; this.low = low; this.high = high; 39

Fork/join parallelism: example (cont d) public class ParSum extends RecursiveTask<Integer> { int [] values; // values to be summed int low, high; // range to be summed // is the range small? protected boolean issmall() { return (high - low + 1 < 4); // compute sum directly protected int computedirectly() { int sum = 0; for (int i = low, i <= high; i++) sum += values[i]; return sum; 40

Fork/join parallelism: example (cont d) @Override protected Integer compute() { if ( issmall() ) { // directly compute small instances return computedirectly(); else { // split into two halves // note: what s wrong with (low+high)/2? int mid = low + (high - low + 1)/2; ParSum t1 = new ParSum(values, low, mid); t1.fork(); // fork a thread on lower half low = mid + 1; // current thread on upper half // overall result: upper sum + lower sum return compute() + t1.join(); 41

Fork/join parallelism: example (cont d) How to start the parallel computation and get the result: int [] data = ; // get data, somehow ParSum sum = new ParSum(data, 0, data.length-1); ForkJoinPool pool = new ForkJoinPool(); int total = pool.invoke(sum); System.out.println ( The sum is + total); 42