Reputation: 3612
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);
}
Upvotes: 1
Views: 2559
Reputation: 2090
There is a concept called hyper-threading used in most Intel processors. In very simple terms, what this means is- if you have a quad-core (4 CPU) machine, it will allow you to run 8 threads. Four of these are real threads and four are virtual threads. In dual core, you can run 4 threads (2 real and 2 virtual)
If I'd run the same program you wrote on my mac-book (Quad-Core Intel Core i7), I'd get the most optimised time with 8 threads running in parallel, because my processor has hyper-threading enabled.
If your machine CPU does not have hyper-threading enabled, you would be able to run 2 threads in parallel in a dual core machine, 4 threads in quad core, 8 threads in octa core and so on..
Upvotes: 0
Reputation: 8481
Does
parallelism
in contructor mean the amount of threads or amount of processors
The parallelism
parameter tells ForkJoinPool
how many worker threads to use. By default it equals to the number of processors available which is typically optimal. Of course you can set any number here, but setting it greater than the number of processors most likely will not benefit.
Does it mean that the following ForkJoinPool
pool = new ForkJoinPool(8);
has no effect because my PC has only 4 processors?
It depends on your actual task. For example, your code example will print 8 strings per second: you have 8 threads, and the operating system will schedule all of them on 4 processors using a context switching. Since most of the time the threads are just waiting, and the execution time is very short, you get eight strings per second.
Upvotes: 2
Reputation: 19
"This implementation restricts the maximum number of running threads to 32767. Attempts to create pools with greater than the maximum number result in IllegalArgumentException." from ForkJoinPool - Java8 API doc
Upvotes: 0
Reputation: 70909
If you over allocate the number of worker threads, you will simply have threads that bind and unbind from the CPU cores. This might impact performance, or it might not, depending on your workflow (and how often the tasks the threads are working on get blocked by I/O).
Note that the default value is the number of CPU cores, but there is no indication that it is an upper limit.
Upvotes: 1