Set java.util.concurrent.ForkJoinPool.common.parallelism java property?

The dynamic setting works when it is the first statement accessing the pool. Try removing the first line which gets and prints the default parallelism. Use just the below.

    System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism","20");
    System.out.println("getParallelism=" +ForkJoinPool.commonPool().getParallelism());

Otherwise you can create your custom pool and achieve the parallelism you want

int numTasks = 20;
ForkJoinPool pool = new ForkJoinPool(numTasks);
System.out.println("getCustomParallelism=" +pool.getParallelism()); //prints 20

You have to set the parameter before the application starts. So passing a JVM parameter

-Djava.util.concurrent.ForkJoinPool.common.parallelism=20

at startup will change it to 20.

Tags:

Java