I am using java 8 parallel thread, but I don't understand anything:
I have a machine with 8 processors ...
IntStream.range(0, 9).parallel().forEach(i -> {
int cnt = 0;
while (System.currentTimeMillis() < runUntil)
cnt++;
System.out.println(i + ": " + cnt);
})
Does this mean that I can only use 8 threads?
The above code runs 8 in parallel, and the next one will wait, but if I use my own thread pool using ForkJoinPool, tasks will be executed for more than 8.
ForkJoinPool forkJoinPool = new ForkJoinPool(17);
forkJoinPool.submit(()->IntStream.range(0, 17).parallel().forEach(i ->
{
int cnt = 0;
while(System.currentTimeMillis() < runUntil)
cnt++;
System.out.println(i + ": " + cnt);
})).get();
The above code only works 16 in parallel. If I can use more than 8 threads on an 8-processor computer, what is the maximum number of threads I can use.
Change 1 - does this mean that the maximum number of threads that we can use is 2 * available processors?