Threads & Tasks: Executor Framework

Similar documents
Task Execution. Chapter Executing tasks in threads

Java Concurrency Framework. Sidartha Gracias

CS11 Java. Fall Lecture 7

Creating a Simple, Multithreaded Chat System with Java

Dragan Juričić, PBZ May 2015

Java Memory Model: Content

Massachusetts Institute of Technology 6.005: Elements of Software Construction Fall 2011 Quiz 2 November 21, 2011 SOLUTIONS.

Socket-based Network Communication in J2SE and J2ME

OBJECT ORIENTED PROGRAMMING LANGUAGE

Chapter 8 Implementing FSP Models in Java

Network/Socket Programming in Java. Rajkumar Buyya

Design Patterns in C++

Outline of this lecture G52CON: Concepts of Concurrency

Building a Multi-Threaded Web Server

JAVA - MULTITHREADING

Division of Informatics, University of Edinburgh

Deadlock Victim. dimanche 6 mai 12

Mutual Exclusion using Monitors

Getting Started with the Internet Communications Engine

!"# $ %!&' # &! ())*!$

Monitors, Java, Threads and Processes

Multithreaded Programming

Tutorial: Getting Started

Performance issues in writing Android Apps

An Android-based Instant Message Application

EECE 276 Embedded Systems

Amazon Glacier. Developer Guide API Version

Transactionality and Fault Handling in WebSphere Process Server Web Service Invocations. version Feb 2011

Concurrent Programming

Manual. Programmer's Guide for Java API

Background Tasks and Blackboard Building Blocks TM. Two great tastes that taste great together

Built-in Concurrency Primitives in Java Programming Language. by Yourii Martiak and Mahir Atmis

Green Telnet. Making the Client/Server Model Green

SnapLogic Salesforce Snap Reference

Question1-part2 What undesirable consequences might there be in having too long a DNS cache entry lifetime?

Traditional Software Development. Model Requirements and JAVA Programs. Formal Verification & Validation. What is a state?

Scheduling. Yücel Saygın. These slides are based on your text book and on the slides prepared by Andrew S. Tanenbaum

Chapter 2: Processes, Threads, and Agents

Process Scheduling CS 241. February 24, Copyright University of Illinois CS 241 Staff

q for Gods Whitepaper Series (Edition 7) Common Design Principles for kdb+ Gateways

3.5. cmsg Developer s Guide. Data Acquisition Group JEFFERSON LAB. Version

Threads 1. When writing games you need to do more than one thing at once.

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Java SE 8 Programming

Advanced Network Programming Lab using Java. Angelos Stavrou

Multiprocessor Scheduling and Scheduling in Linux Kernel 2.6

Computer Networks. Chapter 5 Transport Protocols

Network Communication

Licensed for viewing only. Printing is prohibited. For hard copies, please purchase from

XII. Distributed Systems and Middleware. Laurea Triennale in Informatica Corso di Ingegneria del Software I A.A. 2006/2007 Andrea Polini

Have both hardware and software. Want to hide the details from the programmer (user).

Decomposition into Parts. Software Engineering, Lecture 4. Data and Function Cohesion. Allocation of Functions and Data. Component Interfaces

Transport layer protocols. Message destination: Socket +Port. Asynchronous vs. Synchronous. Operations of Request-Reply. Sockets

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

Chapter 6 Concurrent Programming

Lesson: All About Sockets

Logging in Java Applications

TUTORIAL FOR INITIALIZING BLUETOOTH COMMUNICATION BETWEEN ANDROID AND ARDUINO

CA Data Protection. Content Provider Development Guide. Release 15.0

Parallel Programming

Monitor Object. An Object Behavioral Pattern for Concurrent Programming. Douglas C. Schmidt

Listeners. Formats. Free Form. Formatted

Design Pattern for the Adaptive Scheduling of Real-Time Tasks with Multiple Versions in RTSJ

Lecture 6: Introduction to Monitors and Semaphores

! "# $%&'( ) * ).) "%&' 1* ( %&' ! "%&'2 (! ""$ 1! ""3($

Operatin g Systems: Internals and Design Principle s. Chapter 10 Multiprocessor and Real-Time Scheduling Seventh Edition By William Stallings

Web Performance, Inc. Testing Services Sample Performance Analysis

Java from a C perspective. Plan

Effective unit testing with JUnit

Lecture 7: Java RMI. CS178: Programming Parallel and Distributed Systems. February 14, 2001 Steven P. Reiss

Developing Task Model Applications

1. What are Data Structures? Introduction to Data Structures. 2. What will we Study? CITS2200 Data Structures and Algorithms

Active Object. An Object Behavioral Pattern for Concurrent Programming. R. Greg Lavender Douglas C. Schmidt

Microsoft HPC. V 1.0 José M. Cámara (checam@ubu.es)

Highly Available AMPS Client Programming

Chapter 13 Embedded Operating Systems

CPU SCHEDULING (CONT D) NESTED SCHEDULING FUNCTIONS

Java Coding Practices for Improved Application Performance

Technical Overview Emif Connector

