ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166y/RecursiveTask.java
Revision: 1.5
Committed: Wed Jul 22 01:36:51 2009 UTC (14 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.4: +2 -0 lines
Log Message:
Add @since, @author tags

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain, as explained at
4     * http://creativecommons.org/licenses/publicdomain
5     */
6    
7     package jsr166y;
8    
9     /**
10     * Recursive result-bearing ForkJoinTasks.
11     * <p> For a classic example, here is a task computing Fibonacci numbers:
12     *
13 jsr166 1.4 * <pre> {@code
14     * class Fibonacci extends RecursiveTask<Integer> {
15 dl 1.1 * final int n;
16 jsr166 1.3 * Fibonacci(int n) { this.n = n; }
17 dl 1.1 * Integer compute() {
18 jsr166 1.4 * if (n <= 1)
19 dl 1.1 * return n;
20     * Fibonacci f1 = new Fibonacci(n - 1);
21     * f1.fork();
22     * Fibonacci f2 = new Fibonacci(n - 2);
23     * return f2.compute() + f1.join();
24     * }
25 jsr166 1.4 * }}</pre>
26 dl 1.1 *
27     * However, besides being a dumb way to compute Fibonacci functions
28     * (there is a simple fast linear algorithm that you'd use in
29     * practice), this is likely to perform poorly because the smallest
30     * subtasks are too small to be worthwhile splitting up. Instead, as
31     * is the case for nearly all fork/join applications, you'd pick some
32     * minimum granularity size (for example 10 here) for which you always
33 jsr166 1.2 * sequentially solve rather than subdividing.
34 dl 1.1 *
35 jsr166 1.5 * @since 1.7
36     * @author Doug Lea
37 dl 1.1 */
38     public abstract class RecursiveTask<V> extends ForkJoinTask<V> {
39    
40     /**
41 jsr166 1.3 * Empty constructor for use by subclasses.
42 dl 1.1 */
43     protected RecursiveTask() {
44     }
45    
46     /**
47     * The result returned by compute method.
48     */
49     V result;
50    
51     /**
52 jsr166 1.2 * The main computation performed by this task.
53 dl 1.1 */
54     protected abstract V compute();
55    
56     public final V getRawResult() {
57     return result;
58     }
59    
60     protected final void setRawResult(V value) {
61     result = value;
62     }
63    
64     /**
65     * Implements execution conventions for RecursiveTask
66     */
67     protected final boolean exec() {
68     result = compute();
69     return true;
70     }
71    
72     }