1

I see that the default ForkJoinPool.commonPool size is one less thread than number of CPU cores. And also to make custom pool we can either use follows,

1. java.util.concurrent.ForkJoinPool.common.parallelism=30
2. ForkJoinPool fjp = new ForkJoinPool(30);

But I am struggling to understand how this actually works. Multithreading is an OS thing. So if a processor has 2 cores and 4 threads (details that OperatingSystem shows), and I changed the Java parallelism to 30 how will it work with the CPU? What is the maximum number of threads it will create instantly and can run simultaneously? Will it create 30 threads at the same time and process in parallel or since the CPU has 4 threads will it create 4 threads and once it is finished again another 4 threads like that? Or even how much we increase parallelism and even in Spring Boot's batch with higher grid size, will it always depend on the CPU cores and CPU's threads? Thanks in advance.

Dave
  • 123
  • 1
  • 11
  • 2
    Do you know what scheduling is? – dan1st Feb 19 '22 at 20:20
  • Yes. I know about scheduling – Dave Feb 19 '22 at 21:07
  • 1
    If the concurrency level is 30, the JVM will create up to 30 threads whenever it thinks it needs to. Then, the OS/scheduler takes over and decide which threads can work. – dan1st Feb 19 '22 at 21:17
  • So that mean, will that 30 threads spawn simultaneously and process simultaneously? Or still it will create threads less than 30? – Dave Feb 19 '22 at 22:31
  • 1
    It will create 30 if it thinks it has sufficient work. – dan1st Feb 20 '22 at 06:27
  • Ok so that means no matter how many Cores and Threads in CPU but it will create many parallel threads right? Because the program is not limited to CPU? – Dave Feb 20 '22 at 08:57
  • 1
    Yes, It is limited only by default because the default size for `commonPool()` is the number of threads. – dan1st Feb 20 '22 at 12:39
  • Great. Thank you for making me understand this. – Dave Feb 20 '22 at 19:24

0 Answers0