6. Recursive, Pools
Recursive
Ex.: Sum calculation
- Split recursively, starting two threads from one
- Stop at some cutoff
- Join and sum
Avoids synchronous summing of thread results.
Total work:
More efficient: reuse one thread to sum one half, use other to sum other.
Executor Service
java.util.concurrent.ExecutorService
Executors.newFixedThreadPool(n)ExecutorService.submit(Runnable)- Returns
Future<Value>. Get with wait thruFuture.get()
- Returns
ExecutorService.shutdown()
map and reduction