The maximum number of threads that can be executed simultaneously in java on the CPU

Please, I am embarrassed by something. I know that the maximum number of threads that can run simultaneously on a regular processor of a modern computer is from 8 to 16 threads. When using GPUs, on the other hand, thousands of threads can be started simultaneously without the scheduler interrupting any thread in order to schedule another one. In several positions: Java Virtual Machine - maximum number of threads https://community.oracle.com/message/10312772 people claim that they run thousands of Java threads simultaneously on conventional processors. How could this happen?? And how can I find out the maximum number of threads that can be launched at the same time, so that my code configures it itself dynamically in accordance with the underlying architecture.

+8
java multithreading cpu gpu
source share
3 answers

Threads are not tied or limited by the number of processors / cores available. The operating system scheduler can switch between any threads of the same processor. This is the meaning of "proactive multitasking."

Of course, if you have more threads than cores, not all threads will run at the same time. Some of them will be waiting for a time interval.

In practice, the number of threads you may have is limited by the scheduler, but this number is usually very large (thousands or more). It will differ from OS to OS with separate versions.

How many threads are useful in terms of performance, as you said, it depends on the number of processors available and whether the task is related to IO or CPU. Experiment to find the optimal number and tune it, if possible.

+8
source share

There is hardware and software concurrency. From 8 to 16 threads refers to the equipment that you have - it is one or more processors with hardware to run from 8 to 16 threads parallel to each other. Thousands of threads are among the program threads, the scheduler will have to replace them so that each program thread gets its own time slice for working on hardware.

To get the number of hardware threads, you can try Runtime.availableProcessors() .

+3
source share

At any given time, the processor will start the number of threads equal to the number of cores contained. This means that in a uniprocessor system at any given time only one thread (or thread) is running.

However, processors do not start each thread one by one, but quickly switch between multiple threads to simulate simultaneous execution. If this were not so, not to mention creating multiple threads, you won’t even be able to run multiple applications.

A java thread (compared to processor instructions) is a very high level of abstraction of a set of instructions for the processor to process. When it goes to the processor level, there is no guarantee which threads will run on which core at any given time. But, given that processors quickly switch between these threads, it is theoretically possible to create an infinite number of threads, albeit at the cost of performance.

If you think about it, a modern computer has thousands of threads working simultaneously (combining all applications), having only 1 ~ 16 (typical case) number of cores. Without this task switch, nothing will work.

If you are optimizing your application, you should consider the number of threads you need at your fingertips, not the underlying architecture. The performance gain from parallelism should be balanced against the increase in thread execution overhead. Since each machine is different, each runtime is different from the others, it is impractical to work out a certain number of gold threads (however, the ball can be estimated by benchmarking and viewing the number of cores).

+1
source share

All Articles