Difference between processors and threads

Viewed 374

I am using parallel stream of java 8, but I don't understand one thing:

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 I can only use 8 threads?

The above code runs 8 in parallel and next will be waiting, but if I use a custom thread pool using ForkJoinPool tasks more than 8 will be running in parallel.

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 runs only 16 in parallel. If I can use more than 8 threads in 8 processor machine, what is the max number of threads I can use.

Edit 1-Does this mean the max number of threads we can use is 2*available processor's?

3 Answers
Related