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 thru Future.get()
  • ExecutorService.shutdown()

map and reduction