80 likes | 194 Views
CSC 480 - Multiprocessor Programming, Spring, 2012. Chapter 8 – Applying Thread Pools Dr. Dale E. Parson, week 10. Types of tasks requiring specific execution policies. Dependent tasks
E N D
CSC 480 - Multiprocessor Programming, Spring, 2012 Chapter 8 – Applying Thread Pools Dr. Dale E. Parson, week 10
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.
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 Nthreads = Ncpu x Ucpu * (1 + W / C) where Ncpu is number of CPUs, Ucpu is target CPU utilization, 0 <= Ucpu <= 1 W / C is ratio of wait time to compute time
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.
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.
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.
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.
Parallelizing recursive algorithms • See examples from Summer 2010 handout. • Loop parallelization. void processSequentially(List<Element> elements) { for (Element e : elements) process(e); } void processInParallel(Executor exec, List<Element> elements) { for (final Element e : elements) exec.execute(new Runnable() { public void run() { process(e); } }); }