4

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?

GhostCat
  • 137,827
  • 25
  • 176
  • 248
nanpakal
  • 971
  • 3
  • 18
  • 37
  • You could run 100 threads on a single processor - but they wouldn't run concurrently: the OS thread scheduler would make them run and wait in turn based on various factors (such as thread priority etc.). – assylias Jul 25 '17 at 13:22
  • https://www.quora.com/How-does-multithreading-work-in-a-single-core-computer – Elan Hamburger Jul 25 '17 at 13:22
  • You can run X-amount of `Threads` inside one single `Process`. A `Processor` in other words the `CPU` is the unit where it all runs down. – Murat Karagöz Jul 25 '17 at 13:23
  • `Thread` and `Process` are a software concept. `Processor `is hardware. Software runs on hardware. Operating system provides special means to schedule Threads and Processes in a time-sharing manner on Processors. And BTW, there is difference between a thread and a process. – Serge Jul 25 '17 at 13:25
  • For maximum number of threads refer to this question - https://stackoverflow.com/questions/763579/how-many-threads-can-a-java-vm-support – Manish Jul 25 '17 at 13:28
  • @assylias, In most of the literature that I have read, two threads are considered to be "concurrent" if both have started, and neither one has finished. By that definition, you can have more concurrent threads on a machine than the machine has processors. https://en.wikipedia.org/wiki/Concurrency_(computer_science) – Solomon Slow Jul 25 '17 at 15:06
  • @jameslarge You're right, I should have said "they won't perform actual work at the same time" or something like that... – assylias Jul 25 '17 at 18:50
  • And just for the record - you can still accept answers for duplicated questions; so in case you find one of the answers really helpful; dont hesitate to accept it. – GhostCat Jul 26 '17 at 06:43
  • Yeah that answers my question but not completely .Behavior of parallel stream is described here http://www.javaspecialists.eu/archive/Issue220.html – nanpakal Jul 26 '17 at 07:42

3 Answers3

2

You can run X-amount of Threads inside one single Process. A Processor in other words the CPU is the unit where it all runs down. You can check the Resource Monitor on Windows to see how many Threads are running in one Process.

For example the Chrome Browser is running in three processes while having 20 Threads respectively. In other words you can start as many Threads as the CPU Power/Memory etc. allows.

Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
  • but how to find out the number of threads i can use assuming just only my application will run on 8 processor machine – nanpakal Jul 25 '17 at 13:39
  • @pppavan See this question https://stackoverflow.com/questions/763579/how-many-threads-can-a-java-vm-support – Murat Karagöz Jul 25 '17 at 13:39
  • @pppavan on recent machines you can go to hundreds or even thousands of threads before hitting a limit. But that's not to say that the best performance will be achieved with such a high number. See for example: https://stackoverflow.com/a/13958877/829571 – assylias Jul 25 '17 at 14:18
1

Determining the optimal number of threads to use is actually way harder than it sounds. It starts with the question how many cores, processors are actually available. And even when you know that - you still don't know how many threads each core will support in hardware.

Thus: there are several articles on the java specialist newsletter which do a very deep dive into this subject.

For example this one: http://www.javaspecialists.eu/archive/Issue135.html

or a very new, on "the number of available processors": http://www.javaspecialists.eu/archive/Issue220.html

In any case, there are some rules of thumb for using parallel streams:

  • understand that the implementation is using certain defaults
  • thus: monitor/measure the actual results delivered by your code
  • and when you are unhappy: start fine-tuning
GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

Number of threads you can use in your Java application only limited by JVM or operating system. You should distinguish two things:

  • real parallelism means that you can run 8 threads using 8 cores in the same time without switching processor context;
  • pseudo-parallelism means that you can run 8 threads at only single processor, for example, but this one processor will constantly switch from one thread to another. While this processor executes one thread, others will be suspended until operating system will make processor to run another thread taken from suspended ones.
noavarice
  • 15
  • 3