下面是我的Fork-join示例代码。它应该计算它正确执行的数组中的最大数量。但是在代码中我无法看到线程从彼此窃取作业。字数是0大部分时代。出了什么问题?
package MultiThreading;
import java.util.*;
import java.util.concurrent.*;
public class MaxValueUsingForkJoin {
public static void main(String[] args) throws ExecutionException, InterruptedException {
Random random = new Random();
int arraysize = 500;
int[] arr = new int[arraysize];
System.out.println("arraysize : " + arraysize);
for (int i = 0; i < arraysize; i++) {
arr[i] = random.nextInt(50000);
}
System.out.println(Arrays.toString(arr));
Task_MaxValueF task = new Task_MaxValueF(arr);
ForkJoinPool forkJoinPool = ForkJoinPool.commonPool();
forkJoinPool.execute(task);
do {
System.out.printf("******************************************\n");
System.out.printf("Main: Parallelism: %d\n", forkJoinPool.getParallelism());
System.out.printf("Main: Active Threads: %d\n", forkJoinPool.getActiveThreadCount());
System.out.printf("Main: Task Count: %d\n", forkJoinPool.getQueuedTaskCount());
System.out.printf("Main: Steal Count: %d\n", forkJoinPool.getStealCount());
System.out.printf("******************************************\n");
try {
TimeUnit.MILLISECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (!task.isDone());
forkJoinPool.shutdown();
int result = task.join();
System.out.println("Highest Number :" + result);
}
}
以下是递归任务的实现
class Task_MaxValueF extends RecursiveTask<Integer> {
int[] array;
final int THRESHOLD = 10;
Task_MaxValueF(int[] array) {
this.array = array;
}
@Override
protected Integer compute() {
if (array.length > THRESHOLD) {
return ForkJoinTask.invokeAll(CreateSubTasks()).
stream().
mapToInt(completedTask -> completedTask.join()
).max().getAsInt();
} else {
if (array.length > 0)
return processing(array);
else
return -1;
}
}
List<Task_MaxValueF> CreateSubTasks() {
Task_MaxValueF task1 = new Task_MaxValueF(Arrays.copyOfRange(array, 0, THRESHOLD));
Task_MaxValueF task2 = new Task_MaxValueF(Arrays.copyOfRange(array, THRESHOLD, array.length));
List<Task_MaxValueF> list = new ArrayList<>();
list.add(task1);
list.add(task2);
return list;
}
private Integer processing(int[] arr) {
//System.out.println(Thread.currentThread().getName() + " is Processing " + Arrays.toString(arr));
// OptionalInt optionalInt = Arrays.stream(arr).max();
int max = 0;
for (int i : arr) {
if (i > max)
max = i;
}
return max;
}
}
输出:
arraysize : 500
[11079, 44714, 48871, 31643, 18782, 43247, 27394, 18218, 10496, 16913, 21932, 12119, 39829, 49303, 3584, 38235, 27041, 16614, 39585, 34167, 48994, 15103, 19147, 1632, 6229, 47797, 28629, 1911, 30696, 32825, 13986, 6212, 9290, 7092, 11926, 41903, 20527, 4384, 43814, 22090, 37184, 47395, 45544, 12478, 25359, 36682, 39290, 10865, 10423, 15070, 29118, 22049, 26834, 9141, 29155, 31248, 39237, 8519, 27519, 16555, 36198, 45867, 47928, 2088, 15726, 16126, 27954, 7052, 43609, 46032, 26594, 48775, 11449, 13659, 12001, 4781, 30123, 5824, 31629, 13233, 40369, 48208, 15177, 10944, 28270, 67, 46949, 22199, 13425, 49305, 25688, 12305, 3337, 37019, 4761, 13265, 28838, 44413, 43358, 22681, 45296, 10690, 15723, 31365, 11217, 17004, 30012, 24615, 20621, 23551, 48702, 40800, 46818, 31008, 4263, 29303, 2714, 33695, 8438, 49417, 43693, 4830, 9765, 35741, 10036, 6093, 12555, 24274, 43048, 25028, 40814, 36328, 15753, 47260, 29125, 43080, 24219, 45370, 9628, 22113, 10714, 12153, 37057, 37968, 11326, 18483, 21915, 14619, 6553, 21787, 18874, 17866, 25037, 10058, 13769, 834, 5532, 49541, 3872, 10383, 35495, 14663, 39414, 6238, 32343, 30000, 27364, 32805, 15498, 32634, 31461, 25567, 12741, 1379, 35289, 26215, 27801, 47276, 16153, 38960, 10112, 36797, 28932, 10126, 12518, 47714, 26648, 47925, 42884, 8615, 21053, 2573, 15143, 30283, 4155, 49886, 20111, 24564, 29004, 29294, 4356, 8959, 26107, 6692, 2291, 34641, 23755, 38856, 40839, 320, 41173, 10601, 16290, 37159, 42106, 36138, 32522, 25018, 27159, 9850, 10513, 25366, 29462, 35726, 18508, 43267, 2816, 29774, 33221, 778, 2096, 30089, 47276, 11069, 2771, 39365, 35608, 34791, 36812, 3335, 38075, 22210, 24756, 33835, 6612, 48622, 23854, 24097, 2686, 35028, 10872, 7860, 39174, 31960, 28638, 28776, 37866, 35444, 6777, 11695, 29996, 38727, 21733, 23560, 28406, 4649, 7280, 45542, 30647, 26009, 41981, 33928, 8507, 23990, 1465, 45818, 13417, 39654, 11253, 49991, 407, 43478, 30065, 21992, 41608, 27695, 5066, 31710, 43253, 24241, 5579, 42040, 32441, 5487, 38319, 14849, 25205, 14775, 30246, 33706, 18069, 28779, 33541, 6559, 23066, 9729, 46856, 25113, 19462, 38419, 7107, 5456, 20823, 12100, 21958, 10410, 11274, 6640, 30958, 3588, 3297, 34319, 30824, 14859, 31247, 35846, 43665, 343, 29759, 2121, 24490, 40854, 40469, 29217, 41143, 22017, 24556, 2246, 41941, 13757, 2847, 10185, 22667, 19680, 3523, 49329, 2666, 365, 4705, 43104, 39246, 8366, 20914, 41153, 45719, 41613, 41635, 19216, 16049, 30639, 44311, 30853, 43678, 17949, 36012, 24319, 36502, 38705, 29157, 12649, 23253, 42777, 39223, 9004, 6706, 41643, 28592, 28321, 15856, 28134, 32001, 22993, 27294, 9024, 44876, 24619, 9353, 27041, 2792, 41473, 13089, 1915, 20833, 47695, 21048, 34325, 3005, 1304, 24311, 27442, 19049, 8079, 13163, 38531, 34866, 39824, 41437, 38038, 16912, 15049, 46175, 19752, 12701, 35848, 5057, 43022, 963, 11826, 41220, 15004, 30978, 45564, 8054, 41457, 17265, 37274, 1100, 27439, 36301, 33138, 10142, 8378, 28365, 37724, 36747, 48845, 17973, 12168, 19117, 18819, 34409, 33836, 21016, 17896, 9929, 46409, 36428, 34429, 14395, 29945, 7642, 24098, 31639, 20951, 26820, 16867, 4395, 22298, 47852, 18754, 6271, 38959, 7294, 48317, 38499, 26227, 16074, 16331, 40692, 37898, 42121, 48568, 12173, 40143, 43425, 16858, 10730, 6728, 9619, 33600, 16312, 16532, 28562, 34054, 30585, 21556, 10958, 23079, 36579, 22708, 38782, 28736, 47032, 42453, 34157, 38269, 32160, 39832, 4811, 4210]
******************************************
Main: Parallelism: 1
Main: Active Threads: 1
Main: Task Count: 0
Main: Steal Count: 0
******************************************
******************************************
Main: Parallelism: 1
Main: Active Threads: 1
Main: Task Count: 0
Main: Steal Count: 0
******************************************
******************************************
Main: Parallelism: 1
Main: Active Threads: 1
Main: Task Count: 0
Main: Steal Count: 0
******************************************
******************************************
Main: Parallelism: 1
Main: Active Threads: 1
Main: Task Count: 0
Main: Steal Count: 0
******************************************
******************************************
Main: Parallelism: 1
Main: Active Threads: 1
Main: Task Count: 0
Main: Steal Count: 0
******************************************
******************************************
Main: Parallelism: 1
Main: Active Threads: 1
Main: Task Count: 0
Main: Steal Count: 0
******************************************
******************************************
Main: Parallelism: 1
Main: Active Threads: 1
Main: Task Count: 0
Main: Steal Count: 0
******************************************
******************************************
Main: Parallelism: 1
Main: Active Threads: 1
Main: Task Count: 0
Main: Steal Count: 0
******************************************
******************************************
Main: Parallelism: 1
Main: Active Threads: 1
Main: Task Count: 0
Main: Steal Count: 0
******************************************
******************************************
Main: Parallelism: 1
Main: Active Threads: 1
Main: Task Count: 0
Main: Steal Count: 0
******************************************
******************************************
Main: Parallelism: 1
Main: Active Threads: 1
Main: Task Count: 0
Main: Steal Count: 0
******************************************
Highest Number :49991
Process finished with exit code 0