Parallel Programming Practice

Similar documents
Parallel Programming Practice

[module lab 1.2] STRUCTURING PROGRAMS IN TASKS

Concurrency in Object Oriented Programs 4. Object-Oriented Software Development COMP4001 CSE UNSW Sydney Lecturer: John Potter

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

Final Concurrency. Oleg October 27, 2014

SUMMARY FUTURES CALLABLES CONCURRENT PROGRAMMING THREAD S ADVANCED CONCEPTS

[module lab 1.3] CANCELLATION AND SHUTDOWN

Concurrency Utilities: JSR-166

Charlie Garrod Bogdan Vasilescu

Basics of. Multithreading in Java

CS 159: Parallel Processing

Introduction to Java Threads

Lecture 03: Thread API (continue)

Only one thread can own a specific monitor

Outline. Command Pattern. Examples. Goal & Applications Motivation (undo / redo) Structure & participants Sequence diagram

COE518 Lecture Notes Week 7 (Oct 17, 2011)

The Java ExecutorService (Part 1)

CONCURRENT API AND PARALLEL PROGRAMMING

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

CST242 Concurrency Page 1

Threads Chate Patanothai

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

UNIT V CONCURRENT PROGRAMMING

7. MULTITHREDED PROGRAMMING

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

Principles of Software Construction: Objects, Design and Concurrency. The Perils of Concurrency, part 4 Can't live with it. Can't live without it.

Principles of Software Construction: Objects, Design, and Concurrency. The Perils of Concurrency, Part 3 Can't live with it. Can't live without it.

Concurrent Programming using Threads

CMSC 330: Organization of Programming Languages

Charlie Garrod Michael Hilton

COP 4610: Introduction to Operating Systems (Spring 2015) Chapter 4: Threads. Zhi Wang Florida State University

Simpler, Faster, Better: Concurrency Utilities in JDK Software Version 5.0

Advanced Programming Methods. Lecture 7 - Concurrency in Java (2)

Principles of Software Construction: Objects, Design, and Concurrency. Concurrency: More Design Tradeoffs School of Computer Science

Principles of Software Construction: Objects, Design and Concurrency. The Perils of Concurrency, Part 3 Can't live with it. Cant live without it.

Java Threads. Introduction to Java Threads

CONCURRENCY IN JAVA Course Parallel Computing

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

Advanced Programming Methods. Lecture 6 - Concurrency in Java (1)

Advanced Programming Concurrency

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

Java Threads. COMP 585 Noteset #2 1

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

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

Thread Pools SE 441. Prof. Bullinger

Contents. 6-1 Copyright (c) N. Afshartous

Object Oriented Programming (II-Year CSE II-Sem-R09)

Threads Questions Important Questions

The Java FutureTask. Douglas C. Schmidt

Contents. G53SRP: Java Threads. Definition. Why we need it. A Simple Embedded System. Why we need it. Java Threads 24/09/2009 G53SRP 1 ADC

Chapter 32 Multithreading and Parallel Programming

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

Object Oriented Programming. Week 10 Part 1 Threads

CMSC 433 Programming Language Technologies and Paradigms. Concurrency

Principles of Software Construction: Concurrency, Part 2

Software Practice 1 - Multithreading

-Aditya Bhave CSCI-5448 Graduate Presentation

Programming Language Concepts: Lecture 11

Advanced Concepts of Programming

SUMMARY INTRODUCTION CONCURRENT PROGRAMMING THREAD S BASICS. Introduction Thread basics. Thread states. Sequence diagrams

Techniques of Java Programming: Concurrent Programming in Java

COMP 322: Fundamentals of Parallel Programming

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

CS455: Introduction to Distributed Systems [Spring 2019] Dept. Of Computer Science, Colorado State University

Multi-threading in Java. Jeff HUANG

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling

Unit III Rupali Sherekar 2017

CS11 Java. Fall Lecture 7

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

Unit 4. Thread class & Runnable Interface. Inter Thread Communication

Module - 4 Multi-Threaded Programming

COP 4610: Introduction to Operating Systems (Fall 2016) Chapter 4: Threads. Zhi Wang Florida State University

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

