aspose file tools*
The moose likes Features new in Java 7 and the fly likes Real World Example for Fork/Join with Java SE 7 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Features new in Java 7
Bookmark "Real World Example for Fork/Join with Java SE 7" Watch "Real World Example for Fork/Join with Java SE 7" New topic
Author

Real World Example for Fork/Join with Java SE 7

Shiv Swaminathan
Ranch Hand

Joined: Jul 21, 2010
Posts: 48

Jeff, Can you provide us a real world example of Fork/Join feature usage of Java 7, which explain how the work breakdown happens and how the work stealing algorithm works inside?


SCEA EE5, CSM, PMP, IBM OOAD with UML, SCDJWS, SCWCD, SCJP 1.5
Jeffrey Friesen
Author
Ranch Hand

Joined: Dec 29, 2012
Posts: 67
    
    5

Hi Shiv,

Beginning Java 7 presents a matrix multiplication example of fork/join. There are many real world examples that use matrix multiplication -- see http://math.stackexchange.com/questions/41706/practical-uses-of-matrix-multiplication for some of them.

You might also want to check out stackoverflow.com's "How can I show that in Java Fork/Join framework work-stealing occurs?" topic (http://stackoverflow.com/questions/14076890/how-can-i-show-that-in-java-fork-join-framework-work-stealing-occurs).

All the best.

Jeff
Shiv Swaminathan
Ranch Hand

Joined: Jul 21, 2010
Posts: 48

Thank you Jeff for the examples.

I have some follow-up questions

When using fork/join for doing recursive multiplication or linear computing, won't there be problem if the order of the computation is not followed.

For eg: Lets take Fibonacci Series computation (Assuming we are going to use make the multiplication as two numbers as the Recursive task)
The result of one thread's computation is needed for the next thread, and we dont want to duplicate the computation of another thread also.

Does fork/join help here?
Can we use fork/join for all kind of recursive task even when there are dependencies between the results of the recursive task and when order of execution needs to be maintained.
Should we consider some guidelines before deciding to use fork/join or identifying a Recursive Task.

Also please explain me how does Java do the real work stealing?
Jeffrey Friesen
Author
Ranch Hand

Joined: Dec 29, 2012
Posts: 67
    
    5

Hi Shiv,

Alex Miller points out in his "Comparing concurrent frameworks" blog post (http://tech.puredanger.com/2011/01/14/comparing-concurrent-frameworks/comment-page-1/) that the fork/join framework handles dependencies. I recommend that you read Doug Lea's "A Java Fork/Join Framework" paper (http://gee.cs.oswego.edu/dl/papers/fj.pdf) to learn more about this framework and work stealing.

All the best.

Jeff
Shiv Swaminathan
Ranch Hand

Joined: Jul 21, 2010
Posts: 48

Oh Thank you Jeff.
Both the links you provided are really good.
I'm also going to experiment a program hands-on to show how dependencies are handled in Fork/Join.

Because looks like we have to programmatically handle it using the api's provided.

"Express dependencies directly using the RecursiveTask apis for fork, join, invoke, etc."
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Real World Example for Fork/Join with Java SE 7