chapter 8 applying thread...
TRANSCRIPT
![Page 1: Chapter 8 Applying Thread Poolsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch8-presentation.pdf · Starvation deadlock •Simplest example of deadlock: –Single-threaded executor](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f3f4c966e7a0d6c891adef2/html5/thumbnails/1.jpg)
Chapter 8 Applying Thread Pools
Magnus Andersson
![Page 2: Chapter 8 Applying Thread Poolsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch8-presentation.pdf · Starvation deadlock •Simplest example of deadlock: –Single-threaded executor](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f3f4c966e7a0d6c891adef2/html5/thumbnails/2.jpg)
Execution policies
• Not all task are suitable for all execution policies
– Dependent task
– Task exploiting thread confinement
– Response time sensitive tasks
– ThreadLocal tasks
![Page 3: Chapter 8 Applying Thread Poolsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch8-presentation.pdf · Starvation deadlock •Simplest example of deadlock: –Single-threaded executor](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f3f4c966e7a0d6c891adef2/html5/thumbnails/3.jpg)
Starvation deadlock
• Simplest example of deadlock:
– Single-threaded executor
– Task A submits a new task B, which A depends on
– Deadlock!
– Easy to extrapolate to a concurrent executor
• Make sure that your pool size is large enough
![Page 4: Chapter 8 Applying Thread Poolsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch8-presentation.pdf · Starvation deadlock •Simplest example of deadlock: –Single-threaded executor](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f3f4c966e7a0d6c891adef2/html5/thumbnails/4.jpg)
Sizing the thread pool
• Don’t hard code!
• The black art of thread pool sizing
– Is your program compute intensive?
•
– Does your program use a fair amount of blocking, I/O, etc?
•
![Page 5: Chapter 8 Applying Thread Poolsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch8-presentation.pdf · Starvation deadlock •Simplest example of deadlock: –Single-threaded executor](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f3f4c966e7a0d6c891adef2/html5/thumbnails/5.jpg)
The ThreadPoolExecutor constuctor
public ThreadPoolExecutor(
int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable>
workQueue,
ThreadFactory
threadFactory,
RejectedExecutionHandler
handler
) { … }
![Page 6: Chapter 8 Applying Thread Poolsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch8-presentation.pdf · Starvation deadlock •Simplest example of deadlock: –Single-threaded executor](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f3f4c966e7a0d6c891adef2/html5/thumbnails/6.jpg)
The ThreadPoolExecutor constuctor
public ThreadPoolExecutor(
int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable>
workQueue,
ThreadFactory
threadFactory,
RejectedExecutionHandler
handler
) { … }
# of desired threads
Upper bound on # of threads
How long idle threads are kept around
keepAliveTime time unit (?)
![Page 7: Chapter 8 Applying Thread Poolsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch8-presentation.pdf · Starvation deadlock •Simplest example of deadlock: –Single-threaded executor](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f3f4c966e7a0d6c891adef2/html5/thumbnails/7.jpg)
The ThreadPoolExecutor constuctor
public ThreadPoolExecutor(
int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable>
workQueue,
ThreadFactory
threadFactory,
RejectedExecutionHandler
handler
) { … }
![Page 8: Chapter 8 Applying Thread Poolsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch8-presentation.pdf · Starvation deadlock •Simplest example of deadlock: –Single-threaded executor](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f3f4c966e7a0d6c891adef2/html5/thumbnails/8.jpg)
The work queue
Work queue
Co
res
Threads
Client
![Page 9: Chapter 8 Applying Thread Poolsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch8-presentation.pdf · Starvation deadlock •Simplest example of deadlock: –Single-threaded executor](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f3f4c966e7a0d6c891adef2/html5/thumbnails/9.jpg)
The work queue Unbounded blocking queue
Unbounded blocking queue
Co
res
Threads
Client
![Page 10: Chapter 8 Applying Thread Poolsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch8-presentation.pdf · Starvation deadlock •Simplest example of deadlock: –Single-threaded executor](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f3f4c966e7a0d6c891adef2/html5/thumbnails/10.jpg)
The work queue Unbounded blocking queue
Co
res
Threads
Unbounded blocking queue
Client
![Page 11: Chapter 8 Applying Thread Poolsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch8-presentation.pdf · Starvation deadlock •Simplest example of deadlock: –Single-threaded executor](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f3f4c966e7a0d6c891adef2/html5/thumbnails/11.jpg)
• Arrival rate > Handling rate
The work queue Unbounded blocking queue
Co
res
Threads
Client
Unbounded blocking queue
![Page 12: Chapter 8 Applying Thread Poolsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch8-presentation.pdf · Starvation deadlock •Simplest example of deadlock: –Single-threaded executor](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f3f4c966e7a0d6c891adef2/html5/thumbnails/12.jpg)
• ArrayBlockingQueue
• LinkedBlockingQueue
• PriorityBlockingQueue
The work queue Bounded blocking queue
Co
res
Threads
Bounded blocking queue
Client
![Page 13: Chapter 8 Applying Thread Poolsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch8-presentation.pdf · Starvation deadlock •Simplest example of deadlock: –Single-threaded executor](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f3f4c966e7a0d6c891adef2/html5/thumbnails/13.jpg)
• Direct hand-off to the thread that will handle the task
The work queue Synchronous “queue”
Busy threads
Client
Free threads
![Page 14: Chapter 8 Applying Thread Poolsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch8-presentation.pdf · Starvation deadlock •Simplest example of deadlock: –Single-threaded executor](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f3f4c966e7a0d6c891adef2/html5/thumbnails/14.jpg)
The ThreadPoolExecutor constuctor
public ThreadPoolExecutor(
int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable>
workQueue,
ThreadFactory
threadFactory,
RejectedExecutionHandler
handler
) { … }
![Page 15: Chapter 8 Applying Thread Poolsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch8-presentation.pdf · Starvation deadlock •Simplest example of deadlock: –Single-threaded executor](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f3f4c966e7a0d6c891adef2/html5/thumbnails/15.jpg)
Saturation policy C
ores
Threads
Bounded blocking queue Client
What am I supposed to do
now?
![Page 16: Chapter 8 Applying Thread Poolsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch8-presentation.pdf · Starvation deadlock •Simplest example of deadlock: –Single-threaded executor](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f3f4c966e7a0d6c891adef2/html5/thumbnails/16.jpg)
Saturation policy
• Abort – Throws a RejectedExecutionException
• Discard
– Silently kill submitted task
• Discard-oldest
– Silently kill oldest task
• Caller-runs
– Push work task back to client
• Effectively slows down submission rate since client will be busy for a while and can’t (shouldn’t) submit new tasks
![Page 17: Chapter 8 Applying Thread Poolsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch8-presentation.pdf · Starvation deadlock •Simplest example of deadlock: –Single-threaded executor](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f3f4c966e7a0d6c891adef2/html5/thumbnails/17.jpg)
Blocking execute The fifth Beatle
• Doesn’t exist
• Blocks caller if the queue is full
• Easily implemented using semaphores
public void submitTask(final Runnable command) throws InterruptedException {
semaphore.aquire();
try {
exec.execute(new Runnable() {
public void run() {
try {
command.run();
} finally {
semaphore.release();
}
}
});
} catch (RejectedExecutionException e) {
semaphore.release();
}
}
![Page 18: Chapter 8 Applying Thread Poolsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch8-presentation.pdf · Starvation deadlock •Simplest example of deadlock: –Single-threaded executor](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f3f4c966e7a0d6c891adef2/html5/thumbnails/18.jpg)
The ThreadPoolExecutor constuctor
public ThreadPoolExecutor(
int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable>
workQueue,
ThreadFactory
threadFactory,
RejectedExecutionHandler
handler
) { … }
![Page 19: Chapter 8 Applying Thread Poolsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch8-presentation.pdf · Starvation deadlock •Simplest example of deadlock: –Single-threaded executor](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f3f4c966e7a0d6c891adef2/html5/thumbnails/19.jpg)
Thread factory
• One method: newThread
• Configure thread pool threads
• Naming, logging, exception handling
public class MyThreadFactory implements ThreadFactory {
private final String poolName;
public MyThreadFactory(String poolName) {
this.poolName = poolName;
}
public Thread newThread(Runnable runnable) {
return new MyAppThread(runnable, poolName);
}
}
// See MyAppThread listing in book
![Page 20: Chapter 8 Applying Thread Poolsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch8-presentation.pdf · Starvation deadlock •Simplest example of deadlock: –Single-threaded executor](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f3f4c966e7a0d6c891adef2/html5/thumbnails/20.jpg)
Post-construction modification
• Possible, but could be dangerous
– Single-threaded executor with increased pool size?
– Pool size not enough to handle dependent tasks?
• If this is a problem: encapsulate the Executor
![Page 21: Chapter 8 Applying Thread Poolsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch8-presentation.pdf · Starvation deadlock •Simplest example of deadlock: –Single-threaded executor](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f3f4c966e7a0d6c891adef2/html5/thumbnails/21.jpg)
Additional ThreadPoolExecutor hooks
• Discriptive names:
– beforeExecute
– afterExecute
– terminated
• Runs at the very end
• Logging, statistics, etc…
• It’s OK to use ThreadLocal between beforeExecute and afterExecute
![Page 22: Chapter 8 Applying Thread Poolsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch8-presentation.pdf · Starvation deadlock •Simplest example of deadlock: –Single-threaded executor](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f3f4c966e7a0d6c891adef2/html5/thumbnails/22.jpg)
Parallelizing algorithms
void processSequentially(List<Element> elements) {
for (Element e : elements)
process(e)
}
void processInParallel(Executor exec, List<Elements> elements) {
for (final Element e : elements) {
exec.execute(new Runnable() {
public void run() { process(e); }
});
}
}
![Page 23: Chapter 8 Applying Thread Poolsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch8-presentation.pdf · Starvation deadlock •Simplest example of deadlock: –Single-threaded executor](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f3f4c966e7a0d6c891adef2/html5/thumbnails/23.jpg)
Parallelizing recursive algorithms
public<T> void sequentialRecursive(List<Node<T>> nodes,
Collection<T> results)
{
for (Node<T> n : nodes) {
results.add(n.compute());
sequentialRecursive(n.getChildren(), results);
}
}
public<T> void parallelRecursive(final Executor exec,
List<Node<T>> nodes, final Collection<T> results)
{
for (Node<T> n : nodes) {
exec.execute(new Runnable() {
public void run() { results.add(n.compute()); }
});
parallelRecursive(n.getChildren(), results);
}
}
![Page 24: Chapter 8 Applying Thread Poolsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch8-presentation.pdf · Starvation deadlock •Simplest example of deadlock: –Single-threaded executor](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f3f4c966e7a0d6c891adef2/html5/thumbnails/24.jpg)
Exercise Ray tracing
![Page 25: Chapter 8 Applying Thread Poolsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch8-presentation.pdf · Starvation deadlock •Simplest example of deadlock: –Single-threaded executor](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f3f4c966e7a0d6c891adef2/html5/thumbnails/25.jpg)
Exercise
Primary rays
Image plane
Eye
Scene objects (only spheres)
Shadow rays
Light source
Pixels
![Page 26: Chapter 8 Applying Thread Poolsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch8-presentation.pdf · Starvation deadlock •Simplest example of deadlock: –Single-threaded executor](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f3f4c966e7a0d6c891adef2/html5/thumbnails/26.jpg)
Exercise
• Ray tracing – “embarrassingly parallel”
• Each pixel is its own task – Setup primary ray
– Test intersection with all scene objects
– Use closest hit as pixel color
– If hit: • Setup shadow ray
• Test intersection with all scene objects
• Make pixel dark if anything is hit
• All of this is already done
![Page 27: Chapter 8 Applying Thread Poolsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch8-presentation.pdf · Starvation deadlock •Simplest example of deadlock: –Single-threaded executor](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f3f4c966e7a0d6c891adef2/html5/thumbnails/27.jpg)
Exercise • Your task:
1. traceSerial() serially loops over all screen pixels and performs ray tracing. Create a parallel version in traceParallel() which results in the same output image. You must configure your own ThreadPoolExecutor using the constructor (and not the factory), and use it to dispatch your tasks. Tweak it to gain maximum performance.
You can switch algorithms with the private static final boolean serial flag at the top of RayTracer.java. No changes to GFX.java should be necessary.
2. Gather statistics using beforeExecute() and afterExecute() on what the average and the maximum task run times were. Print out the result in terminate().
• Optional: Separate the shadow rays to separate tasks to be re-inserted in to the work queue. The maximum thread count and queue size may only be 40 or less each.
Although you’re not likely to see a speedup from this, it is an interesting concurrency problem to make sure that the output image is still correct (while not starving any threads to death).
![Page 28: Chapter 8 Applying Thread Poolsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch8-presentation.pdf · Starvation deadlock •Simplest example of deadlock: –Single-threaded executor](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f3f4c966e7a0d6c891adef2/html5/thumbnails/28.jpg)
Exercise
• Alternatively: If you have your own idea of a problem that could be parallelized using thread pools, you are welcome to do that instead. The same criteria apply – you may not use the factory-method to create a ThreadPoolExecutor, but must use the constructor. You must also experiment and tweak the settings to get good overall performance.
You’ll also need to gather some statistics during the execution. Average and maximum task running times, for example.
![Page 29: Chapter 8 Applying Thread Poolsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch8-presentation.pdf · Starvation deadlock •Simplest example of deadlock: –Single-threaded executor](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f3f4c966e7a0d6c891adef2/html5/thumbnails/29.jpg)
Fin
![Page 30: Chapter 8 Applying Thread Poolsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch8-presentation.pdf · Starvation deadlock •Simplest example of deadlock: –Single-threaded executor](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f3f4c966e7a0d6c891adef2/html5/thumbnails/30.jpg)
[BACKUP] Sizing the thread pool
= Runtime.getRuntime().availableProcessors()
= target utilization [0, 1]
= Wait to compute time ratio
• Example:
– 8 processors targeting 0.5 utilization, with a profiled wait time of 2 and compute time of 5:
– Threads = 8 * 0.5 * (1 + 2/5) = 5.6
![Page 31: Chapter 8 Applying Thread Poolsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch8-presentation.pdf · Starvation deadlock •Simplest example of deadlock: –Single-threaded executor](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f3f4c966e7a0d6c891adef2/html5/thumbnails/31.jpg)
[BACKUP] Standard ThreadPoolExecutors
• Executor factory: – newCachedThreadPool
• SynchronousQueue • corePoolSize = 0. maximumPoolSize = INF. Timeout = 1 min
– newFixedThreadPool • Unbounded LinkedBlockingQueue • corePoolSize = maximumPoolSize
– newSingleThreadExecutor • Unbounded LinkedBlockingQueue • corePoolSize = maximumPoolSize = 1
– newScheduledThreadExecutor • DelayQueue • corePoolSize = maximumPoolSize = 1
![Page 32: Chapter 8 Applying Thread Poolsfileadmin.cs.lth.se/cs/Education/EDA015F/2013/Ch8-presentation.pdf · Starvation deadlock •Simplest example of deadlock: –Single-threaded executor](https://reader034.vdocuments.us/reader034/viewer/2022042802/5f3f4c966e7a0d6c891adef2/html5/thumbnails/32.jpg)
[BACKUP] The ThreadPoolExecutor constuctor
public ThreadPoolExecutor(
int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler)