Introduction to JDK7's Fork/Join framework. Present on May 3 2012 at the Belgian Java User Group (BeJUG). For more information see: http://www.bejug.org/confluenceBeJUG/display/BeJUG/ForkJoin+and+Akka
Demo code can be found at http://bit.ly/bejug-fj
10. Problem?
So let the compiler/runtime solve it!
n
<
2
n
if
(n
<
2)
{
return
n;
}
else
{
return
fib(n
-‐
1)
+
fib(n
-‐
2);
}
11. Problem?
So let the compiler/runtime solve it!
Unfortunately not in ,
but feasible in pure
functional languages like
12. First attempt
public
static
int
fib(final
int
n)
{
if
(n
<
2)
{
return
n;
}
else
{
final
int[]
t1
=
{0},
t2
=
{0};
Thread
thread1
=
new
Thread()
{
public
void
run()
{
t1[0]
=
fib(n
-‐
1);
}
};
Thread
thread2
=
new
Thread()
{
public
void
run()
{
t2[0]
=
fib(n
-‐
2);
}
};
thread1.start();
thread2.start();
thread1.join();
thread2.join();
return
t1[0]
+
t2[0];
}
} if
(n
<
2)
{
return
n;
}
else
{
return
fib(n
-‐
1)
+
fib(n
-‐
2);
}
13. Threads
Threads are mostly waiting
Improve with threadpooling
but not by much
starvation
What if we sum in a new thread?
synchronization
visibility issues (Java Memory Model)
14. ForkJoin
Fork:
Recursively decompose Result = 3
large task into subtasks
Fib(4)
Join:
Fib(3) Fib(2)
Await results of
recursive tasks
and combine Fib(2) Fib(1) Fib(1) Fib(0)
Fib(1) Fib(0)
34. ForkJoin & Java EE
ForkJoinPool creates threads
Illegal in EJBs
CDI/Servlet is a gray area
JCA/WorkManager could work
@Asynchronous as alternative
But: Java EE7 may contain javax.util.concurrent
35. Comparison
ExecutorService
Thread pooling (bounded or
unbounded)
Single work queue (no workstealing)
Coarse-grained independent tasks
Blocking I/O ok
36. Comparison
MapReduce
Environment Single JVM Cluster
Model Recursive forking Often single map
Scales with Cores/CPUs Nodes
Worker Workstealing No inter-node
interaction communication
38. Criticism
Complex implementation
(uses sun.misc.Unsafe)
Scalability > 100 cores?
Questionable assumption:
1-1 mapping worker
thread to core
Too low-level
39. Future
InfoQ: “What is supported out of the box?”
“Almost nothing".
We chickened out; we are not going to release
the layers on top of this
That means that right now, people who are using
this framework are going to be the people who
actually get into this parallel recursive
decomposition and know how to use it.
40. Future
JDK 8 plans
Parallel collections
Depends on Project Lambda
CountedCompleter for I/O
Some already available in jsr166extra
41. Future
int
findCities(List<String>
cities,
String
query)
{
Pattern
p
=
Pattern.compile(query)
return
cities.parallel()
.filter(c
=>
p.matcher(c).matches());
}
int
findNearestCities(List<String>
lines,
int
lat,
int
lng)
{
return
lines.parallel()
.map(c
=>
toCity(c))
.filter(c
=>
c.isNear(lat,
lng))
.sort();
}