Lukas has posted 6 posts at DZone. You can read more from them at their website. View Full User Profile

Fork/Join in slow motion

  • submit to reddit
Have you ever wondered how fork/join algorithm really works? You can read a description but it is much better to actually see how the tasks are split, how the threads process them, how work stealing works and so on. And that’s exactly what we have done on the last GPars Hackathon. We have created demo that visualizes fork/join in slow motion.


(alternatively you can download and execute this jar

You can notice several things

  1. A thread stays in its column. Threads do not compete, each of them works on its part of the problem.
  2. If a thread is forced to steal some work it takes a big chunk so it does not have to go stealing soon again.
  3. It does not run in the sandbox. Th jsr166y library uses sun.misc.Unsafe which is not part of public SDK API. That’s why you have to trust my self-signed certificate.

Source codes are available here (Groovy, Java). The demo was brought to you by me, Jety and Václav.
Your rating: None Average: 5 (1 vote)
Published at DZone with permission of its author, Lukas Krecan.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Hamlet D'Arcy replied on Tue, 2011/03/29 - 12:57am

This is actually pretty interesting. Turn the speed up, the number of threads up, and random data... and you start to see surprising patterns in how work is completed.


Also, the whole demo is only 250 lines of code. Check out the source:;a=blob_plain;f=src/test/groovy/groovyx/gpars/samples/forkjoin/DemoVisualForkJoinMergeSort.groovy;hb=373d0bad2a0ce2521cd36055e475cb4f47e7686c

Kamil Szabo replied on Tue, 2011/03/29 - 2:08am

And does this helps run java code on more cores at once? I was watching some official podcast implying that yes, but I've also red lot's of articles that says no. So what is definitive answer, and if no, what is this good for?

Hamlet D'Arcy replied on Tue, 2011/03/29 - 3:11am in response to: Kamil Szabo

Fork/Join absolutely 100% helps Java code run on more cores in parallel. What article did you read that said Fork/Join did not help with parallelization?


Martijn & Ben V... replied on Tue, 2011/03/29 - 6:27am

Cool demo! I've been presenting on F/J and having static diagrams just doesn't do it justice! I'd like to ask for permission to use your code in future Java 7 presentations, let me know! Cheers, Martijn (@java7developer)

Ben Anderson replied on Tue, 2011/03/29 - 10:31am

very cool, demo. visiualizing mult-threaded programming makes it easier to grasp.

Lukas Krecan replied on Wed, 2011/03/30 - 2:37am

@Martijn Sure, use it as however you want. I am glad you find it useful. The code is licensed under Apache 2 license.

Liezel Jane Jandayan replied on Mon, 2011/09/19 - 1:18am

Conventionally a computer consists of some form of memory for data storage, at least one element that carries out arithmetic and logic operations, and a sequencing and control element that can change the order of operations based on the information that is stored. Peripheral devices allow information to be entered from an external source, and allow the results of operations to be sent out.-Any Lab Test Now

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.