eh3rrera / ocpj8-book

Study guide for the Oracle Certified Professional, Java SE 8 Programmer Exam (1Z0-809)
Other
131 stars 91 forks source link

Chapter 28 confirmation #88

Open VishGov opened 4 years ago

VishGov commented 4 years ago

Hi Esteban,

In chapter 28 for the RecursiveTask example code,

FindMinimumAction firstSubtask = new FindMinimumAction(data, start, mid); FindMinimumAction secondSubtask = new FindMinimumAction(data, mid, end); // queue the first task firstSubtask.fork(); // Return the miminum of all subtasks return Math.min(firstSubtask.compute(), secondSubtask.join());

Could you elaborate why we are forking AND also computing the first task, and then joining with the second?

From the notes, I thought that its generally the first is forked, the second computed and we wait on the result of the forked first task( which is usually "stolen" by another worker thread)

Or does it not really matter?

Thanks and Regards, Vishnu

eh3rrera commented 4 years ago

Hi VishGov. You're right, the recommended way is to join the first task, I've changed the example. The text and the other examples also reflect this. However, in practice, most of the time it doesn't matter. Parallelism is a bit tricky to get it right, it depends on the complexity of the task and the number of cores of the computer executing the code.

Some links: https://stackoverflow.com/questions/30242516/in-javas-forkjointask-does-the-order-of-fork-join-matter https://stackoverflow.com/questions/50757332/forkjointask-order-of-join-ing

Thanks!

VishGov commented 4 years ago

Thanks for the StackOverflow references Esteban!

On Mon, 4 Nov 2019, 2:45 am Esteban Herrera, notifications@github.com wrote:

Hi VishGov. You're right, the recommended way is to join the first task, I've changed the example. The text and the other examples also reflect this. However, in practice, most of the time it doesn't matter. Parallelism is a bit tricky to get it right, it depends on the complexity of the task and the number of cores of the computer executing the code.

Some links:

https://stackoverflow.com/questions/30242516/in-javas-forkjointask-does-the-order-of-fork-join-matter https://stackoverflow.com/questions/50757332/forkjointask-order-of-join-ing

Thanks!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/eh3rrera/ocpj8-book/issues/88?email_source=notifications&email_token=ADFQTOHWZZNVBPVJREI3GD3QR45P7A5CNFSM4JIKYAGKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEC5476Q#issuecomment-549179386, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADFQTOAFU6JUS2MV34PB57DQR45P7ANCNFSM4JIKYAGA .