Online Java Compiler By
JavaTpoint.com
import java.util.concurrent.ForkJoinPool; import static java.util.concurrent.ForkJoinTask.invokeAll; import java.util.concurrent.RecursiveTask; import java.util.concurrent.TimeUnit; public class JavaForkJoinPoolisQuiescenceExample1 { public static void main(final String[] arguments) throws InterruptedException { int proc = Runtime.getRuntime().availableProcessors(); System.out.println("numbers of core available in your processor:" +proc); int[] n = {20 , 23 , 5 ,6 ,7,8,23,12,56,1}; ForkJoinPool Pool = new ForkJoinPool(proc); Work7 t=new Work7(n,0,n.length); System.out.println(" is Quiescent :" + Pool.isQuiescent() ); ForkJoinPool.commonPool().awaitQuiescence(2, TimeUnit.SECONDS); Integer r = Pool.invoke(t); System.out.println("Pool.invoke :" +r); } } class Work7 extends RecursiveTask
{ int st; int end; int[] arr; Work7(int[] arr, int st, int end) { this.arr = arr; this.st = st; this.end = end; } protected Integer compute() { if(st - end <= 10) { int sum = 0; for(int i = st; i < end; ++i) sum += arr[i]; return sum; } else { int mid = st + (end - st) / 2; Work7 t1 = new Work7(arr, st, mid); Work7 t2 = new Work7(arr, st, mid); invokeAll(t1 , t2); return (t1.join() + t2.join()); } } }
Output