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

查看:26
本文介绍了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.

按照我目前的理解,一个core一次只能执行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 内核,以及逻辑内核,即操作系统告诉软件可供使用的内核数.逻辑核心本质上是一种抽象.在典型的现代 Intel 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.

  • 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天全站免登陆