I have met a new class from java.util.concurrent package named ForkJoinPool
As i understand it works using the following scenario:
Divide the big task into subtasks(fork) and when each task is completed, gather all subtask(join), then merge it.
Fork join pool has a constructor public ForkJoinPool(int parallelism)
.
From the official docs i have read the following:
By splitting itself up into subtasks, each subtask can be executed in parallel by different CPUs, or different threads on the same CPU.
The question is How to set the amount of threads to forkjoinpool.
Does parallelism
in contructor mean the amount of threads or amount of processors. If it is the amount of processors, consider the following code:
Runtime.getRuntime().availableProcessors()
return 4
Does it mean that the following ForkJoinPool pool = new ForkJoinPool(8);
has no effect because my PC has only 4 processors?
Code example
public static void main(String[] args) throws Exception {
ForkJoinPool pool = new ForkJoinPool(8);
final List<String> list = Arrays.asList("dasd","dasd")//for example 300 hundrends strings;
pool.submit(()->{
list.parallelStream().forEach(AvailableProc::test);
}).get();
}
public static void test(final String code){
Thread.sleep(1000);
System.out.println(code);
}