Thread Pools SE 441. Prof. Bullinger

Similar documents
Chapter 8 Applying Thread Pools. Magnus Andersson

Threads Questions Important Questions

[module lab 1.2] STRUCTURING PROGRAMS IN TASKS

Parallel Programming Practice

Using the Executor Framework to Implement AEH in the RTSJ

Parallel Programming Practice

Parallel Task Executor in Java

[module lab 1.3] CANCELLATION AND SHUTDOWN

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

Lecture 03: Thread API (continue)

Final Concurrency. Oleg October 27, 2014

MULTI-THREADED QUERIES

White Paper. Major Performance Tuning Considerations for Weblogic Server

SEDA: An Architecture for Well-Conditioned, Scalable Internet Services

Effective Concurrent Java. Brian Goetz Sr. Staff Engineer, Sun Microsystems

THE PROCESS ABSTRACTION. CS124 Operating Systems Winter , Lecture 7

Efficient Android Threading

Use cases. Faces tagging in photo and video, enabling: sharing media editing automatic media mashuping entertaining Augmented reality Games

Vulkan: Scaling to Multiple Threads. Kevin sun Lead Developer Support Engineer, APAC PowerVR Graphics

CSCE 626 Experimental Evaluation.

Using Time Division Multiplexing to support Real-time Networking on Ethernet

CS555: Distributed Systems [Fall 2017] Dept. Of Computer Science, Colorado State University

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

SUMMARY FUTURES CALLABLES CONCURRENT PROGRAMMING THREAD S ADVANCED CONCEPTS

CST242 Concurrency Page 1

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

CS 153 Design of Operating Systems Winter 2016

Only one thread can own a specific monitor

Multimedia Systems 2011/2012

[08] IO SUBSYSTEM 1. 1

Announcements. Reading. Project #1 due in 1 week at 5:00 pm Scheduling Chapter 6 (6 th ed) or Chapter 5 (8 th ed) CMSC 412 S14 (lect 5)

Scheduling of processes

CMSC 433 Programming Language Technologies and Paradigms. Concurrency

Java Concurrency in practice Chapter 9 GUI Applications

Announcements. Program #1. Program #0. Reading. Is due at 9:00 AM on Thursday. Re-grade requests are due by Monday at 11:59:59 PM.

Chapter 9 Uniprocessor Scheduling

Chapter 4: Multi-Threaded Programming

Concurrency Utilities: JSR-166

CS 571 Operating Systems. Midterm Review. Angelos Stavrou, George Mason University

PROCESSES AND THREADS THREADING MODELS. CS124 Operating Systems Winter , Lecture 8

Subject Name: OPERATING SYSTEMS. Subject Code: 10EC65. Prepared By: Kala H S and Remya R. Department: ECE. Date:

A Predictable RTOS. Mantis Cheng Department of Computer Science University of Victoria

Uniprocessor Scheduling. Aim of Scheduling

Uniprocessor Scheduling. Aim of Scheduling. Types of Scheduling. Long-Term Scheduling. Chapter 9. Response time Throughput Processor efficiency

CS 450 Exam 2 Mon. 11/7/2016

CSE 120. Fall Lecture 8: Scheduling and Deadlock. Keith Marzullo

Devsummit Concurrency Hacks

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

Case Study: Parallelizing a Recursive Problem with Intel Threading Building Blocks

Thread Safety. Review. Today o Confinement o Threadsafe datatypes Required reading. Concurrency Wrapper Collections

Implementing Scheduling Algorithms. Real-Time and Embedded Systems (M) Lecture 9

CS533 Concepts of Operating Systems. Jonathan Walpole

Windows Interrupts

Page 1. Analogy: Problems: Operating Systems Lecture 7. Operating Systems Lecture 7

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

Yi Shi Fall 2017 Xi an Jiaotong University

Processes, Execution, and State. What is CPU Scheduling? Goals and Metrics. Rectified Scheduling Metrics. CPU Scheduling: Proposed Metrics 4/6/2016

