JVM线程调度程序如何控制多处理器的线程? [英] How JVM thread scheduler control threads for multiprocessors?

查看:132
本文介绍了JVM线程调度程序如何控制多处理器的线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读《 Head First》以了解多线程.我对多线程的了解是:

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

当我们使用 Thread 类的对象调用 start()时,线程将进入 Runnable 状态.因此,在所有线程的对象调用 start()之后,所有线程都进入 Runnable 状态.是 JVM线程调度程序 ,他从 Runnable 状态中随机选择线程以使其处于 Running 状态.进入运行状态后,将执行为该特定线程确定的调用堆栈.

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.

同样,JVM线程调度程序可以通过将线程从运行"状态选择为可运行"状态来停止线程的执行.这次,代码执行在该线程的调用堆栈中暂停.

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.

现在我的问题是,对于多处理器计算机,JVM线程如何运行 调度程序从Runnable状态中选择线程?它只选一个吗 线程并将其提供给处理器?还是选了不止一个 线程并将这些线程赋予不同的运行状态 处理器?

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?

我写了下面的代码:

// 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");
    }
}

在这里,有两个线程.主线程,以及我创建的线程.如果我的机器有多处理器,它将如何运行? JVM线程调度程序会一次选择两个线程,然后将其分配给两个处理器吗?

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?

推荐答案

如果我们将操作系统,JVM和类库整体视为一个执行环境,则术语"JVM线程调度程序"才有意义.然后,无论此环境如何实现,都可以确保该环境具有调度程序.

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.

在当今的大多数实现中,JVM都会为每个Java线程创建一个操作系统级别的线程,并且本身不会进行活动的调度活动.但是特定的JVM实现 可能包含一个调度程序,用于没有该操作系统的操作系统.

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.

例如,对于Sun的JVM来说,这在上个千年就已经发生了.目前,可以选择使用绿色线程,而不是使用本地线程.请注意,在没有操作系统帮助的情况下实现的这些线程无法使用多个CPU/内核.

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.

虽然没有关于特定调度行为的保证,但是大多数SMP库和工具都是基于(已建立的)假设构建的,即如果有足够的可运行线程和足够的工作量,则基础系统会将这些线程分配给可用的CPU内核.

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.

这篇关于JVM线程调度程序如何控制多处理器的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