To Java SE 8, and Beyond (Plan B)

ODROID Multithreading in Android

AVRO - SERIALIZATION

Running a Workflow on a PowerCenter Grid

13 File Output and Input

Lecture 8: Safety and Liveness Properties

public static void main(string[] args) { System.out.println("hello, world"); } }

Introduction. What is an Operating System?

3C03 Concurrency: Condition Synchronisation

Transcription:

Threads & Tasks: Executor Framework Introduction & Motivation WebServer Executor Framework Callable and Future 12 April 2012 1

Threads & Tasks Motivations for using threads Actor-based Goal: Create an autonomous, active object which can react on events Example: swing event handler thread Task-based Task = Logical unit of work Goal: Asynchronous execution of a task Example: mandelbrot slice task Task based concurrency Simplifies program organization Facilitates error recovery by providing natural transaction boundaries Promotes concurrency by providing a natural structure for parallelizing work 12 April 2012 2

Threads & Tasks Example: Webserver Server accepts incoming socket requests Handler executes requests Handler executes requests Handler executes requests Thread Tasks 12 April 2012 3

Introduction: Implementation of a WebServer1 public class WebServer1 { public static void main(string[] args) throws IOException { ServerSocket serversocket = new ServerSocket(80); while(true) { Socket s = serversocket.accept(); handlerequest(s); Single-Threaded Server: Tasks are executed within the server thread Poor performance, can only handle one request at a time While server is processing, new requests must wait Possible if request processing is very fast 12 April 2012 4

Introduction: Implementation of a WebServer2 public class WebServer2 { public static void main(string[] args) throws IOException { ServerSocket serversocket = new ServerSocket(80); while(true) { final Socket s = serversocket.accept(); Thread t = new Thread(){ public void run(){ handlerequest(s); ; t.start(); Explicitly creating threads for tasks Each task is executed in its own thread handlerequest must be thread-safe Excessive thread creation: Scheduling overhead / memory consumption 12 April 2012 5

Introduction: Implementation of a WebServer3 public class WebServer3 { public static void main(string[] args) throws IOException { final ServerSocket serversocket = new ServerSocket(80); for (int i = 0; i < 10; i++) { Thread t = new Thread() { public void run() { while (true) { try { handlerequest(serversocket.accept()); catch (IOException e) { /*... */ ; t.start(); 12 April 2012 6

Introduction: Implementation of a WebServer3 Disadvantages Maybe incorrect, ServerSocket.accept() is not documented as threadsafe No flexibility, i.e. exactly 10 threads No creation of new threads No deletion of unused threads All threads are pre-created, no lazy allocation No life-cycle management, i.e. "pool" cannot be stopped No error handling, if an exception is not catched, thread terminates silently No flexibility in the order of pending requests They are stored in the Server-Socket queue No full control over size of queue for pending requests backlog parameter only specifies maximum length of the queue 12 April 2012 7

Threads & Tasks: Executor Framework Introduction & Motivation Executor Framework Callable and Future 12 April 2012 8

Executor Framework Fills the gap between single-threaded and thread-per-task 12 April 2012 9

Executor Framework Participants Worker: One thread is used to execute many unrelated tasks These threads are called worker threads / background threads May be organized in a thread pool (if more than one thread is used) May provide a flexible thread management Channel: A buffer which holds pending requests May be bounded Implements the producerconsumer pattern 12 April 2012 10

Executor Framework Executor = Channel + Worker public interface Executor { void execute(runnable command); Decouples task submission from task execution Executes the given command at some time in the future The command may be executed in a new thread / in a pooled thread / in the calling thread Runnable = Task public interface Runnable { void run(); Limitation: Method run cannot return a result (results are placed in shared fields) Method run cannot declare a checked exception 12 April 2012 11

Introduction: Implementation of a WebServer4 public class WebServer4 { public static void main(string[] args) throws IOException { Executor exec = new ThreadPoolExecutor(10); ServerSocket serversocket = new ServerSocket(80); while(true){ final Socket s = serversocket.accept(); Runnable task = new Runnable(){ public void run(){ handlerequest(s); ; exec.execute(task); 12 April 2012 12

Executor: Implementation class ThreadPoolExecutor implements Executor { private final BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(); public void execute(runnable r) { queue.offer(r); public ThreadPoolExecutor(int nrthreads) { for (int i = 0; i < nrthreads; i++) { activate(); private void activate() { new Thread(new Runnable() { public void run() { try { while (true) { queue.take().run(); catch (InterruptedException e) { /* die */ ).start(); 12 April 2012 13

Executor: Simple implementations DirectExecutor: Synchronous execution (in calling thread) class DirectExecutor implements Executor { public void execute(runnable r) { r.run(); With this executor Webserver4 = Webserver1 Thread per task executor class ThreadPerTaskExecutor implements Executor { public void execute(runnable r) { new Thread(r).start(); With this executor Webserver4 = Webserver2 12 April 2012 14

Executor: Advanced Implementations Execution Policies Execution order of submitted tasks (FIFO, LIFO, Priority Queue) More control as with Java's setpriority Number of threads which execute concurrently Maximal size of queue with pending tasks Actions taken before / after task execution (startup/cleanup) Factory: java.util.concurrent.executors Provides several implementations for thread pools which implement different policies All factory methods return an executor which implements the ExecutorService interface 12 April 2012 15

Executor: Advanced Implementations Executors.newFixedThreadPool Threads are created up to a fixed number Threads which die due to an unexpected exception are replaced Executors.newCachedThreadPool Creates new threads as needed, reusing previously constructed threads if they are available Executors.newSingleThreadExecutor Uses single worker thread Worker thread is replaced if an unexpected exception occurs Executors.newScheduledThreadPool Creates a ScheduledExecutorService which supports Periodic tasks (scheduleatfixedrate / schedulewithfixeddelay) Delayed tasks (schedule) 12 April 2012 16

ThreadFactory Some factory methods on j.u.c.executors take a ThreadFactory public interface ThreadFactory { Thread newthread(runnable r); Enables applications to use special Thread subclasses priorities custom named threads daemon flag 12 April 2012 17

Executors: Design Considerations Identity Different tasks are executed with the same thread => thread-specific contextual control techniques are difficult Queuing Use of ThreadLocals Use of security contexts Runnable tasks that are waiting in queues do not run => if a currently running task blocks waiting for a condition produced by a task still waiting in a queue => system may freeze up Use enough worker threads Restrict dependencies Create custom queues that understand the dependencies => Fork-Join Framework 12 April 2012 18

ExecutorService: Executor Life-Cycle ExecutorService: provides life-cycle management methods interface ExecutorService extends Executor { void shutdown(); List<Runnable> shutdownnow(); boolean isshutdown(); boolean isterminated(); boolean awaittermination(long timeout, TimeUnit unit) throws InterruptedException; Supports shutdown methods shutdown: Graceful shutdown: finish pending tasks, do not accept new ones shutdownnow: Abrupt shutdown: running tasks are interrupted, returns list of tasks that were not started awaittermination: awaits until executor service is terminated running shutting down terminated 12 April 2012 19

Executors and JMM Memory consistency effects Actions in a thread prior to submitting a Runnable object to an Executor happen-before its execution begins (possibly in another thread) Actions in a task which is executed by a SingleThreadExecutor happen-before actions executed in subsequent tasks (even if the subsequent task is executed by another thread due to an exception) 12 April 2012 20

Threads & Tasks: Executor Framework Introduction & Motivation Executor Framework Callable and Future 12 April 2012 21

Result-bearing tasks: Callable & Future Callable: Task with a result / exception interface Callable<V> { V call() throws Exception; Future: represents a future result of a task 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; 12 April 2012 22

Callable & Future Submitting tasks interface ExecutorService { //... Lifecycle methods Future<?> submit(runnable task); <T> Future<T> submit(callable<t> task); <T> Future<T> Submit(Runnable task, T result); <T> List<Future<T>> invokeall( Collection<? extends Callable<T>> tasks) throws InterruptedException; <T> T invokeany( Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException; 12 April 2012 23

Callable & Future States of a Task created submitted started completed get() If completed: returns immediately (returns result or throws ExecutionException) If not completed: method call blocks If terminates regularly If terminates with an exception If cancelled If thread calling get was interrupted => result => ExecutionException => CancellationException => InterruptedException 12 April 2012 24

CompletionService CompletionService = Executor + BlockingQueue Decouples production of new tasks from the consumption of the results of completed tasks Producers submit tasks for execution Consumers take completed tasks and process their results interface CompletionService { Future<V> poll(); // returns available future or null Future<V> poll(long timeout, TimeUnit unit) throws IE; Future<V> take() throws IE; // waits for a future Future<V> submit(callable<v> task); Future<V> submit(runnable task, V result); take retrieves and removes the next completed task, potentially waiting ExecutorCompletionService Uses a separate Executor to schedule the tasks 12 April 2012 25

CompletionService: Sample Solver Method solve takes a list of solvers (of type Callable<Result>) Result of first solver is returned void solve(executor e, Collection<Callable<Result>> solvers) throws InterruptedException { CompletionService<Result> cs = new ExecutorCompletionService<>(e); int n = solvers.size(); List<Future<Result>> futures = new ArrayList<Future<Result>>(n); Result result = null; try { for (Callable<Result> s : solvers) futures.add(cs.submit(s));... 12 April 2012 26

CompletionService: Sample for (int i = 0; i < n; ++i) { try { Result r = cs.take().get(); if (r!= null) { // solver may return null result = r; break; catch (ExecutionException ignore) { finally { // cancel all pending solvers for (Future<Result> f : futures) f.cancel(true); if (result!= null) use(result); 12 April 2012 27

Callable & Future and JMM Memory consistency effects Actions in a thread prior to the submission of a Runnable or Callable task to an ExecutorService happen-before any actions taken by that task Any actions taken by a Runnable or Callable task executed by an ExecutorService happen-before the result is retrieved via Future.get() 12 April 2012 28