Chapter 4: Multithreaded

Multiprocessor and Real-Time Scheduling. Chapter 10

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective. Part I: Operating system overview: Processes and threads

Today s class. Scheduling. Informationsteknologi. Tuesday, October 9, 2007 Computer Systems/Operating Systems - Class 14 1

DB2 Performance A Primer. Bill Arledge Principal Consultant CA Technologies Sept 14 th, 2011

A Sense of Time for JavaScript and Node.js

CONCURRENCY IN JAVA Course Parallel Computing

FCM 710: Architecture of Secure Operating Systems

JAVA CONCURRENCY FRAMEWORK. Kaushik Kanetkar

RxNetty vs Tomcat Performance Results

Distributed Scheduling for the Sombrero Single Address Space Distributed Operating System

Other Optimistic Mechanisms, Memory Management!

Multithreading and Interactive Programs

Chapter 32 Multithreading and Parallel Programming

ECE 477 Digital Systems Senior Design Project. Module 10 Embedded Software Development

Overview. Sporadic tasks. Recall. Aperiodic tasks. Real-time Systems D0003E 2/26/2009. Loosening D = T. Aperiodic tasks. Response-time analysis

Chapter 1: Distributed Information Systems

CSE120 Principles of Operating Systems. Prof Yuanyuan (YY) Zhou Scheduling

CSE473/Spring st Midterm Exam Tuesday, February 19, 2007 Professor Trent Jaeger

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

Lecture 2 Process Management

The Mercury project. Zoltan Somogyi

Last class: Today: Thread Background. Thread Systems

Chapter 8: Memory-Management Strategies

CPS 310 midterm exam #2, 4/10/2017

Gustavo Alonso, ETH Zürich. Web services: Concepts, Architectures and Applications - Chapter 1 2

Questions from last time

CHAPTER 3 - PROCESS CONCEPT

Process Concepts. CSC400 - Operating Systems. 3. Process Concepts. J. Sumey

Performance Best Practices Paper for IBM Tivoli Directory Integrator v6.1 and v6.1.1

Processes and Threads. Processes: Review

Vulkan Timeline Semaphores

Summary: Issues / Open Questions:

CHAPTER 8 - MEMORY MANAGEMENT STRATEGIES

SPIN Operating System

SmartSuspend. Achieve 100% Cluster Utilization. Technical Overview

What Developers must know about DB2 for z/os indexes

AUTOBEST: A United AUTOSAR-OS And ARINC 653 Kernel. Alexander Züpke, Marc Bommert, Daniel Lohmann

Static Analysis of Embedded C

The memory unit has three sets of reservation stations, not one:

CSE398: Network Systems Design

Chapter 6 Parallel Loops

Transcription:

Thread Pools SE 441 Prof. Bullinger

Thread Pools Thread Pool Limitations Sizing Thread Pools ThreadPoolExecutor Queuing Tasks Parallelizing Loops

Thread Pool Limitations Task Dependencies Different types of tasks require different execution policies Independent tasks are ideal Dependent tasks induce coupling Timing Results Side-effects Other tasks

Thread Confinement Thread confinement couples a task to its execution policy A single-threaded Executor does not require a thread-safe task Changing to a multi-threaded Executor introduces potential thread-safety issues Time-sensitive tasks may be impeded by an execution policy A long task in a single-threaded Executor may affect response time of other tasks

ThreadLocal ThreadLocal provides thread-specific state Executors reuse threads, rendering thread-specific state unusable Threads may also come and go, depending on the execution policy of the Executor Limit ThreadLocal to the lifecycle of a Task

Thread Pool Heuristics Thread Pools work best when tasks are homogenous and independent Don t mix tasks that depend on each other in the same Executor One task blocks, waiting for the results of the other The other cannot run due to limited resources A single-threaded thread pool A thread-pool with no available threads

