Java Fork Join
MASUD HASAN
Some history
 Until recently, most computers only had 1 processing core
 Multithreading was simulated on a single core
 Not true concurrency
 Serial approach to algorithms often sufficed
multicore
 Concurrency and performance are tied together
 Real concurrency – separate threads can execute on different
cores at the same time
 The JVM runtime controls scheduling
Fork and Join Framework
 Some types of algorithms exist that require tasks to create subtasks
and communicate with each other to complete.
 Those are the “divide and conquer” algorithms, which are also
referred to as “map and reduce,”
Example: partial sums of an Array of Integers
 Divide the array into the number n of available physical processing units
 Create Callable instances to compute each partial sum
 Submit them to an executor managing a pool of n threads
 Collect the result to compute the final sum
Fork Join
 The core addition is a new ForkJoinPool executor that is dedicated to running
instances implementing ForkJoinTask.
 ForkJoinTask objects feature two specific methods:
 fork(): allows a ForkJoinTask to be planned for asynchronous execution
 join() method allows a ForkJoinTask to wait for the completion of another one
 There are 2 types of ForkJoinTask:
 RecursiveAction represent executions that do not yield a return value
 Instances RecursiveTask yields return values
ForkJoinPool
 Implements ExecutorService
 Autosizing workers
 Double‐ended queue
 Workstealing algorithm
Pooling/ExecutorService then why
 Coarse‐grained independent tasks
 Recursively decomposed tasks spend most Cme waiCng
 In normal threadpool: starvaCon
 Task‐queue of threadpool‐backed ExecutorService not opCmized f
or many small tasks
 No workstealing
Map/Reduce
FORK/JOIN HADOOP MAP/REDUCE
Environment Single JVM Cluster
Model Recursive forking Often single map
Scales with Cores/CPUs Nodes
Worker interaction Workstealing No inter‐node communicati
on
Fork Join & JAVA EE
 ForkJoinPool starts threads
 Illegal in EJBs
 Fair game in servlets/CDI beans
 Don’t create ForkJoinPool for each request
 Idea: WorkManager to create poolthreads
 Single pool, async submit(ForkJoinTask)
 Don’t Ce up request thread: Servlet 3.0
Other concurrency models
 Actors (AKKA)
 Sofware transactional memory
 Dataflow concurrency (AKKA)
 Agents

Java fork join

  • 1.
  • 2.
    Some history  Untilrecently, most computers only had 1 processing core  Multithreading was simulated on a single core  Not true concurrency  Serial approach to algorithms often sufficed
  • 3.
    multicore  Concurrency andperformance are tied together  Real concurrency – separate threads can execute on different cores at the same time  The JVM runtime controls scheduling
  • 4.
    Fork and JoinFramework  Some types of algorithms exist that require tasks to create subtasks and communicate with each other to complete.  Those are the “divide and conquer” algorithms, which are also referred to as “map and reduce,”
  • 5.
    Example: partial sumsof an Array of Integers  Divide the array into the number n of available physical processing units  Create Callable instances to compute each partial sum  Submit them to an executor managing a pool of n threads  Collect the result to compute the final sum
  • 6.
    Fork Join  Thecore addition is a new ForkJoinPool executor that is dedicated to running instances implementing ForkJoinTask.  ForkJoinTask objects feature two specific methods:  fork(): allows a ForkJoinTask to be planned for asynchronous execution  join() method allows a ForkJoinTask to wait for the completion of another one  There are 2 types of ForkJoinTask:  RecursiveAction represent executions that do not yield a return value  Instances RecursiveTask yields return values
  • 7.
    ForkJoinPool  Implements ExecutorService Autosizing workers  Double‐ended queue  Workstealing algorithm
  • 8.
    Pooling/ExecutorService then why Coarse‐grained independent tasks  Recursively decomposed tasks spend most Cme waiCng  In normal threadpool: starvaCon  Task‐queue of threadpool‐backed ExecutorService not opCmized f or many small tasks  No workstealing
  • 9.
    Map/Reduce FORK/JOIN HADOOP MAP/REDUCE EnvironmentSingle JVM Cluster Model Recursive forking Often single map Scales with Cores/CPUs Nodes Worker interaction Workstealing No inter‐node communicati on
  • 10.
    Fork Join &JAVA EE  ForkJoinPool starts threads  Illegal in EJBs  Fair game in servlets/CDI beans  Don’t create ForkJoinPool for each request  Idea: WorkManager to create poolthreads  Single pool, async submit(ForkJoinTask)  Don’t Ce up request thread: Servlet 3.0
  • 11.
    Other concurrency models Actors (AKKA)  Sofware transactional memory  Dataflow concurrency (AKKA)  Agents