![Page 1: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/1.jpg)
The Java Fork-Join Pool Framework
(Part 4)
Douglas C. [email protected]
www.dre.vanderbilt.edu/~schmidt
Professor of Computer Science
Institute for Software
Integrated Systems
Vanderbilt University
Nashville, Tennessee, USA
![Page 2: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/2.jpg)
2
• Understand how the Java fork-joinframework processes tasks in parallel
• Recognize the structure & functionalityof the fork-join framework
• Know how the fork-join framework is implemented internally
• Recognize the key methods in the ForkJoinPool class
Learning Objectives in this Part of the Lesson
![Page 3: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/3.jpg)
3
• Understand how the Java fork-joinframework processes tasks in parallel
• Recognize the structure & functionalityof the fork-join framework
• Know how the fork-join framework is implemented internally
• Recognize the key methods in the ForkJoinPool class
• As well as the ForkJoinTask classhierarchy
Learning Objectives in this Part of the Lesson
![Page 4: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/4.jpg)
4
Key Methods inJava ForkJoinPool
![Page 5: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/5.jpg)
5
• ForkJoinPool extends AbstractExecutorService
See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html
Key Methods in Java ForkJoinPoolclass ForkJoinPool extends
AbstractExecutorService {
...
void execute(Runnable cmd){...}
<T> Future<T> submit
(Callable<T> task){...}
<T> List<Future<T>> invokeAll
(Collection<? extends
Callable<T>> tasks){...}
<T> T invokeAny
(Collection<? extends
Callable<T>> tasks){...}
![Page 6: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/6.jpg)
6
• ForkJoinPool extends AbstractExecutorService
• It therefore implements theExecutorService methods
Key Methods in Java ForkJoinPoolclass ForkJoinPool extends
AbstractExecutorService {
...
void execute(Runnable cmd){...}
<T> Future<T> submit
(Callable<T> task){...}
<T> List<Future<T>> invokeAll
(Collection<? extends
Callable<T>> tasks){...}
<T> T invokeAny
(Collection<? extends
Callable<T>> tasks){...}
![Page 7: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/7.jpg)
7
• ForkJoinPool extends AbstractExecutorService
• It therefore implements theExecutorService methods
Key Methods in Java ForkJoinPoolclass ForkJoinPool extends
AbstractExecutorService {
...
void execute(Runnable cmd){...}
<T> Future<T> submit
(Callable<T> task){...}
<T> List<Future<T>> invokeAll
(Collection<? extends
Callable<T>> tasks){...}
<T> T invokeAny
(Collection<? extends
Callable<T>> tasks){...}
However, these methods don’t leverage the powerful fork-join pool features
![Page 8: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/8.jpg)
8
• ForkJoinPool extends AbstractExecutorService
• It therefore implements theExecutorService methods
• It also implements key methods for non-ForkJoinTask clients
Key Methods in Java ForkJoinPoolclass ForkJoinPool extends
AbstractExecutorService {
...
void execute(ForkJoinTask<T>
task)
{ ... }
T invoke(ForkJoinTask<T> task)
{ ... }
ForkJoinTask<T> submit
(ForkJoinTask<T> task)
{ ... }
![Page 9: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/9.jpg)
9
• ForkJoinPool extends AbstractExecutorService
• It therefore implements theExecutorService methods
• It also implements key methods for non-ForkJoinTask clients
Key Methods in Java ForkJoinPoolclass ForkJoinPool extends
AbstractExecutorService {
...
void execute(ForkJoinTask<T>
task)
{ ... }
T invoke(ForkJoinTask<T> task)
{ ... }
ForkJoinTask<T> submit
(ForkJoinTask<T> task)
{ ... }
These methods do leverage the powerful properties of the fork-join pool
![Page 10: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/10.jpg)
10
• ForkJoinPool extends AbstractExecutorService
• It therefore implements theExecutorService methods
• It also implements key methods for non-ForkJoinTask clients
• Arrange async exection
Key Methods in Java ForkJoinPoolclass ForkJoinPool extends
AbstractExecutorService {
...
void execute(ForkJoinTask<T>
task)
{ ... }
T invoke(ForkJoinTask<T> task)
{ ... }
ForkJoinTask<T> submit
(ForkJoinTask<T> task)
{ ... }
See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html#execute
![Page 11: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/11.jpg)
11
• ForkJoinPool extends AbstractExecutorService
• It therefore implements theExecutorService methods
• It also implements key methods for non-ForkJoinTask clients
• Arrange async exection
• Perform the task, blocking until it completes
Key Methods in Java ForkJoinPoolclass ForkJoinPool extends
AbstractExecutorService {
...
void execute(ForkJoinTask<T>
task)
{ ... }
T invoke(ForkJoinTask<T> task)
{ ... }
ForkJoinTask<T> submit
(ForkJoinTask<T> task)
{ ... }
See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html#invoke
![Page 12: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/12.jpg)
12
• ForkJoinPool extends AbstractExecutorService
• It therefore implements theExecutorService methods
• It also implements key methods for non-ForkJoinTask clients
• Arrange async exection
• Perform the task, blocking until it completes
• Submit a ForkJoinTask for execution, returns a future
Key Methods in Java ForkJoinPoolclass ForkJoinPool extends
AbstractExecutorService {
...
void execute(ForkJoinTask<T>
task)
{ ... }
T invoke(ForkJoinTask<T> task)
{ ... }
ForkJoinTask<T> submit
(ForkJoinTask<T> task)
{ ... }
See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html#submit
![Page 13: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/13.jpg)
13
• The ForkJoinPool size defaults to the # of cores available to the JVM
Key Methods in Java ForkJoinPoolclass ForkJoinPool extends
AbstractExecutorService {
public ForkJoinPool() {
this(Math.min(MAX_CAP,
Runtime.getRuntime()
.availableProcessors()),
...);
}
public ForkJoinPool
(int parallelism) {
this(parallelism, ...);
}
...
See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html#ForkJoinPool
![Page 14: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/14.jpg)
14
• The ForkJoinPool size defaults to the # of cores available to the JVM
• This size can also be controlled programmatically
class ForkJoinPool extends
AbstractExecutorService {
public ForkJoinPool() {
this(Math.min(MAX_CAP,
Runtime.getRuntime()
.availableProcessors()),
...);
}
public ForkJoinPool
(int parallelism) {
this(parallelism, ...);
}
...
Key Methods in Java ForkJoinPool
See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html#ForkJoinPool
![Page 15: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/15.jpg)
15
• The common fork-join pool can beaccessed via a static method
Key Methods in Java ForkJoinPool
See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html#commonPool
class ForkJoinPool extends
AbstractExecutorService {
...
static final ForkJoinPool
common;
public static ForkJoinPool
commonPool() {
return common;
}
![Page 16: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/16.jpg)
16
• The common fork-join pool can beaccessed via a static method
• The common pool is used by any ForkJoinTask that is not explicitly submitted to a specified pool
Key Methods in Java ForkJoinPoolclass ForkJoinPool extends
AbstractExecutorService {
...
static final ForkJoinPool
common;
public static ForkJoinPool
commonPool() {
return common;
}
![Page 17: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/17.jpg)
17See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html
• ForkJoinPool also provides various management & monitoring operations
int getParallelism() – Returns the targeted parallelism level of this pool
int getPoolSize() – Returns the number of worker threads that have started but not yet terminated
int getQueuedSubmissionCount() – Returns an estimate of the number of tasks submitted to this pool that have not yet begun executing
long getStealCount() – Returns an estimate of the total number of tasks stolen from one thread's work queue by another
Key Methods in Java ForkJoinPool
![Page 18: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/18.jpg)
18
Key Methods inJava ForkJoinTask
![Page 19: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/19.jpg)
19
• ForkJoinTask implements theFuture interface
See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinTask.html
Key Methods in Java ForkJoinTaskabstract class ForkJoinTask<V>
implements Future<V>,
Serializable {
...
![Page 20: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/20.jpg)
20
• ForkJoinTask implements theFuture interface
• fork() enables a task to createsub-tasks that run in parallel
See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinTask.html#fork
Key Methods in Java ForkJoinTaskabstract class ForkJoinTask<V>
implements Future<V>,
Serializable {
...
final ForkJoinTask<V> fork()
{ ... }
final V join() { ... }
final V invoke() { ... }
![Page 21: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/21.jpg)
21
• ForkJoinTask implements theFuture interface
• fork() enables a task to createsub-tasks that run in parallel
• Asynchronously execute this task in the current task’s poolor ForkJoinPool.commonPool()
Key Methods in Java ForkJoinTaskabstract class ForkJoinTask<V>
implements Future<V>,
Serializable {
...
final ForkJoinTask<V> fork()
{ ... }
final V join() { ... }
final V invoke() { ... }
![Page 22: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/22.jpg)
22
• ForkJoinTask implements theFuture interface
• fork() enables a task to createsub-tasks that run in parallel
• Asynchronously execute this task in the current task’s poolor ForkJoinPool.commonPool()
• Pushes the task on the head of the deque owned by the current worker thread
Key Methods in Java ForkJoinTask
Sub-Task1.1
Sub-Task1.2
Sub-Task1.3 Sub-Task3.3
Sub-Task3.4Sub-Task2.4
WorkQueue WorkQueue WorkQueue
Sub-Task1.4
2. push()1. fork()
![Page 23: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/23.jpg)
23
• ForkJoinTask implements theFuture interface
• fork() enables a task to createsub-tasks that run in parallel
• join() returns the result of a computation when it’s done
See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinTask.html#join
Key Methods in Java ForkJoinTaskabstract class ForkJoinTask<V>
implements Future<V>,
Serializable {
...
final ForkJoinTask<V> fork()
{ ... }
final V join() { ... }
final V invoke() { ... }
![Page 24: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/24.jpg)
24
• ForkJoinTask implements theFuture interface
• fork() enables a task to createsub-tasks that run in parallel
• join() returns the result of a computation when it’s done
• It “blocks” the calling taskuntil forked sub-task is done
Key Methods in Java ForkJoinTaskabstract class ForkJoinTask<V>
implements Future<V>,
Serializable {
...
final ForkJoinTask<V> fork()
{ ... }
final V join() { ... }
final V invoke() { ... }
![Page 25: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/25.jpg)
25
• ForkJoinTask implements theFuture interface
• fork() enables a task to createsub-tasks that run in parallel
• join() returns the result of a computation when it’s done
• It “blocks” the calling taskuntil forked sub-task is done
• Defines a synchronization point
• Ensures all writes in a worker thread that “happen-before” join() are made visible to other threads after the join()
Key Methods in Java ForkJoinTaskabstract class ForkJoinTask<V>
implements Future<V>,
Serializable {
...
final ForkJoinTask<V> fork()
{ ... }
final V join() { ... }
final V invoke() { ... }
See stackoverflow.com/questions/4800503/memory-visibility-in-fork-join
![Page 26: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/26.jpg)
26
• ForkJoinTask implements theFuture interface
• fork() enables a task to createsub-tasks that run in parallel
• join() returns the result of a computation when it’s done
• invoke() performs this task, awaits its completion if needed, & returns its result
See docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinTask.html#invoke
Key Methods in Java ForkJoinTaskabstract class ForkJoinTask<V>
implements Future<V>,
Serializable {
...
final ForkJoinTask<V> fork()
{ ... }
final V join() { ... }
final V invoke() { ... }
![Page 27: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/27.jpg)
27
• ForkJoinTask implements theFuture interface
• fork() enables a task to createsub-tasks that run in parallel
• join() returns the result of a computation when it’s done
• invoke() performs this task, awaits its completion if needed, & returns its result
• Throws RuntimeException or Errorif the underlying computation did so
Key Methods in Java ForkJoinTaskabstract class ForkJoinTask<V>
implements Future<V>,
Serializable {
...
final ForkJoinTask<V> fork()
{ ... }
final V join() { ... }
final V invoke() { ... }
![Page 28: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/28.jpg)
28
Key Methods in the Java RecursiveAction
![Page 29: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/29.jpg)
29
• RecursiveAction extends theForkJoinTask superclass & does not return a result
See docs.oracle.com/javase/8/docs/api/java/util/concurrent/RecursiveAction.html
Key Methods in Java RecursiveActionabstract class RecursiveAction
extends ForkJoinTask<Void> {
...
![Page 30: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/30.jpg)
30
• RecursiveTask extends ForkJoinTaskto return a result
• compute() must be overriddenby subclasses to perform thetask’s main computation
abstract class RecursiveAction
extends ForkJoinTask<Void> {
protected abstract Void
compute();
...
See docs.oracle.com/javase/8/docs/api/java/util/concurrent/RecursiveTask.html#compute
Key Methods in Java RecursiveAction
![Page 31: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/31.jpg)
31
• RecursiveTask extends ForkJoinTaskto return a result
• compute() must be overriddenby subclasses to perform thetask’s main computation
• It may split its work up into smaller sub-tasks that are fork()’d to run in parallel
abstract class RecursiveAction
extends ForkJoinTask<Void> {
protected abstract Void
compute();
...
Key Methods in Java RecursiveAction
![Page 32: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/32.jpg)
32
• RecursiveTask extends ForkJoinTaskto return a result
• compute() must be overriddenby subclasses to perform thetask’s main computation
• It may split its work up into smaller sub-tasks that are fork()’d to run in parallel
• It join()’s these smaller sub-tasks but does not return aresult directly
• Result’s often stored in array
abstract class RecursiveAction
extends ForkJoinTask<Void> {
protected abstract Void
compute();
...
Key Methods in Java RecursiveAction
![Page 33: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/33.jpg)
33
• RecursiveTask extends ForkJoinTaskto return a result
• compute() must be overriddenby subclasses to perform thetask’s main computation
• Called internally by the fork-join pool to execute the task
abstract class RecursiveAction
extends ForkJoinTask<Void> {
protected abstract Void
compute();
protected final boolean exec(){
compute();
return true;
}
...
See docs.oracle.com/javase/8/docs/api/java/util/concurrent/RecursiveTask.html#exec
Key Methods in Java RecursiveAction
![Page 34: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/34.jpg)
34
Key Methods in the Java RecursiveTask
![Page 35: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/35.jpg)
35
• RecursiveTask extends ForkJoinTaskto return a result
See docs.oracle.com/javase/8/docs/api/java/util/concurrent/RecursiveTask.html
Key Methods in Java RecursiveTaskabstract class RecursiveTask<V>
extends ForkJoinTask<V> {
...
![Page 36: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/36.jpg)
36
• RecursiveTask extends ForkJoinTaskto return a result
• compute() must be overriddenby subclasses to perform thetask’s main computation
abstract class RecursiveTask<V>
extends ForkJoinTask<V> {
protected abstract V
compute();
...
See docs.oracle.com/javase/8/docs/api/java/util/concurrent/RecursiveTask.html#compute
Key Methods in Java RecursiveTask
![Page 37: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/37.jpg)
37
• RecursiveTask extends ForkJoinTaskto return a result
• compute() must be overriddenby subclasses to perform thetask’s main computation
• It may split its work up into smaller sub-tasks that are fork()’d to run in parallel
abstract class RecursiveTask<V>
extends ForkJoinTask<V> {
protected abstract V
compute();
...
Key Methods in Java RecursiveTask
![Page 38: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/38.jpg)
38
• RecursiveTask extends ForkJoinTaskto return a result
• compute() must be overriddenby subclasses to perform thetask’s main computation
• It may split its work up into smaller sub-tasks that are fork()’d to run in parallel
• It join()’s the results of these smaller sub-tasks into a collective result
abstract class RecursiveTask<V>
extends ForkJoinTask<V> {
protected abstract V
compute();
...
Key Methods in Java RecursiveTask
![Page 39: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/39.jpg)
39
• RecursiveTask extends ForkJoinTaskto return a result
• compute() must be overriddenby subclasses to perform thetask’s main computation
• Called internally by the fork-join pool to execute the task
abstract class RecursiveTask<V>
extends ForkJoinTask<V> {
protected abstract V
compute();
V result;
protected final boolean exec(){
result = compute();
return true;
}
...
See docs.oracle.com/javase/8/docs/api/java/util/concurrent/RecursiveTask.html#exec
Key Methods in Java RecursiveTask
![Page 40: The Java Fork-Join Pool Framework (Part 4)10 •ForkJoinPool extends Abstract ExecutorService •It therefore implements the ExecutorService methods •It also implements key methods](https://reader034.vdocuments.mx/reader034/viewer/2022042211/5eb1778a44cd846a506d23d4/html5/thumbnails/40.jpg)
40
End of the Java Fork-Join
Pool Framework (Part 4)