• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Question about Multi-Threaded Application - fork/join

 
Ranch Hand
Posts: 201
1
Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Campers!

Please let me start off by wishing my fellow Java-heads a very happy Towel Day! Now, to get straight to the biscuits...

I am reading the interesting and helpful book, "Oracle Certified Professional Java SE 7 Programmer Exams 1Z0-804 and 1Z0-805 - A Comprehensive OCPJP7 Certification Guide" by Ganesh and Sharma (2013). In this book there is an interesting program that illustrates how to use some classes from the java.util.concurrent.*; package. One I am trying to understand with my limited bonobos brain has to do with the "fork/join framework". I modified the code located in the Ganesh and Sharma (2013) book, referenced above, so that I could understand what the program was doing.

My question is: What is the fork() method doing in the program? Is it recursively calling the compute() method or is it summing up the numbers without making sure the value is not too large in the first if statement in the compute() method. That if statement is: if ( (to - from) <= N/NUM_THREADS)

The full program is below. I made some formatting modifications to display number values using the US locale and also to print some more debugging information such as count and note every time compute() is called. I also count every time fork() is called and then print out the totals upon program completion to verify that in fact every time fork() is called, compute() is again called from itself as a recursive method invocation.


The Code:




Thank-you for reading this post, especially on Hitchhiker's Guide to the Galaxy Day!

Respectfully,

Tedster
 
Sheriff
Posts: 9692
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ted North wrote:My question is: What is the fork() method doing in the program? Is it recursively calling the compute() method or is it summing up the numbers without making sure the value is not too large in the first if statement in the compute() method. That if statement is: if ( (to - from) <= N/NUM_THREADS)


Hi Ted, the fork method calls the compute method asynchronously. The recursive behaviour is defined in the compute method itself...
 
Ted North
Ranch Hand
Posts: 201
1
Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:

Ted North wrote:My question is: What is the fork() method doing in the program? Is it recursively calling the compute() method or is it summing up the numbers without making sure the value is not too large in the first if statement in the compute() method. That if statement is: if ( (to - from) <= N/NUM_THREADS)


Hi Ted, the fork method calls the compute method asynchronously. The recursive behaviour is defined in the compute method itself...



Thank-you for the reply Ankit. What you are saying makes sense, that fork() calls compute() again at some later time in some different thread.

Does the fork() method always asynchronously call the compute() method in every implementation of the fork() method? I am reading the API on the fork() method and the documentation does not mention the compute() method. Here is the URL to what I am typing about: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ForkJoinTask.html#fork%28%29

Do you think there is a way to show that fork() calls compute() again someway with a System.out.println (SOP) statement?

Kind Regards,

T
 
Ankit Garg
Sheriff
Posts: 9692
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Directly the fork() method doesn't call the compute() method, fork() submits the task to the worker pool. You can check the source code

To backtrack, I think you can check ForkJoinWorkerThread.execTask method, which calls ForkJoinTask.doExec which calls RecursiveTask.exec where compute() method is called
 
Ted North
Ranch Hand
Posts: 201
1
Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:Directly the fork() method doesn't call the compute() method, fork() submits the task to the worker pool. You can check the source code

To backtrack, I think you can check ForkJoinWorkerThread.execTask method, which calls ForkJoinTask.doExec which calls RecursiveTask.exec where compute() method is called



Wow! Thank-you for the helpful reply Ankit. I can follow your logic in how compute() is called again by fork() but I am confused about how ForkJoinWorkerThread.execTask() gets called and starts the process that eventually ends with compute being called again. Does it have something to do with the pushTask() method inside the fork() method? I searched for pushTask() on DuckDuckGo and on GrepCode but could not find where this method is defined.

Thank-you for sharing GrepCode.com. That is a convenient way to see the source-code of Java itself.

Respectfully,

T
 
Ted North
Ranch Hand
Posts: 201
1
Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am still not sure how fork() ends up calling ForkJoinWorkerThread.execTask() that results in compute() being called again. I think this functionality has something to do with the mysterious pushTask() method I was able to locate at grepcode.com in the openjdk version 7 of Java here: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7u40-b43/java/util/concurrent/ForkJoinWorkerThread.java#ForkJoinWorkerThread.pushTask%28java.util.concurrent.ForkJoinTask%29

However, looking at the Oracle documentation for the class ForkJoinWorkerThread there is no mention of the strange pushTask() method. Here is the URL for the Oracle API documentation on said class: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ForkJoinWorkerThread.html

The pushTask() method does some complex bit shifting I think so it is difficult for me to understand. Does anyone have a layman's definition of what the pushTask() method does? I am thinking something along the lines of the excellent Head First Java by Sierra and Bates.

If pushTask() does not call the execTask() method what actually does? Finally, why is pushTask() shown in the Open-JDK 7 but not in the Oracle proprietary JDK?

Regards,

Ted
 
"Don't believe every tiny ad you see on the internet. But this one is rock solid." - George Washington
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic