ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166y/RecursiveTask.java
Revision: 1.2
Committed: Mon Jul 20 21:45:06 2009 UTC (14 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.1: +2 -2 lines
Log Message:
whitespace

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     * <pre>
14     * class Fibonacci extends RecursiveTask&lt;Integer&gt; {
15     * final int n;
16     * Fibonnaci(int n) { this.n = n; }
17     * Integer compute() {
18     * if (n &lt;= 1)
19     * 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     * }
26     * </pre>
27     *
28     * However, besides being a dumb way to compute Fibonacci functions
29     * (there is a simple fast linear algorithm that you'd use in
30     * practice), this is likely to perform poorly because the smallest
31     * subtasks are too small to be worthwhile splitting up. Instead, as
32     * is the case for nearly all fork/join applications, you'd pick some
33     * minimum granularity size (for example 10 here) for which you always
34 jsr166 1.2 * sequentially solve rather than subdividing.
35 dl 1.1 *
36     */
37     public abstract class RecursiveTask<V> extends ForkJoinTask<V> {
38    
39     /**
40     * Empty contructor for use by subclasses.
41     */
42     protected RecursiveTask() {
43     }
44    
45     /**
46     * The result returned by compute method.
47     */
48     V result;
49    
50     /**
51 jsr166 1.2 * The main computation performed by this task.
52 dl 1.1 */
53     protected abstract V compute();
54    
55     public final V getRawResult() {
56     return result;
57     }
58    
59     protected final void setRawResult(V value) {
60     result = value;
61     }
62    
63     /**
64     * Implements execution conventions for RecursiveTask
65     */
66     protected final boolean exec() {
67     result = compute();
68     return true;
69     }
70    
71     }