Java fork join

430 views 11 slides Jan 13, 2018
Slide 1
Slide 1 of 11
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11

About This Presentation

fork join framework in java


Slide Content

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
ImplementsExecutorService
Autosizingworkers
Double‐endedqueue
Workstealingalgorithm

Pooling/ExecutorService then why
Coarse‐grainedindependenttasks
RecursivelydecomposedtasksspendmostCmewaiCng
Innormalthreadpool:starvaCon
Task‐queueofthreadpool‐backedExecutorServicenotopCmizedf
ormanysmalltasks
Noworkstealing

Map/Reduce
FORK/JOIN HADOOP MAP/REDUCE
Environment SingleJVM Cluster
Model Recursiveforking Oftensinglemap
Scaleswith Cores/CPUs Nodes
Worker interaction Workstealing Nointer‐nodecommunicati
on

Fork Join & JAVA EE
ForkJoinPoolstartsthreads
IllegalinEJBs
Fairgameinservlets/CDIbeans
Don’tcreateForkJoinPoolforeachrequest
Idea:WorkManagertocreatepoolthreads
Singlepool,asyncsubmit(ForkJoinTask)
Don’tCeuprequestthread:Servlet3.0

Other concurrency models
Actors (AKKA)
Sofwaretransactionalmemory
Dataflowconcurrency (AKKA)
Agents
Tags