Java Reference
In-Depth Information
There are two classes: RecursiveTask and RecursiveAction . The main difference is that
RecursiveTask has each step of the work returning a value, whereas RecursiveAction
does not. In other words, the RecursiveAction method compute() has a return type of
void, whereas the RecursiveAction method of the same name has a return type of T, some
Type Parameter. You might use RecursiveTask when each call returns a value that repres-
ents the computation for its subset of the overall task, in other words, to divide a problem
like summarizing data—each task would summarize one part and return that. You might use
RecursiveAction to operate over a large data structure performing some transform of the
data in place.
There are two demos of the Fork/Join framework here, named after the ForkJoinTask that
each subclasses:
RecursiveTaskDemo uses fork() and join() directly.
RecursiveActionDemo uses invokeAll() to invoke the two subtasks. invoke() is just
a fork() and a join() ; and invokeAll() just does this repeatedly until done. Compare
the versions of compute() in Examples 22-15 and 22-16 and this will make sense.
Example 22-15. RecursiveActionDemo.java
/** A trivial demonstration of the "Fork-Join" framework:
* square a bunch of numbers using RecursiveAction.
* We use RecursiveAction here b/c we don't need each
* compute() call to return its result; the work is
* accumulated in the "dest" array.
* @author Ian Darwin
*/
public
public class
class RecursiveActionDemo
RecursiveActionDemo extends
extends RecursiveAction {
private
private static
static final
final long
long serialVersionUID = 3742774374013520116L ;
static
static int
int [] raw = {
19 , 3 , 0 , - 1 , 57 , 24 , 65 , Integer . MAX_VALUE , 42 , 0 , 3 , 5
};
static
static int
int [] sorted = null
null ;
int
int [] source ;
int
int [] dest ;
int
int length ;
int
int start ;
final
final static
static int
int THRESHOLD = 4 ;
Search WWH ::




Custom Search