Java线程和核心数 [英] Java threads and number of cores

查看:138
本文介绍了Java线程和核心数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚才有一个关于处理器和线程如何工作的快速问题。根据我目前的理解,核心一次只能执行1个进程。但是我们能够生成一个线程池(比方说30),其数量大于我们拥有的核心数量(假设为4)并让它们同时运行。如果我们只有4个核心,这怎么可能?我也可以在我的本地计算机上运行我的30线程程序,并继续在我的计算机上执行其他活动,如观看电影或浏览互联网。

I just had a quick question on how processors and threads work. According to my current understanding, a core can only perform 1 process at a time. But we are able to produce a thread pool(lets say 30) with a larger number than the number of cores that we posses(lets say 4) and have them run concurrently. How is this possible if we are only have 4 cores? I am also able to run my 30 thread program on my local computer and also continue to perform other activities on my computer such as watch movies or browse the internet.

我已经读过某个地方发生了线程调度,这种情况让人觉得这30个线程是由4个内核同时运行的。这是真的,如果是这样,有人可以解释这是如何工作的,并建议一些好的阅读吗?

I have read somewhere that scheduling of threads occurs and that sort of gives the illusion that these 30 threads are running concurrently by the 4 cores. Is this true and if so can someone explain how this works and also recommend some good reading on this?

提前感谢您的帮助。

推荐答案

进程vs线程



在旧的日子里,每个进程只有一个线程执行,因此进程被直接安排到核心上(在过去的这些日子里) ,几乎只有一个核心安排到)。但是,在支持线程的操作系统(几乎都是现代操作系统)中,它是线程,而不是计划的进程。因此,对于本讨论的其余部分,我们将专门讨论线程,您应该了解每个正在运行的进程都有一个或多个执行线程。

Processes vs Threads

In days of old, each process had precisely one thread of execution, so processes were scheduled onto cores directly (and in these old days, there was almost only one core to schedule onto). However, in operating systems that support threading (which is almost all moderns OS's), it is threads, not processes that are scheduled. So for the rest of this discussion we will talk exclusively about threads, and you should understand that each running process has one or more threads of execution.

当两个线程在并行中运行时,它们同时运行 。例如,如果我们有两个线程A和B,那么它们的并行执行将如下所示:

When two threads are running in parallel, they are both running at the same time. For example, if we have two threads, A and B, then their parallel execution would look like this:

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

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

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

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

当两个线程同时运行 时,它们的执行重叠。重叠可以通过以下两种方式之一发生:线程正在同时执行(即并行,如上所述),或者它们的执行在处理器上交错,如下所示:

When two threads are running concurrently, their execution overlaps. Overlapping can happen in one of two ways: either the threads are executing at the same time (i.e. in parallel, as above), or their executions are being interleaved on the processor, like so:

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

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

所以,出于我们的目的,并行性可以被认为是并发的一个特例*

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


但是我们能够生成一个线程池(比方说30)数字大于我们拥有的核心数量(比方说4)并让它们同时运行。如果我们只有4个核心,这怎么可能?

But we are able to produce a thread pool(lets say 30) with a larger number than the number of cores that we posses(lets say 4) and have them run concurrently. How is this possible if we are only have 4 cores?

在这种情况下,它们可以并发运行,因为CPU调度程序正在为这30个线程中的每一个分配一些CPU时间。有些线程并行运行(如果你有4个内核,那么4个线程将同时并行运行),但所有30个线程将同时运行。你可以去玩游戏或浏览网页的原因是这些新线程被添加到线程池/队列中,并且还分配了CPU时间。

In this case, they can run concurrently because the CPU scheduler is giving each one of those 30 threads some share of CPU time. Some threads will be running in parallel (if you have 4 cores, then 4 threads will be running in parallel at any one time), but all 30 threads will be running concurrently. The reason you can then go play games or browse the web is that these new threads are added to the thread pool/queue and also given a share of CPU time.


根据我目前的理解,核心一次只能执行1个进程

According to my current understanding, a core can only perform 1 process at a time

这不是相当为真。由于非常聪明的硬件设计和流水线操作太长而无法进入(加上我不理解),一个物理内核可能实际执行两个完全不同的执行线程同一时间。如果你需要,可以稍微咀嚼这句话 - 它仍然让我大吃一惊。

This is not quite true. Due to very clever hardware design and pipelining that would be much too long to go into here (plus I don't understand it), it is possible for one physical core to actually be executing two completely different threads of execution at the same time. Chew over that sentence a bit if you need to -- it still blows my mind.

这个惊人的壮举被称为同时多线程(或普遍的超线程,尽管这是此类技术的特定实例的专有名称。因此,我们有物理内核,它们是实际的硬件CPU内核,逻辑内核,这是操作系统告诉软件可以使用的内核数。逻辑核心本质上是一种抽象。在典型的现代英特尔CPU中,每个物理核心充当两个逻辑核心。

This amazing feat is called simultaneous multi-threading (or popularly Hyper-Threading, although that is a proprietary name for a specific instance of such technology). Thus, we have physical cores, which are the actual hardware CPU cores, and logical cores, which is the number of cores the operating system tells software is available for use. Logical cores are essentially an abstraction. In typical modern Intel CPUs, each physical core acts as two logical cores.


有人可以解释这是如何工作的,并建议一些好的阅读这个?

can someone explain how this works and also recommend some good reading on this?

如果你真的想了解流程,线程和方法,我会推荐操作系统概念安排所有工作。

I would recommend Operating System Concepts if you really want to understand how processes, threads, and scheduling all work together.


  • 术语 parallel 并发的确切含义热烈的争论,甚至在我们自己的堆栈溢出。这些术语的含义在很大程度上取决于应用程序域。

  • The precise meanings of the terms parallel and concurrent are hotly debated, even here in our very own stack overflow. What one means by these terms depends a lot on the application domain.

这篇关于Java线程和核心数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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