简体   繁体   English

我有一台双核计算机。 在Java中,我的计算机如何管理3个线程的fixedThreadPool? jvm的可能行为是什么?

[英]I have a dual core machine. In java, how is a fixedThreadPool of 3 threads managed by my computer? What is a possible behavior of the jvm?

I'm trying to visualize how my machine would behave with this code if its a dual core processor. 我试图可视化我的机器在使用双核处理器时如何使用此代码。 How would it behave if it were a quad core machine? 如果它是四核计算机,它将如何表现?

    ExecutorService executor = Executors.newFixedThreadPool(3);

    // Submission of tasks
    for (int i =0; i<10; i++){
        executor.submit(new Processor(i));
    }

The Processor has a run method that prints "running in thread: about to sleep..." Then it sleeps for 5 seconds then prints "running in thread: woke up..." 处理器具有运行方法,该方法打印“正在线程中运行:即将进入睡眠状态...”,然后睡眠5秒钟,然后打印“正在线程中运行:已唤醒...”。

I'm trying to connect the dots between java code and hardware. 我正在尝试在Java代码和硬件之间建立联系。 I'm having difficulty picturing how this differs in different processor environments. 我很难想象在不同的​​处理器环境中这有何不同。 Could someone give an example of a possible behavior of the jvm in this situation? 有人可以举例说明在这种情况下jvm的可能行为吗? Thank you in advance. 先感谢您。

The answer to each of your questions is, "yes". 您的每个问题的答案都是“是”。 Or, to be less coy, the behavior you are asking for us to document is undefined very deliberately only loosely defined. 或者,为了减少麻烦,您要我们记录的行为是 未明确 定义的,只能是宽松定义的。

The JVM and O/S, working together are free to run your threads in any order at any time, on whatever CPU/core is available when they are eligible to run, provided they sleep for at least 5 seconds between the running and woke up messages. JVM和O / S可以自由工作,并且可以在任意时间以任何顺序自由运行线程,只要它们有资格在runningwoke up之间睡眠至少5秒钟,就可以在有资格运行的任何CPU /核上运行它们woke up消息。 Due to the nature of sleep, it will be at least that time, and the thread will awake as close to that time as the scheduler can manage. 由于睡眠的性质,至少要到那个时间,线程将在调度程序可以管理的那个时间醒来。

Beyond that, there's not much more that can be said. 除此之外,没有什么可以说的了。


In response to the comment, the question's code will add 10 items to a task queue, and will dequeue up to three at a time(*) and run each group of three concurrently. 作为对评论的回应,问题的代码会将10个项目添加到任务队列中,并且一次最多可出3个队列(*),并同时运行每组三个项目。

Because the execution time is dominated by the sleep , the behavior will be essentially identical no matter how many cores are available. 由于执行时间主要由sleep ,因此无论有多少个内核可用,其行为基本上都是相同的。

However, the exact order in which things will happen is non-deterministic, beyond the fact that the tasks will be pulled off in the order added. 但是,发生事情的确切顺序是不确定的,超出了将按照添加的顺序执行任务的事实。 So while you can be certain that the 10th task will execute after the 1st, you can't be certain about the ordering of 1, 2 and 3, nor whether 4, 5 or 6 might start before 1, 2 or 3 has entirely finished. 因此,尽管可以确定第十个任务将在第一个任务之后执行,但是不能确定1、2和3的顺序,也不能确定4、5或6是否可能在1、2或3完全完成之前开始。

(*) Up to three, because the last group will have only two items and the third thread will be idle while they run. (*)最多三个,因为最后一个组只有两个项目,而第三个线程在运行时将处于空闲状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM