Download presentation
Presentation is loading. Please wait.
Published byBenjamin Hancock Modified over 9 years ago
1
CSC 480 - Multiprocessor Programming, Spring, 2012 Chapter 8 – Applying Thread Pools Dr. Dale E. Parson, week 10
2
Types of tasks requiring specific execution policies Dependent tasks Interacting tasks require more careful execution management than independent tasks. The Executor must manage their interaction dynamics. Tasks that exploit thread confinement may require a single- threaded Executor. Response-time-sensitive tasks require sufficient parallelism for responsiveness. Current latency-driven assignment. ThreadLocal should not be used in pool threads to communicate values between tasks.
3
Thread Starvation Deadlock Executing interdependent tasks in an Executor may lead to thread starvation deadlock if the Executor schedules tasks in a way that causes waits on tasks that never run. Long running tasks can cause responsiveness problems for thread pools. Sizing thread pools N threads = N cpu x U cpu * (1 + W / C) where N cpu is number of CPUs, U cpu is target CPU utilization, 0 <= U cpu <= 1 W / C is ratio of wait time to compute time
4
Configuring ThreadPoolExecutor ThreadPoolExecutor is the base implementation for cached thread pool, fixed thread pool, and scheduled executor. It supports configuration of core and max pool sizes, keep alive time, a work queue, a thread factory, and a rejected execution handler. Creating and maintaining a custom Executor is complicated.
5
ThreadPoolExecutor Heuristics LinkedBlockQueue is the default. It does not throttle back the task submitter(s). A bounded queue throttles back submitters. A large bounded queue coupled with a small thread pool reduces memory usage, CPU usage and context switching, potentially at the cost of throughput. Use a Semaphore to throttle submitter threads. For large or unbounded pools SyncronousQueue is an option. It is practical only if the pool is effectively unbounded or if rejecting excess tasks is acceptable. – Bounding the thread pool or work queue is suitable only when tasks are independent.
6
Saturation Policies Full bounded work queue == saturation. Abort policy causes execute to throw unchecked RejectedExecutionException. Caller-runs policy borrows the submitting thread instead of using a pool thread. Discard policy silently discards submissions. Discard oldest policy silently discards the oldest waiting submission, retries execute.
7
ThreadFactory and Thread subclasses Custom Thread subclass with extensions. Build an UncaughtExceptionHandler. Debug logging. Set up custom ThreadLocal data. ThreadPoolExecutor can be customized after construction. It can be subclassed to extend beforeExecute and afterExecute to add logging, timing, monitoring or statistics gathering.
8
Parallelizing recursive algorithms See examples from Summer 2010 handout. Loop parallelization. void processSequentially(List elements) { for (Element e : elements) process(e); } void processInParallel(Executor exec, List elements) { for (final Element e : elements) exec.execute(new Runnable() { public void run() { process(e); } }); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.