Multithreaded Programming

Programming in Java

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

Exercise Session Week 8

Reading from URL. Intent - open URL get an input stream on the connection, and read from the input stream.

Animation Part 2: MoveableShape interface & Multithreading

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

Buch gefunden? Im Blackboard eingetragen? Abgabe 1. Übung: , 23:59

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

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

EDAF65 Processes and Threads

Threads and Java Memory Model

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

Chair of Software Engineering. Java and C# in Depth. Prof. Dr. Bertrand Meyer. Exercise Session 8. Nadia Polikarpova

Exercise Session Week 8

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

EDA095 Processes and Threads

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

By: Abhishek Khare (SVIM - INDORE M.P)

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

Unit - IV Multi-Threading

CS 556 Distributed Systems

Handouts. 1 Handout for today! Recap. Homework #2 feedback. Last Time. What did you think? HW3a: ThreadBank. Today. Small assignment.

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

Monitors CSCI 201 Principles of Software Development

Network Programming COSC 1176/1179. Lecture 6 Concurrent Programming (2)

Transcription:

Parallel Programming Practice Threads and Tasks Susanne Cech Previtali Thomas Gross Last update: 2009-10-29, 09:12

Thread objects java.lang.thread Each thread is associated with an instance of the class Thread Two strategies for using Thread objects To directly control thread creation and management Instantiate Thread each time for an asynchronous task Abstract thread management from the rest of the application Pass the tasks to an Executor 2

Today Low-level: basic building blocks Thread API Wait and notify mechanism High-level: concurrency API Executor framework 3

Thread API 4

