0
public class Demo {
    public static void main(String[] args) {
        ForkJoinPool forkJoinPool = new ForkJoinPool(17);
        for (int i = 0; i < 100; i++) {
            forkJoinPool.execute(() -> {
                System.out.println(Thread.currentThread().getName());
            });
        }
        try {
            TimeUnit.DAYS.sleep(1L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

output (part of console)

ForkJoinPool-1-worker-4
ForkJoinPool-1-worker-19
ForkJoinPool-1-worker-18
ForkJoinPool-1-worker-30
ForkJoinPool-1-worker-1
ForkJoinPool-1-worker-23
ForkJoinPool-1-worker-8
ForkJoinPool-1-worker-16
ForkJoinPool-1-worker-25
ForkJoinPool-1-worker-11
ForkJoinPool-1-worker-29
ForkJoinPool-1-worker-9
ForkJoinPool-1-worker-5
ForkJoinPool-1-worker-15
ForkJoinPool-1-worker-22
ForkJoinPool-1-worker-26
ForkJoinPool-1-worker-12

Why more than 17 threads?

In my test case, I want set 17 threads, how can I do that? How to understand the parallelism of forkjoinpool's constructor?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
jzdayz
  • 11
  • 1
  • the total number of threads are still 17.... it's just that they are numbered differently. – tsamridh86 Nov 25 '20 at 03:03
  • for the official documentation : https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html#ForkJoinPool-int- – tsamridh86 Nov 25 '20 at 03:04
  • why do you expect that they start with `0` or `1` all the time? this is not documented to be such, so... – Eugene Nov 25 '20 at 03:39
  • Does this answer your question? [ForkJoinPool creates a huge amount of workers](https://stackoverflow.com/questions/36422654/forkjoinpool-creates-a-huge-amount-of-workers) – akuzminykh Nov 25 '20 at 06:58
  • If you want a specific number of work threads, don't use `ForkJoinPool`, use `Executors.newFixedThreadPool​(int nThreads)` instead. – Mark Rotteveel Nov 25 '20 at 09:50
  • Related: [What determines the number of threads a Java ForkJoinPool creates?](https://stackoverflow.com/questions/10797568/what-determines-the-number-of-threads-a-java-forkjoinpool-creates) – Mark Rotteveel Nov 25 '20 at 09:51
  • @Samridh Tuladhar You are right, I found through visualvm that the number of threads will not exceed 17 – jzdayz Nov 30 '20 at 06:04

0 Answers0