Fork/Join and Akka: parallelizing the Java platform BeJUG conference
Join the DZone community and get the full member experience.Join For Free
here is another article on a bejug conference. this time, sander mak, software developer and architect at info support, the netherlands, gave us an overview of two concurrency frameworks: fork/join and akka.
fork/join is a framework that was added to the standard libraries starting with jdk 7. akka is an open-source alternative with emphasis on the resilience of concurrent process.
setting the scene
the cpu speed has been growing up these last years until reaching some kind of a technical limit at around 3,5 ghz. right now, a cpu is mainly idling while waiting for its i/o. that's why the new trend is to have multiple cpus.
but as sander quote: "the number of idle cores in my machine doubles every two years". there is an architectural mismatch because developers use to believe that the compiler and/or the jvm can handle parallelism on their own. but unfortunately, this isn't true.
the first demos are declined around the computation of fibonacci's suite whose definition is
of course, the objective here is not to find an optimal solution to that problem (transforming the recursive definition into an iterative form) but just apply a concurrent computation of the recursive form.
we can solve this problem by creating 1 thread to compute fib(n-1) and another thread for fib(n-2), then wait they have finished their computation and adding the results.
immediately, the number of thread explodes.
if we implement the same algorithm with 2
objects and a thread pool, the number grows slowlier but is high anyway.
the problem is that the current thread blocks while its two children finish.
- with fork/join, the thread dependency is explicit and thus the join method call doesn't block the current thread.
join/fork works with an auto-sized thread pool. each worker thread is assigned a task queue which gets fed by the fork method calls. the interesting behavior is that a worker is allowed to steal work from the task queue of another worker.
another, more advanced, demo was also perform, demonstrating how to make a dichotomic search on a large set of cities to find which one are within a certain distance from a point. of course, the algorithm is implemented with fork/join.
all the code of those examples is available on http://bit.ly/bejug-fj .
api & patterns
the algorithm must be acyclic (no thread can work with another thread that is already present in its call stack) and cpu-bound. i/o-bound problems wait a lot on blocking system calls and thus prevent those threads to perform other tasks.
to avoid that the overhead consumes all the computation time, you must set a threshold to decide whether the problem should be solved sequentially or in parallel. this leads to define work chunks that are processed in parallel but each steps inside the same chunk are processed sequentially.
fork once, fool me twice
some algorithm implementations allows to reuse the current thread to do come computation instead of forking a new task, thus limiting the overhead.
there exist convenience methods :
- method invoke() is semantically equivalent to fork(); join() but always attempts to begin execution in the current thread.
- method invokeall performs the most common form of parallel invocation: forking a set of tasks and joining them all.
future and comparisons
fork/join creates threads. it is thus currently forbidden in the ejb spec. when it comes to cdi or servlet specs, we are there navigating in some kind of grey zone. maybe this could work with jca work manager. @asynchronous could be used as an alternative.
anyway, it is foreseen that javaee 7 spec may contain java.util.concurrent package.
compared with fork/join, the more classic executorservice doesn't allow work stealing. it is better suited at coarse-grained independent tasks. bounded thread pools supports blocking i/o better.
mapreduce implementations they are targeted at cluster and not single jvm. while fork/join is targeted at recursive working, mapreduce is often working on a single map. furthermore, mapreduce has no inter-node communication and thus doesn't allow work stealing.
the popular critics about fork/join are:
- the implementation is really complex.
- the scalability is unknown above 100 cores. which may seem many for a cpu but is far below current standards for a gpu.
- the one-to-one mapping between the thread pool size and the core numbers is probably too low-level.
with the availability of jdk8, the fork/join api could be extended with methods on collections working with lambdas. there is also a countedcompleter forkjointask implementation that is better at working with i/o-based tasks and that is currently contained in jsr-166-extra).
akka is a concurrency framework written in scala that offers a java api. i wouldn't introduce akka better than their author so here is what they say about it:
we believe that writing correct concurrent, fault-tolerant and scalable applications is too hard. most of the time it’s because we are using the wrong tools and the wrong level of abstraction. akka is here to change that. using the actor model we raise the abstraction level and provide a better platform to build correct concurrent and scalable applications. for fault-tolerance we adopt the “let it crash” model which have been used with great success in the telecom industry to build applications that self-heals, systems that never stop. actors also provides the abstraction for transparent distribution and the basis for truly scalable and fault-tolerant applications.
an actor is an object with a local state, a local behavior and a mailbox to receive messages sent to it. an actor processes only one message at a time. and as they are lightweight (around 400 bytes of memory per actor), you can instantiate many of them in a standard jvm heap.
the receive method of an actor is called when a message processeing begins and it is where all the processing is done. the framework itself is responsible for the actor management (thread pooling, dispatching, ...).
using a forkjoinpool with akka scales very well.
a demo of akka usage is available on http://bit.ly/bejug-akka .
Published at DZone with permission of Francois Roland. See the original article here.
Opinions expressed by DZone contributors are their own.