Java Reference
In-Depth Information
public
public static
void main ( String [] args ) {
sorted = new
static void
int [ raw . length ];
RecursiveActionDemo fb =
new
new int
new RecursiveActionDemo ( raw , 0 , raw . length , sorted );
ForkJoinPool pool = new
new ForkJoinPool ();
pool . invoke ( fb );
System . out . print ( '[' );
for
for ( int
int i : sorted ) {
System . out . print ( i + "," );
}
System . out . println ( ']' );
}
public
public RecursiveActionDemo ( int
int [] src , int
int start , int
int length , int
int [] dest ) {
this
this . source = src ;
this
this . start = start ;
this
this . length = length ;
this
this . dest = dest ;
}
@Override
protected
protected void
void compute () {
System . out . println ( "ForkJoinDemo.compute()" );
iif ( length <= THRESHOLD ) { // Compute Directly
for
for ( int
int i = start ; i < start + length ; i ++) {
dest [ i ] = source [ i ] * source [ i ];
}
} else
else {
// Divide and Conquer
int
int split = length / 2 ;
invokeAll (
new
new RecursiveActionDemo ( source , start ,
split ,
dest ),
new
new RecursiveActionDemo ( source , start + split , length - split , dest ));
}
}
}
Example 22-16. RecursiveTaskDemo.java
/**
* Demonstrate the Fork-Join Framework to average a large array.
* Running this on a multi-core machine as e.g.,
* $ time java threads.RecursiveTaskDemo
* shows that the CPU time is always greater than the elapsed time,
* indicating that we are making use of multiple cores.
* That said, it is a somewhat contrived demo.
*
Search WWH ::




Custom Search