multithreading 软件线程与硬件线程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5593328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 01:14:20  来源:igfitidea点击:

software threads vs hardware threads

multithreadinghardware

提问by Bhadri

What is the difference between software threads, hardware threads and java threads?

软件线程、硬件线程和java线程有什么区别?

Are software threads, java threads and hardware threads independent or interdependent? I am asking this because, I know Java threads are created inside a process with in jvm (java.exe).

软件线程、java线程和硬件线程是独立的还是相互依赖的?我问这个是因为,我知道 Java 线程是在使用 jvm (java.exe) 的进程内创建的。

Also is it true that these different process are executed on different hardware threads?

这些不同的进程是否在不同的硬件线程上执行?

采纳答案by Will

Software threads are threads of execution managed by the operating system.

软件线程是由操作系统管理的执行线程。

Hardware threads are a feature of some processors that allow better utilisation of the processor under some circumstances. They may be exposed to/by the operating system as appearing to be additional cores ("hyperthreading").

硬件线程是某些处理器的一项功能,可在某些情况下更好地利用处理器。它们可能会暴露给操作系统/被操作系统暴露为似乎是附加内核(“超线程”)。

In Java, the threads you create maintain the software thread abstraction, where the JVM is the "operating system". Whether the JVM then maps Java threads to OS threads is the JVM's business (but it almost certainly does). And then the OS will be using hardware threads if they are available.

在 Java 中,您创建的线程维护软件线程抽象,其中 JVM 是“操作系统”。JVM 然后是否将 Java 线程映射到 OS 线程是 JVM 的事情(但几乎可以肯定)。然后操作系统将使用硬件线程(如果可用)。

回答by mdma

A "hardware thread" is a physical CPU or core. So, a 4 core CPU can genuinelysupport 4 hardware threads at once - the CPU really is doing 4 things at the same time.

“硬件线程”是物理 CPU 或内核。因此,一个 4 核 CPU 可以真正同时支持 4 个硬件线程——CPU 确实在同时做 4 件事。

One hardware thread can run many software threads. In modern operating systems, this is often done by time-slicing - each thread gets a few milliseconds to execute before the OS schedules another thread to run on that CPU. Since the OS switches back and forth between the threads quickly, it appearsas if one CPU is doing more than one thing at once, but in reality, a core is still running only one hardware thread, which switches between many software threads.

一个硬件线程可以运行多个软件线程。在现代操作系统中,这通常是通过时间切片来完成的——在操作系统调度另一个线程在该 CPU 上运行之前,每个线程都有几毫秒的时间来执行。由于操作系统在线程之间快速来回切换,看起来好像一个 CPU 一次在做不止一件事情,但实际上,一个内核仍然只运行一个硬件线程,它在许多软件线程之间切换。

Modern JVMs map java threads directly to the native threads provided by the OS, so there is no inherent overhead introduced by java threads vs native threads. As to hardware threads, the OS tries to map threads to cores, if there are sufficient cores. So, if you have a java program that starts 4 threads, and have 4 or more cores, there's a good chance your 4 threads will run truly in parallel on 4 separate cores, if the cores are idle.

现代 JVM 将 Java 线程直接映射到 OS 提供的本地线程,因此没有 Java 线程与本地线程引入的固有开销。对于硬件线程,如果有足够的内核,操作系统会尝试将线程映射到内核。因此,如果您有一个启动 4 个线程并且有 4 个或更多内核的 Java 程序,那么您的 4 个线程很有可能在 4 个独立的内核上真正并行运行(如果内核空闲)。

回答by Rakesh K

Hardware threads can be thought of as the CPU cores, although each core can run multiple threads. Most of the CPUs mention how many threads can be run on each core (on linux, lscpu command gives this detail). These are the number of cores that can be used in parallel.

硬件线程可以被认为是 CPU 内核,尽管每个内核可以运行多个线程。大多数 CPU 都提到每个核心上可以运行多少线程(在 linux 上,lscpu 命令提供了这个细节)。这些是可以并行使用的内核数。

Software threads are abstraction to the hardware to make multi-processing possible. If you have multiple software threads but there are not multiple resources then these software threads are a way to run all tasks in parallel by allocating resources for limited time(or using some other strategy) so that it appears that all threads are running in parallel. These are managed by the operating system. Java thread is an abstraction at the JVM level.

软件线程是硬件的抽象,使多处理成为可能。如果您有多个软件线程但没有多个资源,那么这些软件线程是一种通过在有限的时间内分配资源(或使用其他策略)来并行运行所有任务的方法,这样看起来所有线程都在并行运行。这些由操作系统管理。Java 线程是 JVM 级别的抽象。

回答by questzen

I think you are mistaken. I never heard about hardware threads (unless you mean hyper threading on certain intel machines). Every process is a running representation of a program. Threads are simultaneous execution flows with in a process. Java thread definitions are mapped to system threads by JVM. Java used to have a concept of GreenThreads, which is no longer the case.

我认为你错了。我从未听说过硬件线程(除非您指的是某些英特尔机器上的超线程)。每个进程都是一个程序的运行表示。线程是进程中的同时执行流。Java 线程定义由 JVM 映射到系统线程。Java 曾经有一个GreenThreads的概念,现在已经不是这样了。