Thread Pool Heuristics Thread Pools work best when tasks are homogenous and independent Don t mix long-running and short-running tasks Eventually, long-running tasks dominate the thread pool Responsiveness may suffer Suggest adding timeouts to blocking calls to ensure liveness Large or unbounded thread pools minimize risk Document the execution policy requirement for a task!

Thread Pools Thread Pool Limitations Sizing Thread Pools ThreadPoolExecutor Queuing Tasks Parallelizing Loops

Thread Pool Size The number of threads in a thread pool can be optimized for the target tasks Should be sized dynamically int cpucount = Runtime.getRuntime().availableProcessors(); Based on the type of task I/O bound vs. CPU bound Sizing is not an absolute number, but rather too big or too small

How Many Threads? A large thread pool: incurs significant overhead for management, locks-up memory while active or blocked A small thread pool: reduces parallelism impacts throughput

Sizing Thread Pools Consider the nature of the tasks How many resources (sockets, file handles, etc.) are required? What type of task (I/O vs CPU) How many processors are available? How much memory is available? Are other required resources scarce (i.e. JDBC connection) Consider different execution policies (thread pools) for different types of tasks

Thread Pool Sizing Ncpu = number of available CPUs Ucpu = target CPU utilization (0 <= Ucpu <= 1 W/C = ratio of wait time to compute time (blocked) Nthreads = Ncpu * Ucpu * (1 + W/C)

Thread Pools Thread Pool Limitations Sizing Thread Pools ThreadPoolExecutor Queuing Tasks Parallelizing Loops

Executor Default Executors provide many behaviors Executors may be customized after construction ThreadPoolExecutor

Customizing ThreadPoolExecutor Hooks are provides in ThreadPoolExecutor beforeexecute - called before a task is executed afterexecute - called after a task is run, or an exception terminated - called when the thread pool completes shutdown Ideal for logging and statistics gathering http://jcip.net/listings/timingthreadpool.java

Thread Pools Thread Pool Limitations Sizing Thread Pools ThreadPoolExecutor Queuing Tasks Parallelizing Loops

Queued Tasks Thread Pools maintain a queue of pending tasks Tasks may still be scheduled faster than they can execute Resulting in resource consumption and instability LinkedBlockingQueue is the default Executor queue mechanism BlockQueues provide one potential solution An optional parameter to a ThreadPoolExecutor

Bounded Blocking Queues Bounded blocking queues provide a better solution Submitted tasks will be queued to the limit of the queue size Clients will block when attempting to submit tasks past the upper bound Requires a saturation policy Queue size and thread pool size must be considered together

Synchronous Queue A synchronous queue can be used for thread pool task submission Requires large or unbound pool sizes Consumer must be faster than the producer Improves response time to new tasks May invoke the saturation policy sooner

PriorityQueue A priority queue can be used to create a scheduling policy in conjunction with an execution policy

Saturation Policy How does a client respond to a rejected task submission? Use a provided rejection policy handler AbortPolicy - throws RejectedExecutionException CallerRunsPolicy - the client runs the task DiscardPolicy - deletes the submitted task DiscardOldestPolicy - replaces the next task to run(!) Define a custom rejection handler setrejectedexecutionhandler

Block on Full Work Queue http://jcip.net/listings/boundedexecutor.java

Thread Factories Executors create threads through the default thread factory Returns a plain, non-daemon thread Custom ThreadFactories can be specified for Executors

Custom Thread Factory Reasons: Trap uncaught exceptions Provide names for threads Provide debug logging Apply security policies Include performance statistics Threads created Threads destroyed

Thread Pools Thread Pool Limitations Sizing Thread Pools ThreadPoolExecutor Queuing Tasks Parallelizing Loops

Sequential to Parallel

Sequential to Parallel Candidates have: Complex computations in the loop body Potentially blocking I/O in the loop body Independent processing No ordering dependencies Enough loop elements to offset the overhead The loop completes after tasks are queued Not after the processing!

Recursive Parallelism

Awaiting Parallel Results