Rec 09
Problems
1. ForkJoinPool Example
When a current workder hits
task.join(), the current worker will become idle.
2. Difference between fork(), join() and compute()
fork(), join() and compute()
task.fork()will actually lettaskto be completed by another worker.
f1.fork();
int a = f2.compute();
int b = f1.join();
return a + b;The task forks f1 for another worker to complete, while completing f2 by itself in the meantime.
f1.fork();
int a = f1.join();
int b = f2.compute();
return a + b;The task forks f1 for another worker to complete, and waits for it to complete first, before completing f2 by itself. There is no parallelism in this version.
int a = f1.compute();
int b = f2.compute();
return a + b;This task completes both subtasks f1 and f2 by itself, sequentially.
f1.fork();
f2.fork();
int a = f2.join();
int b = f1.join();
return a + b;This task forks both subtasks f1 and f2 to be run in parallel. Then waits for the subtasks to
complete.
Same as the Option 4. But there is a "crossing" and is less efficient.
Last updated