2009/01/02

Devoxx: From Concurrent to Parallel by Brian Goetz

The trend of getting faster processors every year just stopped a few years ago. Modern CPUs now get more cores, but each single core is actually slower than its single-core ancestor. Buying new hardware won't automatically make your software run faster anymore, unless your software is built for parallel processing. The more cores are added to the cpu, the more important it becomes to find fine-grained parallelism in your apps, in other words, to split coarse-grained request into smaller chunks.

Java 5 brought the basic concurrency support beyond Threads into Java: Futures + Executors. Those are very powerful concepts, but the resulting code is complex to maintain. Java 7 builds on the existing java.util.concurrent foundation by providing the 'fork-join' framework to Java (JSR166).

The concept behind the fork-join framework is "divide and conquer": a job is recursively split in halves, until a manageable size for the sub-jobs is found. The correct size for the chunks is typically determined by the number of cores (=Runtime.availableProcessors()). Another key concept is "work stealing": if one thread finishes its job early, it 'steals' work from other 'double-ended queues' (Deque) of other threads. That way all cores can remain busy while limitting congestion for the queue.

Java 7 java.util.concurrent.forkjoin:
  • basic fork join classes: RecursiveAction, AsyncAction, CyclicAction ...
  • higher abstraction: ParallelArray offers parallel functions out of the box: filter, map, replace, aggregate ... I like the declarative style use for this class: it chains declarations before firing the action: students.withFilter(isSenior).withMapping(selectGpa).summary();

Definitively an interesting step for Java, although once you enter a multi-threaded environment, you loose many safety guarantees from Java and you as a developer has to ensure that concurrency doesn't break your app. The growing number of cores puts parallel programming research in the spotlight, with the renewed interest in functional languages like haskell, erlang and Scala for example.

References:
Post a Comment