Lecture 10 – Using Thread Pools Housekeeping:  exam return Monday  Programming assignment on Monday Purpose: practice use of the mechanisms we’ve discussed.

Slides:



Advertisements
Similar presentations
Concurrency (p2) synchronized (this) { doLecture(part2); } synchronized (this) { doLecture(part2); }
Advertisements

50.003: Elements of Software Construction Week 10 Thread Pool.
TELE202 Lecture 8 Congestion control 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »X.25 »Source: chapter 10 ¥This Lecture »Congestion control »Source:
CS 408 Computer Networks Congestion Control (from Chapter 05)
Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Designing a thread-safe class  Store all states in public static fields  Verifying thread safety is hard  Modifications to the program hard  Design.
Ceng Operating Systems Chapter 2.2 : Process Scheduling Process concept  Process scheduling Interprocess communication Deadlocks Threads.
1 Thread Pools Representation and Management of Data on the Internet.
CS444/CS544 Operating Systems Scheduling 1/31/2007 Prof. Searleman
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
Uniprocessor Scheduling Chapter 9. Aim of Scheduling The key to multiprogramming is scheduling Scheduling is done to meet the goals of –Response time.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 4: Threads Dr. Mohamed Hefeeda.
1 Thread Pools. 2 What’s A Thread Pool? A programming technique which we will use. A collection of threads that are created once (e.g. when server starts).
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
1 Uniprocessor Scheduling Chapter 9. 2 Aim of Scheduling Main Job: Assign processes to be executed by the processor(s) and processes to be loaded in main.
Real-Time Software Design Yonsei University 2 nd Semester, 2014 Sanghyun Park.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Lecture 5 Operating Systems.
Chapter 6 CPU SCHEDULING.
CGS 3763 Operating Systems Concepts Spring 2013 Dan C. Marinescu Office: HEC 304 Office hours: M-Wd 11: :30 AM.
Chapter 5 – CPU Scheduling (Pgs 183 – 218). CPU Scheduling  Goal: To get as much done as possible  How: By never letting the CPU sit "idle" and not.
CS 153 Design of Operating Systems Spring 2015 Lecture 11: Scheduling & Deadlock.
Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo.
Scheduler Activations: Effective Kernel Support for the User- Level Management of Parallelism. Thomas E. Anderson, Brian N. Bershad, Edward D. Lazowska,
Practical OOP using Java Basis Faqueer Tanvir Ahmed, 08 Jan 2012.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion.
1 (Worker Queues) cs What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.
Operating Systems CSE 411 Multi-processor Operating Systems Multi-processor Operating Systems Dec Lecture 30 Instructor: Bhuvan Urgaonkar.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Concurrency in Java Brad Vander Zanden. Processes and Threads Process: A self-contained execution environment Thread: Exists within a process and shares.
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 24 th, 2010 The University of Georgia.
Lecture 8 Page 1 CS 111 Online Other Important Synchronization Primitives Semaphores Mutexes Monitors.
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
Operating Systems CSE 411 CPU Management Sept Lecture 10 Instructor: Bhuvan Urgaonkar.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
Futures, Scheduling, and Work Distribution Speaker: Eliran Shmila Based on chapter 16 from the book “The art of multiprocessor programming” by Maurice.
CSE 153 Design of Operating Systems Winter 2015 Midterm Review.
CSC Multiprocessor Programming, Spring, 2012 Chapter 8 – Applying Thread Pools Dr. Dale E. Parson, week 10.
Multithreading The objectives of this chapter are: To understand the purpose of multithreading To describe Java's multithreading mechanism.
Operating System Concepts
Process Control Management Prepared by: Dhason Operating Systems.
Lecture 4 Page 1 CS 111 Online Modularity and Memory Clearly, programs must have access to memory We need abstractions that give them the required access.
Introduction to operating systems What is an operating system? An operating system is a program that, from a programmer’s perspective, adds a variety of.
Lecture 4 Page 1 CS 111 Summer 2013 Scheduling CS 111 Operating Systems Peter Reiher.
Embedded Real-Time Systems Processing interrupts Lecturer Department University.
Lecturer 3: Processes multithreaded Operating System Concepts Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess.
CPU Scheduling CS Introduction to Operating Systems.
Process Management Deadlocks.
Multithreading / Concurrency
Thread Pools (Worker Queues) cs
Thread Pools (Worker Queues) cs
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
CS240: Advanced Programming Concepts
Outline Other synchronization primitives
Other Important Synchronization Primitives
Modularity and Memory Clearly, programs must have access to memory
Uniprocessor Scheduling
Chapter 2.2 : Process Scheduling
Threads and Data Sharing
CSCI1600: Embedded and Real Time Software
Multithreading.
CPU scheduling decisions may take place when a process:
CSCI1600: Embedded and Real Time Software
Processes and operating systems
Outline Chapter 3: Processes Chapter 4: Threads So far - Next -
Chapter 4: Threads.
Uniprocessor Scheduling
CSE 153 Design of Operating Systems Winter 2019
Synchronization and liveness
Presentation transcript:

