my dog learned polymorphism*
The moose likes Threads and Synchronization and the fly likes Fork / Join Framework 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 » Threads and Synchronization
Bookmark "Fork / Join Framework" Watch "Fork / Join Framework" New topic
Author

Fork / Join Framework

meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305



I wasted whole day but couldn't understood, how is fork/join working here, please explain flow.Thank you.


My one regret in life is that I am not someone else.
- Woody Allen
Edward Harned
Ranch Hand

Joined: Sep 19, 2005
Posts: 291

Exactly what is your question?


Ed's latest article: A Java Parallel Calamity http://coopsoft.com/ar/Calamity2Article.html
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

i don't understand how it is working .
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

A common use case with threads is, of course, start another thread to do a portion of the work, and then use join() to wait for all the portions to finish.

Unfortunately, with a threadpool, this use case is not applicable. You can't really control when to start another thread. And you can't join() a threadpool thread because they are reused. To get around that, you have to add another task to the pool (instead of starting another thread), which requires a task to maintain the pool information. And you have to use the Future object (instead of joining with the new thread), which requires that a task tracks the future objects.

So... enter the ForkJoinPool and the ForkJoinTask. This is a task that allow more tasks to be submitted, from the task itself (via the fork() method). And does all the tracking of the future objects from the task itself (via the join() method).


In this example, if a task finds that the portion of work is too large, it will create tasks (left and right) to cut the work in half. It will then fork the left and directly do the right -- technically it can fork both sides, but then this task will do nothing but wait for the two halves -- might as well do one of the two halves directly.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

Thank you very much Henry and Edward ,You made it easier.
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305




5
1
8
8
0
9
1
8
8
3
Thread[ForkJoinPool-1-worker-1,5,main] computing: 7 to 10
Thread[ForkJoinPool-1-worker-1,5,main] computing: 5 to 7
Thread[ForkJoinPool-1-worker-2,5,main] computing: 2 to 5
Thread[ForkJoinPool-1-worker-2,5,main] computing: 0 to 2
9


Please could you explain its meaning. "Thread[ForkJoinPool-1-worker-2,5,main]"

It mentioned Thread but which thread like i know till thread-0, thread-1 etc. and what is 5 and main?
Edward Harned
Ranch Hand

Joined: Sep 19, 2005
Posts: 291

I think there is some confusion about how F/J works. So, perhaps you should start here:
Tutorial
and here:
Tutorial2
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

I read but didn't find answer of my question.I made some changes in my code.


ForkJoinPool-1-worker-1 computing: 7 to 10
ForkJoinPool-1-worker-3 computing: 0 to 2
ForkJoinPool-1-worker-4 computing: 5 to 7
ForkJoinPool-1-worker-2 computing: 2 to 5
9


I'm not getting i am not calling compute() then how i am getting result ?

I saw source code of RecursiveTask and ForkJoinTask but there is no call of this method anywhere, then how this method is being execute ?
Edward Harned
Ranch Hand

Joined: Sep 19, 2005
Posts: 291

Keep looking. Nothing happens by magic.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40


Also... keep in mind that it is not as simple as fork() to put a task on a thread which calls compute(). The algorithm is designed to best use the available threads -- so there is more than one path to get to the compute() method.

Henry
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

Henry Wong wrote:
Also... keep in mind that it is not as simple as fork() to put a task on a thread which calls compute(). The algorithm is designed to best use the available threads -- so there is more than one path to get to the compute() method.

Henry


Thank you Henry for your hint ,now if anyone asks me same question then at least i can say that forkjoin worker thread calls compute method for submitted task.
meeta gaur
Ranch Hand

Joined: Dec 05, 2012
Posts: 305

eureka!! after wasting 3 days on this topic finally i got this whole thing.Cheers

I tried some books but found not good i.e. "The Well Grounded Java Developer" and "Java.7.New.Features.Cookbook".

But "Java The Complete Reference" is , explained whole thing, those are not even explained on Oracle documentation.

Thanks to all.
 
 
subject: Fork / Join Framework