Hi,
I think that this requirement is deferred. Question for the future.
(e.g) 3.5.1 and 3.5.2
Does distributability for a Fork/Join task conform to the original
work stealer algorithm even in a distributed cluster ? I may have
misunderstood the contract for a distributable.
Can one or more tasks from a 'ForkJoinPool' be distributable ? It seems to
me that the distributable requirements only address the Serializability
requirement and container process affinity both of which seem to be more
obvious than other aspects.
Correct me if I am looking too far ahead of the spec. So this code has to
be rewritten as per 3.5.1/3.5.2 ?
Thanks,
Mohan
public class ForkJoinSortTask extends RecursiveAction{
private static final long serialVersionUID = 1070860898589424509L;
long[] aux = new long[ 40 ];
private int lo;
private int hi;
long[] list = new long[ 40 ];
public ForkJoinSortTask( long[] list, int lo, int hi ){
this.list = list;
this.lo = lo;
this.hi = hi;
}
public ForkJoinSortTask() {
this.list = getArray();
this.lo = 0;
this.hi = list.length - 1;
}
private void merge( int lo,
int mid,
int hi ){
int i = lo, j = mid + 1;
int k = lo;
for( ; k <= hi ; k ++ ){
aux[ k ] = list[ k ];
}
k = lo;
while( i <= mid && j <= hi){
if( aux[i] < aux[j]){
list[k++] = aux[i++];
}else{
list[k++] = aux[j++];
}
}
while( i <= mid ){
list[k++] = aux[i++];
}
}
private long[] getArray(){
Random random = new Random();
for( int i = 0 ; i < list.length ; i ++ ){
list[ i ] = random.nextInt(1000);
}
return list;
}
private void printArray(){
for( int i = 0 ; i < list.length ; i ++ ){
System.out.printf( "%2s ", list[ i ]);
}
System.out.printf( "%n");
}
public static void main( String... argv ){
ForkJoinSortTask st = new ForkJoinSortTask();
st.printArray();
ForkJoinPool fjp = new ForkJoinPool();
fjp.invoke( st );
st.printArray();
}
@Override
protected void compute() {
if( lo < hi ){
int mid = ( lo + hi )/ 2;
invokeAll( new ForkJoinSortTask(list, lo, mid),
new ForkJoinSortTask(list, mid + 1, hi));
merge( lo, mid, hi );
}
}
}