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

Similar documents
Parallel Programming Practice

[module lab 1.2] STRUCTURING PROGRAMS IN TASKS

Parallel Programming Practice

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

The Java ExecutorService (Part 1)

Charlie Garrod Bogdan Vasilescu

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

CONCURRENT API AND PARALLEL PROGRAMMING

CS 159: Parallel Processing

Lecture 03: Thread API (continue)

Thread Pools SE 441. Prof. Bullinger

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

Threads Chate Patanothai

UNIT V CONCURRENT PROGRAMMING

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

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

CST242 Concurrency Page 1

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

Charlie Garrod Michael Hilton

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

Java Threads. Written by John Bell for CS 342, Spring 2018

7. MULTITHREDED PROGRAMMING

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

CONCURRENCY IN JAVA Course Parallel Computing

Multi-threading in Java. Jeff HUANG

Chapter 32 Multithreading and Parallel Programming

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

Java Threads. COMP 585 Noteset #2 1

Only one thread can own a specific monitor

Threads Questions Important Questions

JSR 236 Status Jan 28, 2013

Java Threads. Introduction to Java Threads

Multithreaded Programming

Basics of. Multithreading in Java

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

CS 351 Design of Large Programs Threads and Concurrency

CMSC 433 Programming Language Technologies and Paradigms. Concurrency

Multithreading and Interactive Programs

Animation Part 2: MoveableShape interface & Multithreading

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

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

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling

Unit - IV Multi-Threading

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

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

COMP 322: Fundamentals of Parallel Programming

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

CS11 Java. Fall Lecture 7

Object Oriented Programming. Week 10 Part 1 Threads

CMSC 330: Organization of Programming Languages

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

-Aditya Bhave CSCI-5448 Graduate Presentation

Multithreading using Java. Dr. Ferdin Joe John Joseph

Concurrent Programming. Copyright 2017 by Robert M. Dondero, Ph.D. Princeton University

Advanced Concepts of Programming

Introduction to Java Threads

Concurrent Programming using Threads

CS193k, Stanford Handout #8. Threads 3

Module - 4 Multi-Threaded Programming

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

G52CON: Concepts of Concurrency

What is a thread anyway?

CS 556 Distributed Systems

Multithreading. Introduction and Basics. Classical Model. New Model. Summary. Multithreading Basics Problems with Multithreading

CSCI 201L Written Exam #1 Fall % of course grade

Programming in Java

Chapter 8 Applying Thread Pools. Magnus Andersson

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

Principles of Software Construction: Concurrency, Part 2

[module lab 2.2] GUI FRAMEWORKS & CONCURRENCY

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

Overview of Java Threads (Part 2)

Info 408 Distributed Applications Programming Exercise sheet nb. 4

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

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

The Java FutureTask. Douglas C. Schmidt

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

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

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

Advanced Programming Concurrency

Threads & Timers. CSE260, Computer Science B: Honors Stony Brook University

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

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

COURSE 11 PROGRAMMING III OOP. JAVA LANGUAGE

Chapter 4: Threads. Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads

Unit III Rupali Sherekar 2017

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

CSCD 330 Network Programming

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

Concurrent Computing CSCI 201 Principles of Software Development

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

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

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

Paradigmas de Computação Paralela

Threads and Parallelism in Java

Transcription:

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

Outline Thread Control Tasks and Task Control Finding and Exploiting Parallelism 2

Thread States A thread can be in one of the following states: NEW A thread that has not yet started is in this state. RUNNABLE A thread executing in the Java virtual machine is in this state. BLOCKED A thread that is blocked waiting for a monitor lock is in this state. WAITING A thread that is waiting indefinitely for another thread to perform a particular action is in this state. TIMED_WAITING A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state. TERMINATED A thread that has exited is in this state. 3

Thread Control Manage the schedules of multiple threads Hand information from one thread to another Signal one thread that another thread has finished doing something Methods for inter-thread control and synchronisation are: getpriority, setpriority, join, interrupt yield, sleep (for current thread) wait, notify, notifyall (per object) 4

Thread Priorities Runnable threads are scheduled for execution by JVM Priority mechanism is platform dependent Depends on VM (and OS) Programmers can get and set priority for individual objects getpriority() setpriority() The scheduler uses priorities to allocate CPU time ADVICE: Avoid use of priorities May cause liveness problems e.g. via priority inversion Introduces platform dependence Too weak to protect critical sections! Usually sufficient to rely on default priority 5

