• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Fork / Join Framework

 
Ranch Hand
Posts: 305
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I wasted whole day but couldn't understood, how is fork/join working here, please explain flow.Thank you.
 
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly what is your question?
 
meeta gaur
Ranch Hand
Posts: 305
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i don't understand how it is working .
 
author
Posts: 23887
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
meeta gaur
Ranch Hand
Posts: 305
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Henry and Edward ,You made it easier.
 
meeta gaur
Ranch Hand
Posts: 305
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 305
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 291
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keep looking. Nothing happens by magic.
 
Henry Wong
author
Posts: 23887
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 305
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 305
Tomcat Server Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ruth Stout was famous for gardening naked. Just like this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic