12 |
|
import java.util.concurrent.ForkJoinTask; |
13 |
|
import java.util.concurrent.ForkJoinWorkerThread; |
14 |
|
import java.util.concurrent.RecursiveAction; |
15 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
16 |
|
import java.util.concurrent.TimeUnit; |
17 |
|
import java.util.concurrent.TimeoutException; |
18 |
|
import static java.util.concurrent.TimeUnit.SECONDS; |
19 |
+ |
import java.util.Arrays; |
20 |
|
import java.util.HashSet; |
21 |
|
|
22 |
|
public class RecursiveActionTest extends JSR166TestCase { |
791 |
|
} |
792 |
|
|
793 |
|
/** |
794 |
< |
* A reinitialized task may be re-invoked |
794 |
> |
* A reinitialized normally completed task may be re-invoked |
795 |
|
*/ |
796 |
|
public void testReinitialize() { |
797 |
|
RecursiveAction a = new CheckedRecursiveAction() { |
811 |
|
} |
812 |
|
|
813 |
|
/** |
814 |
+ |
* A reinitialized abnormally completed task may be re-invoked |
815 |
+ |
*/ |
816 |
+ |
public void testReinitializeAbnormal() { |
817 |
+ |
RecursiveAction a = new CheckedRecursiveAction() { |
818 |
+ |
public void realCompute() { |
819 |
+ |
FailingFibAction f = new FailingFibAction(8); |
820 |
+ |
checkNotDone(f); |
821 |
+ |
|
822 |
+ |
for (int i = 0; i < 3; i++) { |
823 |
+ |
try { |
824 |
+ |
f.invoke(); |
825 |
+ |
shouldThrow(); |
826 |
+ |
} catch (FJException success) { |
827 |
+ |
checkCompletedAbnormally(f, success); |
828 |
+ |
} |
829 |
+ |
f.reinitialize(); |
830 |
+ |
checkNotDone(f); |
831 |
+ |
} |
832 |
+ |
}}; |
833 |
+ |
testInvokeOnPool(mainPool(), a); |
834 |
+ |
} |
835 |
+ |
|
836 |
+ |
/** |
837 |
|
* invoke task throws exception after invoking completeExceptionally |
838 |
|
*/ |
839 |
|
public void testCompleteExceptionally() { |
1197 |
|
testInvokeOnPool(asyncSingletonPool(), a); |
1198 |
|
} |
1199 |
|
|
1200 |
+ |
static class SortTask extends RecursiveAction { |
1201 |
+ |
final long[] array; final int lo, hi; |
1202 |
+ |
SortTask(long[] array, int lo, int hi) { |
1203 |
+ |
this.array = array; this.lo = lo; this.hi = hi; |
1204 |
+ |
} |
1205 |
+ |
final static int THRESHOLD = 100; |
1206 |
+ |
protected void compute() { |
1207 |
+ |
if (hi - lo < THRESHOLD) |
1208 |
+ |
sequentiallySort(array, lo, hi); |
1209 |
+ |
else { |
1210 |
+ |
int mid = (lo + hi) >>> 1; |
1211 |
+ |
invokeAll(new SortTask(array, lo, mid), |
1212 |
+ |
new SortTask(array, mid, hi)); |
1213 |
+ |
merge(array, lo, mid, hi); |
1214 |
+ |
} |
1215 |
+ |
} |
1216 |
+ |
static void sequentiallySort(long[] array, int lo, int hi) { |
1217 |
+ |
Arrays.sort(array, lo, hi); |
1218 |
+ |
} |
1219 |
+ |
static void merge(long[] array, int lo, int mid, int hi) { |
1220 |
+ |
long[] buf = Arrays.copyOfRange(array, lo, mid); |
1221 |
+ |
for (int i = 0, j = lo, k = mid; i < buf.length; j++) |
1222 |
+ |
array[j] = (k == hi || buf[i] < array[k]) ? |
1223 |
+ |
buf[i++] : array[k++]; |
1224 |
+ |
} |
1225 |
+ |
} |
1226 |
+ |
|
1227 |
+ |
/** |
1228 |
+ |
* SortTask demo works as advertised |
1229 |
+ |
*/ |
1230 |
+ |
public void testSortTaskDemo() { |
1231 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
1232 |
+ |
long[] array = new long[1007]; |
1233 |
+ |
for (int i = 0; i < array.length; i++) |
1234 |
+ |
array[i] = rnd.nextLong(); |
1235 |
+ |
long[] arrayClone = array.clone(); |
1236 |
+ |
testInvokeOnPool(mainPool(), |
1237 |
+ |
new SortTask(array, 0, array.length)); |
1238 |
+ |
Arrays.sort(arrayClone); |
1239 |
+ |
assertTrue(Arrays.equals(array, arrayClone)); |
1240 |
+ |
} |
1241 |
|
} |