Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How JVM thread scheduler control threads for multiprocessors?

I've been reading Head First for multithreading. What I know about multithreading is:

When we call start() with an object of Thread class that thread goes to the Runnable state. So all the threads go to Runnable state after calling start() by the object of those threads. It is JVM thread scheduler, who picks thread randomly from Runnable state to give it in Running state. After going to Running state, the determined call stack for that specific thread becomes executed.

Again, JVM thread scheduler can stop the execution of a thread by picking that thread from Running state to Runnable state. This time, code execution is paused in the call stack of that thread.

Now my question is, for multiprocessor machine, how JVM thread scheduler picks thread from the Runnable state? Does it pick only one thread and give it to a processor? Or, does it pick more than one thread and give those threads to Running state of different processors?

I have written below code:

// Class of main thread
public class ThreadMain {

    public static void main(String[] args) {

        Runnable threadJob=new MyRunnable();
        Thread t=new Thread(threadJob);
        t.start();
        System.out.println("Back in the Main");
    }
}
// Class of another thread
public class MyRunnable implements Runnable{

    public void run()
    {
        System.out.println("I'm Thread");
    }
}

Here, there are two threads. Main thread, and the thread I've created. If my machine has multiprocessor, how will it behave? Will the JVM thread scheduler pick two threads at a time, and give those to two processors?

like image 318
Mukit09 Avatar asked Jan 20 '17 08:01

Mukit09


3 Answers

The term “JVM thread scheduler” makes only sense, if we consider operating system, JVM and class library as an execution environment as a whole. Then, it’s guaranteed that this environment has a scheduler, regardless of how it is implemented.

In most of today’s implementations the JVM will create an operating system level thread for each Java thread and does no active scheduling activity itself. But a particular JVM implementation may contain a scheduler for operating systems that don’t have one.

E.g., for Sun’s JVM that was the case back in the last millennium. At this time, there was the option to use green threads, as opposed to native threads. Note that these threads implemented without the aid of the operating system aren’t capable of using multiple CPUs/cores.

So in practice, when you run your example program, the operating system’s scheduler may indeed assign the second thread to a different core. However, since this is a tiny program, it’s also possible that the first thread terminates before the second even starts its actual work, and in that case, it will likely run on the same core as the first, but there is no guaranty about any particular scheduling behavior at all.

While there is no guaranty regarding a particular scheduling behavior, most SMP libraries and tools are built on the (founded) assumption, that if there are enough runnable threads with sufficient workload, the underlying system will assign these threads to available CPU cores.

like image 195
Holger Avatar answered Oct 18 '22 22:10

Holger


As the others answered correctly, JVM uses the underlying OS (Windows 10 in my case) to manage threads. The windows OS will do pre-emptive (priority-based) scheduling. If there exists multiple threads with Highest priority, windows will use round-robin based time-slicing scheduling for the threads.

( Reference:- https://docs.microsoft.com/en-us/windows/win32/procthread/scheduling-priorities )

To answer the specific question about the multi-processor systems (windows again here as OS), it depends on the architecture such a system is using. Typically there are 2 types of architecture

  1. NUMA - Non Uniform Memory Access
  2. SMP - Symmetric Multi-Processing

In case of NUMA, the thread is assigned to a processor which is closer (physically) to the memory being used. Because, that way the memory access is faster.

However, in an SMP computer, two or more identical processors or cores connect to a single shared main memory. Essentially, it is like running things on a single integrated processor. Here, we cannot guarantee which processor the thread will be assigned to.

You can also specify a thread to execute on a particular processor by setting the "ThreadAffinity" or the "Thread Ideal Processor" property.

( Reference:- https://docs.microsoft.com/en-us/windows/win32/procthread/multiple-processors ).

Hope this helps !! Cheers !!

like image 29
Yash Nasery Avatar answered Oct 18 '22 20:10

Yash Nasery


JVM use underlying OS(Unix, Windows etc) threading mechanism to schedule java thread on multiprocessor system.

like image 36
Pandey Amit Avatar answered Oct 18 '22 22:10

Pandey Amit