Java threads and number of cores

I just had a quick question about how processors and threads work. According to my current understanding, the kernel can execute only one process at a time. But we can create a pool of threads (say 30) with a larger number than the number of cores that we have (say 4), and run them simultaneously. How is this possible if we have only 4 cores? I can also run the 30 thread program on my local computer, and also continue to perform other actions on my computer, such as watching movies or browsing the Internet.

I read somewhere that thread scheduling is happening, and it seems like the illusion that these 30 threads work simultaneously with four cores. Is that true, and if so, can someone explain how this works and also recommend a good reading for you?

Thank you in advance.

+7
java multithreading concurrency cpu-cores
source share
3 answers

Processes versus Threads

In the old days, each process had exactly one thread of execution, so the processes were planned directly for the kernel (and in these old days only one core was planned). However, in operating systems that support streaming processing (these are almost all modern operating systems), these are threads, not the processes that are planned. Therefore, for the rest of this discussion, we will talk exclusively about threads, and you should understand that every running process has one or more threads of execution.

Parallelism vs concurrency

When two threads run in parallel , they both start simultaneously. For example, if we have two threads, A and B, then their parallel execution will look like this:

CPU 1: A ------------------------->

CPU 2: B ------------------------->

When two threads are running simultaneously , their execution overlaps. Overlapping can occur in one of two ways: either the threads are executed at the same time (that is, in parallel, as indicated above), or their execution is alternated on the processor, for example:

CPU 1: A -----------> B ----------> A -----------> B ----- --- ->

So, for our purposes, parallelism can be considered as a special case of concurrency *

Planning

But we can create a pool of threads (say 30) with a larger number than the number of cores we have (say 4), and run them simultaneously. How is this possible if we have only 4 cores?

In this case, they can start at the same time, because the CPU scheduler gives each of these 30 threads a fraction of the processor time. Some threads will execute in parallel (if you have 4 cores and then four threads will execute in parallel at any time), but all 30 threads will execute at the same time. The reason you can play games or browse the web is because these new threads are added to the thread pool / queue and also give a fraction of the processor time.

Logical and physical cores

According to my current understanding, a kernel can only execute one process at a time

This is not entirely true. Thanks to a very clever equipment design and pipelining that would be too long to enter here (plus I don’t understand this), one physical core can actually execute two completely different execution threads at the same time. Catch this sentence a bit if you need to - it is still blowing in my head.

This amazing feat is called simultaneous multi-threaded (or popular Hyper-Threading, although this is the brand name for a particular instance of such technology). Thus, we have physical cores , which are the actual nuclear cores of the processor, and logical cores , which are the number of cores that the operating system reports about the software. Logical kernels are abstraction. In typical modern Intel processors, each physical core acts as two logical cores.

can someone explain how this works and also recommend you a good read?

I would recommend the concept of an operating system if you really want to understand how processes, threads, and planning work.

  • The exact meanings of terms, parallel and parallel, are hotly debated, even here, in our own stack overflow . What these terms mean is highly dependent on the application area.
+13
source share

In short, your understanding of the kernel is correct. A kernel can execute one thread (for example, a process) at a time.

However, your program does not really run 30 threads at a time. Of these 30 threads, only 4 are executed at a time, and the remaining 26 are waiting. The CPU will schedule threads and give each thread a piece of time to run on the kernel. Thus, the processor will start all threads.

Common misconception:

If you have more threads, my program will run faster.

FALSE: if there are more threads, NOT your program will run faster. It just means that the CPU needs to do more switching, but in fact, too many threads will make your program run slower due to the overhead caused by shutting down all the different processes.

+2
source share

Java does not perform thread scheduling ; it leaves this to the operating system to perform thread scheduling.

For tasks with intensive computing, it is recommended that you have a thread pool size equal to the number of available cores. But for I / O bound tasks, we need to have more threads. There are many other options if both types of tasks are available and a processor slice is required.

the kernel can only execute one process at a time

Yes, but they can multitask and create the illusion that they process more than one process at a time

How is this possible if we have only 4 cores? I can also run my program 30 threads on my local computer, and also continue to perform other actions on my computer.

This is possible due to multitasking (which is concurrency ). Let's say you started 30 threads, and the OS also works with 50 threads, and all 80 threads will share 4 processor cores, getting a processor slice one after another (one thread per core at a time). This means that on average each core will simultaneously launch 80/4 = 20 threads. And you will feel that all threads / processes are running at the same time.

can someone explain how this works

All this happens at the OS level. If you are a programmer, you should not worry about that. But if you are an OS student, then select any OS book and learn more about OS-level multithreading or find a good research paper for depth. One thing you should know is that each OS treats these things differently (but in general the concepts are the same)

There are languages ​​like Erlang that use green threads (or processes) that make it possible to map and schedule threads on their own, eliminating the OS. So do some research on green flows if you are interested.

Note. . You can also explore the participants , which is another thread abstraction . Languages ​​such as Erlang, Scala, etc., use participants to complete tasks. There can be one hundred actors in one topic; each actor can perform different tasks (similar to threads in java).

This is a very extensive and active research topic , and there are many things to learn.

+1
source share

All Articles