Thread.sleep Put target thread to sleep for a specified time VM guarantees a minimum sleep time Does not give up locks Any held continue to be held while sleeping Should usually release locks before sleep sleep is useful for providing timeouts controlling the speed of applications e.g. specifying framerates for multimedia presentations sleep responds to an interrupt on the thread by exiting, clearing interrupt status and throwing an InterruptedException 6

Sleep Example public class Sleep { public static void main(string args[]) throws InterruptedException { final int N = 10; for (int i = N; i > 0; --i) { // Pause for 10, 9,... 1 seconds Thread.sleep(1000 * i); // Awake indicator System.out.print("."); System.out.println(); 7

Thread.yield() Causes current thread to pause Gives up the CPU Allows other threads to execute But no guarantee about which order others may run Current thread may just start up again Similar to Thread.sleep(0) Except for throwing of InterruptedExceptions 8

object.wait() A method of the Object class Cause current thread to wait Current thread must hold the target object s lock when called release the lock and place thread on wait queue for the lock Resumption occurs if one of following occurs Waiting thread is interrupted resumes, clears interrupt status, and throws InterruptedException Another thread invokes the notify()/notifyall() methods for the target object 9

object.notify() or notifyall() Which of these should be called? Unless there is only one condition for an object that may be waited on call notifyall() and re-check the condition for that thread while(!condition) wait(); with try catch InterruptedException handling Only use notify() when you are sure that any thread that might be waiting can make use of the condition causing the notification 10

thread.interrupt() Send an indication to a thread that it should stop and do something else useful for external signals Programmers determine exactly how a thread responds to an interrupt Interrupt handling: If thread is currently blocked by wait, sleep or join interrupt status is cleared, InterruptedException is thrown, and thread is resumed otherwise interrupt status of thread is set 11

Programming with Interrupts Handle InterruptedException in blocking methods for (int i = 0; i < importantinfo.length; i++) { //Pause for 4 seconds try { Thread.sleep(4000); catch (InterruptedException e) { //We've been interrupted: no more messages. return; System.out.println(importantInfo[i]); 12

Interrupt Status Thread.interrupted() tests whether the current thread has been interrupted. The interrupted status of the thread is cleared by this method. So normally programmer must code some interrupt handling whenever this returns true thread.isinterrupted() tests whether this thread has been interrupted. The interrupted status of the thread is unaffected by this method. 13

Thread.interrupted() Programmers can explicitly check the interrupt status of thread when no call throws InterruptedException useful for long running methods for (int i = 0; i < inputs.length; i++) { heavycrunch(inputs[i]); if (Thread.interrupted()) { //We've been interrupted: no more crunching. return; 14

thread.join() Force the current thread to wait until another thread terminates used to define synchronization points This thread may continue after another thread has done its business It can provide an alternative to use of locks when different threads are strongly interdependent join responds to an interrupt on the thread by exiting, clearing interrupt status and throwing an InterruptedException 15

Example of Thread Join Runnable r1 = ; Runnable r2 = ; Thread t1 = new Thread (r1); Thread t2 = new Thread (r2); t1.start(); t2.start(); // other computation. Do not use r1 or r2 t1.join(); // block until t1 is done t2.join(); // block until t2 is done //Now use r1 and r2 holding results of t1 and t2 computations 16

Terminating Threads Normal termination run() terminates Abnormal termination Exception that propagates beyond run() method causes thread termination can define uncaught exception handler for individual threads, or accept default System termination Runtime.exit() 17

Comments on Thread Control In general, it is advisable not to directly control thread execution Better approach is to rely on a higher level pattern of thread use The Java Concurrency Framework provides a better option Use executors to separate submission of runnable tasks from execution policy 18

Outline Thread Control Tasks and Task Control Finding and Exploiting Parallelism 19

Organise Task Execution Each task represents a small fraction of the application's processing capacity We should identify sensible task boundaries ideally, tasks should be as independent as possible independence facilitates concurrency Most server applications offer a natural choice of task boundary individual requests as task boundaries usually requests offer both independence and appropriate task sizing 20

Executing Tasks Sequentially The simplest way to organise tasks is to execute tasks sequentially in a single thread class SingleThreadWebServer { public static void main(string[] args) throws IOException { ServerSocket socket = new ServerSocket(80); while (true) { Socket connection = socket.accept(); handlerequest(connection); 21

Creating Threads for Tasks A more responsive approach is to create a new thread for servicing each request class ThreadPerTaskWebServer { public static void main(string[] args) throws IOException { ServerSocket socket = new ServerSocket(80); while (true) { Socket connection = socket.accept(); Runnable task = new Runnable() { public void run() { handlerequest(connection); ; new Thread(task).start(); 22

Consequences of Executing Tasks in Threads Task processing is offloaded from the main thread this enables the main loop to resume waiting for the next incoming connection more quickly Tasks can be processed in parallel this enables multiple requests to be serviced simultaneously Task-handling code must be thread-safe 23

Disadvantages of Unbounded Thread Creation Thread life cycle overhead thread creation and teardown are not free Resource consumption active threads consume system resources, especially memory Stability there is a limit on how many threads can be created We need to place some bound on how many threads the application creates 24

The Executor Framework We have seen two policies for executing tasks execute tasks sequentially in a single thread execute each task in its own thread Both have serious limitations either poor responsiveness and throughput or poor resource management The third option for thread management use bounded queues to prevent an application from overloading The primary abstraction for task execution in the Java libraries is not Thread, but Executor Since Java 5 25

The Executor Interface java.util.concurrent provides a flexible thread pool implementation as part of the Executor framework public interface Executor { void execute(runnable task); Decouple task submission from task execution tasks are represented by Runnable objects Support a wide variety of task execution policies 26

Web Server Using Executor 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) { Socket connection = socket.accept(); Runnable task = new Runnable() { public void run() { handlerequest(connection); ; exec.execute(task); 27

Implement the Executor Interface Executor interface can be implemented to support different task execution policies public class ThreadPerTaskExecutor implements Executor { public void execute(runnable r) { new Thread(r).start(); public class WithinThreadExecutor implements Executor { public void execute(runnable r) { r.run(); 28

Execution Policies An execution policy specifies what, where, when, and how of task execution in what thread will tasks be executed? in what order should tasks be executed? FIFO, LIFO, priority order how many tasks may execute concurrently? how many tasks may be queued pending execution? if a task has to be rejected because the system is overloaded, which task should be selected as the victim, and how should the application be notified? what actions should be taken before or after executing a task? 29

Execution Policies Execution policies must deal with resource management issues the optimal policy depends on the available computing resources and your quality-of-service requirements Decoupling task submission from execution has benefits we can easily specify the execution policy and subsequently change it without great difficulty it becomes practical to select an execution policy at deployment time matching to available system resources e.g. number of CPUs 30

Threads Pools A thread pool manages a homogeneous pool of worker threads bound to a queue holding tasks waiting to be executed The lifecycle of a worker thread request the next task from the queue execute the task wait for another task 31

Creating Thread Pools java.util.concurrent.executors provides static factory methods for creating thread pools see Java API docs newfixedthreadpool return a thread pool with constant size newcachedthreadpool return a thread pool without bound size, but always try to reuse existing threads first newsinglethreadexecutor newscheduledthreadpool 32

Advantages of Thread Pools Reuse an existing thread instead of creating a new one reduce overhead of thread creation/teardown Improve responsiveness immediately execute a task without latency associated with thread creation Exploit multiple processors without exhausting resources enough threads to keep processors busy not too many threads to exhaust resources 33

ExecutorService Interface ExecutorService interface extends Executor, with methods for lifecycle management: public interface ExecutorService extends Executor { void shutdown(); List<Runnable> shutdownnow(); boolean isshutdown(); boolean isterminated(); boolean awaittermination(long timeout, TimeUnit unit) throws InterruptedException; //... additional convenience methods for task submission 34

Web Server with Shutdown class LifecycleWebServer { private final ExecutorService exec =...; public void start() throws IOException { ServerSocket socket = new ServerSocket(80); while (!exec.isshutdown()) { try { Socket conn = socket.accept(); exec.execute(new Runnable() { public void run() { handlerequest(conn); ); catch (RejectedExecutionException e) { if (!exec.isshutdown()) log("task submission rejected", e); 35

Web Server with Shutdown public void stop() { exec.shutdown(); void handlerequest(socket connection) { Request req = readrequest(connection); if (isshutdownrequest(req)) stop(); else dispatchrequest(req); 36

Outline Thread Control Tasks and Task Control Finding and Exploiting Parallelism 37

Case Study: Page Renderer The page-rendering portion of a browser application takes a page of HTML and renders it into an image buffer We will develop several versions that admit different degrees of concurrency Examples are from Java Concurrency in Practice The simplest approach is to process the HTML document sequentially as text markup is encountered, render it into the image buffer as image references are encountered, fetch the image over the network and draw it into the image 38

Sequential Page Renderer public class SingleThreadRenderer { void renderpage(charsequence source) { rendertext(source); List<ImageData> imagedata = new ArrayList<ImageData>(); for (ImageInfo imageinfo : scanforimageinfo(source)) imagedata.add(imageinfo.downloadimage()); for (ImageData data : imagedata) renderimage(data); 39

Runnable vs Callable Both Runnables and Callables are tasks that can be processed by an Executor Runnable cannot return a value or throw checked exceptions it can only have side effects Callable can return a value and throw an exception public interface Callable<V> { V call() throws Exception 40

Callable and Future The lifecycle of a task executed by an Executor has four phases: created, submitted, started, and completed once a task is completed, it stays in that state forever Future represents the lifecycle of a task provide methods to test whether the task has completed or been cancelled, retrieve its result, and cancel the task 41

The Future Interface public interface Future<V> { boolean cancel(boolean mayinterruptifrunning); boolean iscancelled(); boolean isdone(); V get() throws InterruptedException, ExecutionException, CancellationException; V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, CancellationException, TimeoutException; 42

ExecutorService Interface (ctd.) public interface ExecutorService extends Executor { // lifecycle management methods // additional convenience methods for task submission <T> Future<T> submit(callable<t> task); <T> Future<T> submit(runnable task, T result); Future<?> submit(runnable task); <T> List<Future<T>> invokeall(collection<? extends Callable<T>> tasks) <T> List<Future<T>> invokeall(collection<? extends Callable<T>> tasks) long timeout, TimeUnit unit) throws InterruptedException; <T> T invokeany(collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException; <T> T invokeany(collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException; 43

Page Renderer with Future Divide page render into two tasks to make it more concurrent one renders the text (largely CPU-bound) one downloads all the images (largely I/O-bound) Callable and Future can help us express the interaction between these cooperating tasks create a Callable to download all the images a future is returned to describe the Callable execution when we need the images, we call Future.get() to wait for the result 44

Page Renderer with Future public class FutureRenderer { private final ExecutorService executor =...; void renderpage(charsequence source) { final List<ImageInfo> imageinfos = scanforimageinfo(source); Callable<List<ImageData>> task = new Callable<List<ImageData>>() { public List<ImageData> call() { List<ImageData> result = new ArrayList<ImageData>(); for (ImageInfo imageinfo : imageinfos) result.add(imageinfo.downloadimage()); return result; ; 45

Page Renderer with Future Future<List<ImageData>> future = executor.submit(task); rendertext(source); try { List<ImageData> imagedata = future.get(); for (ImageData data : imagedata) renderimage(data); catch (InterruptedException e) { // We don't need the result, so cancel the task too future.cancel(true); catch (ExecutionException e) { throw launderthrowable(e.getcause()); 46

Render Page Elements as They Become Available public class Renderer { private final ExecutorService executor; Renderer(ExecutorService executor) { this.executor = executor; void renderpage(charsequence source) { final List<ImageInfo> info = scanforimageinfo(source); CompletionService<ImageData> completionservice = new ExecutorCompletionService<ImageData>(executor); for (final ImageInfo imageinfo : info) completionservice.submit(new Callable<ImageData>() { ); public ImageData call() { return imageinfo.downloadimage(); 47

Render Page Elements as They Become Available rendertext(source); try { for (int t = 0, n = info.size(); t < n; t++) { Future<ImageData> f = completionservice.take(); ImageData imagedata = f.get(); renderimage(imagedata); catch (InterruptedException e) { Thread.currentThread().interrupt(); catch (ExecutionException e) { throw launderthrowable(e.getcause()); 48