How to create a thread 1. Declare a class that implements the Runnable interface public class HelloRunnable implements Runnable { public void run() { System.out.println("Hello from a thread!"); public static void main(string args[]) { Thread t = new Thread(new HelloRunnable()); t.start(); preferable way! Separates Runnable task from the Thread object that executes the task Applicable to high-level thread management APIs (Executor) 5

How to create a thread 2. Declare a class to be a subclass of Thread public class HelloThread extends Thread { public void run() { System.out.println("Hello from a thread!"); public static void main(string args[]) { Thread t = new HelloThread(); t.start(); 6

java.lang.thread: Properties Property Getter Setter Description long id Identifier int priority Priority String name Name boolean isdaemon User or daemon thread 7

java.lang.thread: Queries Instance methods boolean isalive() boolean isinterrupted() Description Is the current thread alive? Has the current thread been interrupted? Class methods Thread currentthread() boolean interrupted() Description Reference to the currently executing thread Has the current thread been interrupted? 8

java.lang.thread: Commands Instance methods void run() void start() void interrupt() void join([long]) Description Default: returns override Start a Thread instance and execute its run() method Interrupt the current thread Block until the other thread exits [for at most the given milliseconds] Class methods void sleep(long) Description Stop temporarily (for the given milliseconds) the execution of the current thread 9

JMM: Happens-before rules for threads Thread start rule T1.start() happens-before every action in T1 Thread termination rule Any action in T1 happens-before any action in T2 that detects that T1 has terminated Detection in T2: T1.join() returns or T1.isAlive() == false Interruption rule In T1: T2.interrupt() happens-before interrupt detection (by any thread including T2) Detection: throw InterruptException, invoke T2.isInterrupted(), Thread.interrupted() 10

Thread control example: Main public class SimpleThreads { public static void main(string args[]) throws InterruptedException { long patience = 1000 * 60 * 60; // 1 hour delay long starttime = System.currentTimeMillis(); Thread t = new Thread(new MessageLoop()).start(); while (t.isalive()) { t.join(1000); // wait for t to finish (max. 1 second) if (((System.currentTimeMillis() - starttime) > patience) && t.isalive()) { t.interrupt(); // tired of waiting -> interrupt t t.join(); // wait indefinitely for t to finish See example at http://java.sun.com/docs/books/tutorial/essential/concurrency/simple.html 11

Thread control example: MessageLoop public class MessageLoop implements Runnable { public void run() { String importantinfo[] = { "A", "B", "C", "D" ; try { for (int i = 0; i < importantinfo.length; i++) { Thread.sleep(4000); // pause for 4 seconds printmessage(importantinfo[i]); catch (InterruptedException e) { printmessage("i wasn't done!"); 12

Wait and notify 13

Wait sets and notification Each Object has an associated lock and wait set Wait set Set of threads Holds threads blocked by Object.wait() until notifications/wait done Used by wait(), notify(), notifyall() and thread scheduling Wait sets interact with locks t.wait(), t.notify(), t.notifyall() must be called only when synchronization lock is hold on t Otherwise IllegalMonitorStateException is thrown 14

Object.wait() and Object.wait(long) If current thread T has been interrupted by another thread return else T is blocked T is placed in wait set of obj T releases any locks for obj (keeps other locks) Lock status is restored upon later resumption synchronized (obj) { while (<condition does not hold>) obj.wait(); // Perform action appropriate to condition Object.wait(long) waits for a maximum time given 15

Object.notify() and Object.notifyAll() A thread T is arbitrarily chosen from wait set of obj No guarantees which thread T re-obtains lock on obj T blocks until notify() releases the lock T may block if some other thread obtains lock first T resumes after wait() wait() returns notifyall() Similar as notify() but for all threads in wait set of obj 16

Example with useless class To illustrate the underlying mechanisms class X { synchronized void w() throws InterruptedException { before(); wait(); after(); synchronized void n() { notifyall(); void before() { void after() { Attention! Broken program: liveness failure missed signal 17

T1: x.w() acquire lock before(); wait: release lock enter wait set exit wait set wait for lock acquire lock after(); release lock T2: x.w() acquire lock before(); wait: release lock enter wait set exit wait set wait for lock acquire lock after(); release lock T3: x.n() wait for lock acquire lock notifyall(); release lock

Remarks Place checks for condition variables in while loops Thread only knows that is has been waken up, must re-check Methods with guarded waits are not completely atomic On wait() lock is released other thread can be scheduled Objects must be in consistent state before calling wait() 19

Typical usage slipped condition if two synchronized blocks public class PatientWaiter { @GuardedBy( this ) private volatile boolean flag = false; public synchronized void waittillchange() { while (!flag) { try { this.wait(); catch (InterruptedException e) { // whatever needs to be done after condition is true public synchronized void change() { flag = true; this.notifyall(); 20

Executor framework 21

Threaded web server public class ThreadPerTaskWebServer { public static void main(string[] args) throws IOException { ServerSocket socket = new ServerSocket(80); while (true) { final Socket connection = socket.accept(); Runnable task = new Runnable() { public void run() { handlerequest(connection); ; new Thread(task).start(); private static void handlerequest(socket connection) {... See example at http://www.javaconcurrencyinpractice.com/listings/threadpertaskwebserver.java 22

Problems of the threaded solution Discussion Up to a certain point: more threads improve throughput Beyond that: slow down, crash Poor resource management Thread lifecycle overhead Thread creation and teardown Resource consumption More runnable threads than processors may hurt performance Memory, garbage collection Stability Number of threads limited OutOfMemoryError 23

Tasks versus threads Task Logical unit of work Thread Mechanism by which tasks can run asynchronously Web server example Each task is executed in its thread Poor resource management 24

Need: High-level abstraction for task execution Low-level constructs wait()/notify() High-level concurrency API Prefer executors and tasks to threads Prefer concurrency utilities to wait()/notify() 25

Web server using Executor public class TaskExecutionWebServer { private static final Executor exec =...; // see later public static void main(string[] args) throws IOException { ServerSocket socket = new ServerSocket(80); while (true) { final Socket connection = socket.accept(); Runnable task = new Runnable() { public void run() { handlerequest(connection); ; exec.execute(task); 29

Different Executor implementations Behavior like ThreadPerTaskWebServer public class ThreadPerTaskExecutor implements Executor { public void execute(runnable r) { new Thread(r).start(); Behavior like a single threaded web server public class WithinThreadExecutor implements Executor { public void execute(runnable r) { r.run(); 30

java.util.concurrent.executor public interface Executor { // Execute the given command at some time in the future void execute(runnable command); 31

Executor implementations Tasks may execute in a newly created thread an existing task-execution thread or the thread calling execute() Tasks may execute sequentially or concurrently 32

Execution policies Executor decouples submission from execution Resource management tool What resources are available? Which QOS requirements? Policies decide In what threads will tasks execute In what order? -- FIFO, LIFO, priority queue? How many concurrent tasks? How many tasks may be queued pending execution? If system overloaded: choose victim task? notify application? Actions before/after executing a task? 33

Thread pool task Advantages Amortize thread creation/teardown costs over multiple requests No latency of thread creation better responsiveness Tuning parameter: size work queue thread pool worker thread - request task - execute task - wait for next task 34

Factory methods to create thread pools public class Executors { // maintain n threads, unbounded queue public static ExecutorService newfixedthreadpool(int n) // create threads as needed (reused), unbounded queue public static ExecutorService newcachedthreadpool() // create one thread, unbounded queue public static ExecutorService newsinglethreadexecutor() // delayed and periodic task execution public static ExecutorService newscheduledthreadpool(int size) //... more methods... consider also overloaded variants 35

Web server using thread pool public class TaskExecutionWebServer { private static final int NTHREADS = 100; private static final Executor exec = Executors.newFixedThreadPool(NTHREADS); public static void main(string[] args) throws IOException { ServerSocket socket = new ServerSocket(80); while (true) { final Socket connection = socket.accept(); Runnable task = new Runnable() { ; public void run() { handlerequest(connection); exec.execute(task); factory method for creating a thread pool 36

Executor lifecycle Executor processes task asynchronously State of tasks may not be obvious Executor provides service to applications: must be able to Shutdown Report status of tasks Also: executor implementation must shut down JVM can exit only after all threads have terminated 37

States of the ExecutorService tasks submitted and executed if task submitted: rejected execution handler possible to wait for termination running shutting down terminated shutdown initiated - graceful - abrupt all tasks have been completed 38

Shutdown Lifecycle of a task can always be cancelled may be cancelled (if open for interruption) cancel: no effect created submitted started completed abrupt shutdown cancel cancel graceful shutdown submitted and started tasks can complete 39

java.util.concurrent.executorservice public interface ExecutorService extends Executor { // graceful shutdown void shutdown(); // abrupt shutdown // -> return list of tasks awaiting execution List<Runnable> shutdownnow(); // query about state change boolean isshutdown(); //... more methods... discussed later 40

java.util.concurrent.executorservice public interface ExecutorService extends Executor { // block until one event happens // (1) all tasks have completed // (2) the timeout occurs // (3) the current thread is interrupted boolean awaittermination(long timeout, TimeUnit unit) throws InterruptedExecution; // Have all tasks been completed? following shut-down boolean isterminated(); //... more methods... discussed later 41

public class LifecycleWebServer { private final ExecutorService exec = Executors.newCachedThreadPool(); public void start() throws IOException { ServerSocket socket = new ServerSocket(80); while (!exec.isshutdown()) { try { final Socket conn = socket.accept(); exec.execute(new Runnable() { public void run() { handlerequest(conn); ); catch (RejectedExecutionException e) { if (!exec.isshutdown()) log("task submission rejected", e); public void stop() { exec.shutdown(); See complete code at http://www.javaconcurrencyinpractice.com/listings/lifecyclewebserver.java 42

Executor revisited public interface Executor { // Execute the given command at some time in the future void execute(runnable command); Runnable as basic task representation Cannot return a value Cannot throw checked exceptions Other task abstractions necessary Callable: task Future: result 43

Task abstraction Callable public interface Callable<V> { // Task that returns a result and may throw an exception V call() throws Exception; See Executors for utility factory methods Example: wrap a Runnable in a Callable 44

Lifecycle abstraction with Future can always be cancelled may be cancelled (if open for interruption) cancel: no effect created submitted started completed cancel(boolean) true iscancelled() true isdone() get() blocks get() result 45

Future public interface Future<V> { V get() throws InterruptedException, ExecutionException; V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException; boolean isdone(); boolean cancel(); boolean iscancelled(); Create a future Interface ExecutorService: Future<V> submit([callable Runnable]) Class FutureTask<V>: base implementation of Future<V> 46