Online Java Compiler By
JavaTpoint.com
import java.util.ArrayList; import java.util.List; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.RecursiveAction; class Work extends RecursiveAction { private long Load = 0; public Work(long Load) { this.Load = Load; } @Override protected void compute() { //if work is above threshold, break tasks up into smaller tasks List
subtasks = new ArrayList
(); subtasks.addAll(createSubtasks()); for(RecursiveAction subtask : subtasks){ subtask.fork(); } } private List
createSubtasks() { List
subtasks =new ArrayList
(); Work subtask1 = new Work(this.Load / 2); subtasks.add(subtask1); return subtasks; } } public class JavaForkJoingetPoolSizeExample1 { public static void main(final String[] arguments) throws InterruptedException { ForkJoinPool Pool = ForkJoinPool.commonPool(); int i= Pool.getPoolSize(); System.out.println("Common Pool Size before :" +i); Work t = new Work(1000); Pool.invoke(t); System.out.println("Common Pool Size after :" +Pool.getPoolSize()); } }
Output