key methods in the java recursive action & recursivetask
TRANSCRIPT
![Page 1: Key Methods in the Java Recursive Action & RecursiveTask](https://reader031.vdocuments.site/reader031/viewer/2022012011/61d9eff4300944357e4c59c7/html5/thumbnails/1.jpg)
Key Methods in the Java Recursive
Action & RecursiveTask Subclasses
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: Key Methods in the Java Recursive Action & RecursiveTask](https://reader031.vdocuments.site/reader031/viewer/2022012011/61d9eff4300944357e4c59c7/html5/thumbnails/2.jpg)
2
• Recognize the key methods in the ForkJoinPool class
• Recognize the key methods in the ForkJoinTask class
• Recognize the key methods in the RecursiveAction & RecursiveTaskclasses
Learning Objectives in this Part of the Lesson
![Page 3: Key Methods in the Java Recursive Action & RecursiveTask](https://reader031.vdocuments.site/reader031/viewer/2022012011/61d9eff4300944357e4c59c7/html5/thumbnails/3.jpg)
3
Key Methods in the Java RecursiveAction
![Page 4: Key Methods in the Java Recursive Action & RecursiveTask](https://reader031.vdocuments.site/reader031/viewer/2022012011/61d9eff4300944357e4c59c7/html5/thumbnails/4.jpg)
4
• RecursiveAction extends ForkJoinTask & 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 5: Key Methods in the Java Recursive Action & RecursiveTask](https://reader031.vdocuments.site/reader031/viewer/2022012011/61d9eff4300944357e4c59c7/html5/thumbnails/5.jpg)
5
• RecursiveAction extends ForkJoinTask & does not return a result
• Subclasses override compute() to perform task’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 6: Key Methods in the Java Recursive Action & RecursiveTask](https://reader031.vdocuments.site/reader031/viewer/2022012011/61d9eff4300944357e4c59c7/html5/thumbnails/6.jpg)
6
• RecursiveAction extends ForkJoinTask & does not return a result
• Subclasses override compute() to perform task’s main computation
• If data size is below a certain threshold perform work directly
abstract class RecursiveAction
extends ForkJoinTask<Void> {
protected abstract Void
compute();
...
Key Methods in Java RecursiveAction
![Page 7: Key Methods in the Java Recursive Action & RecursiveTask](https://reader031.vdocuments.site/reader031/viewer/2022012011/61d9eff4300944357e4c59c7/html5/thumbnails/7.jpg)
7
• RecursiveAction extends ForkJoinTask & does not return a result
• Subclasses override compute() to perform task’s main computation
• If data size is below a certain threshold perform work directly
• If data size is large, split work 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 8: Key Methods in the Java Recursive Action & RecursiveTask](https://reader031.vdocuments.site/reader031/viewer/2022012011/61d9eff4300944357e4c59c7/html5/thumbnails/8.jpg)
8
• RecursiveAction extends ForkJoinTask & does not return a result
• Subclasses override compute() to perform task’s main computation
• If data size is below a certain threshold perform work directly
• If data size is large, split work into smaller sub-tasks that are fork()’d to run in parallel
• These smaller sub-tasks are join()’d,but a result is not returned directly
• e.g., results may be stored in an array
abstract class RecursiveAction
extends ForkJoinTask<Void> {
protected abstract Void
compute();
...
Key Methods in Java RecursiveAction
![Page 9: Key Methods in the Java Recursive Action & RecursiveTask](https://reader031.vdocuments.site/reader031/viewer/2022012011/61d9eff4300944357e4c59c7/html5/thumbnails/9.jpg)
9
• RecursiveAction extends ForkJoinTask & does not return a result
• Subclasses override compute() to perform task’s main computation
• The fork-join framework callsexec() 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
exec() is an abstract method in ForkJoinTask that’s overridden in RecursiveAction
![Page 10: Key Methods in the Java Recursive Action & RecursiveTask](https://reader031.vdocuments.site/reader031/viewer/2022012011/61d9eff4300944357e4c59c7/html5/thumbnails/10.jpg)
10
• RecursiveAction extends ForkJoinTask & does not return a result
• Subclasses override compute() to perform task’s main computation
• The fork-join framework callsexec() to execute the task
abstract class RecursiveAction
extends ForkJoinTask<Void> {
protected abstract Void
compute();
protected final boolean exec(){
compute();
return true;
}
...
See en.wikipedia.org/wiki/Template_method_pattern
Key Methods in Java RecursiveAction
exec() is a template method & compute() is a hook method
![Page 11: Key Methods in the Java Recursive Action & RecursiveTask](https://reader031.vdocuments.site/reader031/viewer/2022012011/61d9eff4300944357e4c59c7/html5/thumbnails/11.jpg)
11
• RecursiveAction extends ForkJoinTask & does not return a result
• Subclasses override compute() to perform task’s main computation
• The fork-join framework callsexec() to execute the task
abstract class RecursiveAction
extends ForkJoinTask<Void> {
protected abstract Void
compute();
protected final boolean exec(){
compute();
return true;
}
...
Key Methods in Java RecursiveAction
The result of compute() is not stored for subsequent access
![Page 12: Key Methods in the Java Recursive Action & RecursiveTask](https://reader031.vdocuments.site/reader031/viewer/2022012011/61d9eff4300944357e4c59c7/html5/thumbnails/12.jpg)
12
Key Methods in the Java RecursiveTask
![Page 13: Key Methods in the Java Recursive Action & RecursiveTask](https://reader031.vdocuments.site/reader031/viewer/2022012011/61d9eff4300944357e4c59c7/html5/thumbnails/13.jpg)
13
• 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 14: Key Methods in the Java Recursive Action & RecursiveTask](https://reader031.vdocuments.site/reader031/viewer/2022012011/61d9eff4300944357e4c59c7/html5/thumbnails/14.jpg)
14
• RecursiveTask extends ForkJoinTaskto return a result
• Subclasses override compute() to perform task’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 15: Key Methods in the Java Recursive Action & RecursiveTask](https://reader031.vdocuments.site/reader031/viewer/2022012011/61d9eff4300944357e4c59c7/html5/thumbnails/15.jpg)
15
• RecursiveTask extends ForkJoinTaskto return a result
• Subclasses override compute() to perform task’s main computation
• If data size is below a certain threshold perform work directly
abstract class RecursiveTask<V>
extends ForkJoinTask<V> {
protected abstract V
compute();
...
Key Methods in Java RecursiveTask
![Page 16: Key Methods in the Java Recursive Action & RecursiveTask](https://reader031.vdocuments.site/reader031/viewer/2022012011/61d9eff4300944357e4c59c7/html5/thumbnails/16.jpg)
16
• RecursiveTask extends ForkJoinTaskto return a result
• Subclasses override compute() to perform task’s main computation
• If data size is below a certain threshold perform work directly
• If data size is large, split work 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 17: Key Methods in the Java Recursive Action & RecursiveTask](https://reader031.vdocuments.site/reader031/viewer/2022012011/61d9eff4300944357e4c59c7/html5/thumbnails/17.jpg)
17
• RecursiveTask extends ForkJoinTaskto return a result
• Subclasses override compute() to perform task’s main computation
• If data size is below a certain threshold perform work directly
• If data size is large, split work into smaller sub-tasks that are fork()’d to run in parallel
• Results of these smaller sub-tasks are join()’d into a merged result
abstract class RecursiveTask<V>
extends ForkJoinTask<V> {
protected abstract V
compute();
...
Key Methods in Java RecursiveTask
![Page 18: Key Methods in the Java Recursive Action & RecursiveTask](https://reader031.vdocuments.site/reader031/viewer/2022012011/61d9eff4300944357e4c59c7/html5/thumbnails/18.jpg)
18
• RecursiveTask extends ForkJoinTaskto return a result
• Subclasses override compute() to perform task’s main computation
• The fork-join framework callsexec() 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
exec() is an abstract (template) method in ForkJoinTask that’s overridden in RecursiveTask
![Page 19: Key Methods in the Java Recursive Action & RecursiveTask](https://reader031.vdocuments.site/reader031/viewer/2022012011/61d9eff4300944357e4c59c7/html5/thumbnails/19.jpg)
19
• RecursiveTask extends ForkJoinTaskto return a result
• Subclasses override compute() to perform task’s main computation
• The fork-join framework callsexec() 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;
}
...
Key Methods in Java RecursiveTask
The result of compute() is stored for subsequent access
![Page 20: Key Methods in the Java Recursive Action & RecursiveTask](https://reader031.vdocuments.site/reader031/viewer/2022012011/61d9eff4300944357e4c59c7/html5/thumbnails/20.jpg)
20
End of Key Methods in the Java RecursiveAction
& RecursiveTask Subclasses