Lecture 10 – Using Thread Pools Housekeeping:  exam return Monday  Programming assignment on Monday Purpose: practice use of the mechanisms we’ve discussed The next assignment will be more oriented toward system design using concurrency Implement the synchronizers of Section 5.5

Quickly, task cancellation Chapter 7, not covering in detail Only a few observations on cancellation  Why? Nobody cares about the work a thread is doing any more – taking too long, some other thread got the answer, another thread got an error that is fatal for the app as a whole  How? Cooperatively! Much saner than C signal (interrupt) handling Thread handles cancellation only when in known state C program may have to handle a signal in any state  Beware: Intrinsic locks don’t respond to interruption  Daemon thread: in Java a thread whose existence doesn’t preclude JVM shutdown (e.g. GC)

Chapter 8 – Applying Thread Pools A lot of Java-specific details in this chapter Lecture will try to illuminate the underlying issues a bit more  Client dependence on execution policy  Thread-pool sizing  Queuing policy  Saturation policy  Thread-creation

Client dependence on execution policy Tasks that use objects that rely on thread- confinement for safety  Recall the single-threaded executor Inter-dependent tasks – ones that rely on state changes or results produced by other tasks  Avoiding thread-starvation deadlock  Timing-sensitive tasks – a less dramatic form of starvation

Thread-starvation deadlock Task A, currently executing in a thread, requires results from task B Task B cannot be executed because there are no available threads in the thread pool Obvious possibility in a single-thread executor but can happen with any executor that has a maximum number of threads  Consider: is the situation any better using Thread directly?

You can have more than one thread pool Suggestion: in any given thread-pool have homogeneous tasks – similar lifetimes, similar work.  It is hard to choose thread-pool policies if arbitrary tasks may be thrown at the pool Long-running tasks keep short-running tasks from executing Priority between vastly dissimilar tasks may be hard to pin down  Neither thread pools nor threads are so expensive that you have to worry very much about whether you have 100 or 200 You may have to worry about the difference between 100 and 1000 Remember that once a thread pool assigns a task to a thread the JVM schedules that thread just like any other

Thread-pool sizing Three thread states: waiting, ready-to-run, running  Waiting threads main effect is that they take up memory  Ready-to-run threads compete for the processor; lots of ready threads may make each scheduling decision more costly  Running threads – limited to the number of processors  Switching between ready and run states is relatively costly

Pool sizing (2) The following are all system-wide (not per thread- pool) considerations Too many waiting threads is bad  How many is too many? Too many ready threads is bad  How many is too many?  Better question: how many is enough? Answer: too few running threads is bad, so ideally # of ready threads is 0 and number of running threads = number of CPUs.

Pool sizing (3) For a resource-limited pool consider  Characteristics of tasks it will service Task execution time Task wait time  and how many resources should be devoted to those tasks What else does the system have to do concurrently?  Nthreads = Ncpu * U *(1 + W/C) U == desired CPU utilization for the whole pool W == task wait time, C == task compute time Nthreads * (C/(C+W)) == Ncpu * U

Pool sizing (4) Nthreads * per-task-disk-utilization = desired pool- disk-utilization Nthreads * per-task-network-utilization = desired pool-disk-utilization Etc. Pool size = minimum of all of the above – it doesn’t help to make it any bigger

Pool sizing (5) Need to be concerned also with the size of the queue “in front of” the thread pool  Implicit assumption: a task can be represented by much less data than a thread Number of waiting tasks >> number of threads On the other hand…  What are queuing systems good for? Smoothing out variations in arrival rate and service time If work continuously arrives at a faster rate than it can be performed then queue length and wait times go to infinity Argues for finite queue size as well as finite pool size

Queuing Policy Fundamental question: bounded or unbounded queue  Unbounded queue allows memory exhaustion and unbounded wait times  Bounded queue raises question of what to do when it is full – the saturation policy Abort – throw an exception Discard – silently ignore submitted task Discard oldest – or other policy that throws away some already queued task Caller runs – run the task in the submitting thread Note: no predefined caller waits policy (see Listing 8.4)

Work rejection by the system In the exam problem work is internally generated by the application. Conceptually easy to slow down work submission What if work is being delivered by an external source, e.g. over the network – need to think about how back pressure can be exerted on the work sources  Classic denial of service attack – no effective way to exert such backpressure  Other kinds of DoS may work by breaking some service (different set of issues)

Discussion Given the dangers of unbounded thread pools and unbounded queues why is an unbounded queue the default for fixed size thread pools and why do cachedThread pools support an unlimited number of threads?

Thread Creation Thread pools need to create threads from time to time A default thread Factory is supplied but you can customize the thread pool with your own factory (what is a factory?)  Statistics gathering  Customized unhandled exception processing  Customized security policies  … All this can be done without using a custom factory without using a thread pool at all but a thread pool provides a nice point of focus