简体   繁体   English

多处理器运行多线程 - 易失性关键字 Java

[英]Multiple Processors Running Multiple Threads - Volatile Keyword Java

Can someone please help me to understand the following concept?有人可以帮我理解以下概念吗? I am studying the Volatile keyword in Java as per the link shown here: https://www.geeksforgeeks.org/volatile-keyword-in-java/根据此处显示的链接,我正在研究 Java 中的 Volatile 关键字: https://www.geeksforgeeks.org/volatile-keyword-in-java/

It seems to make sense that volatile means: that the changes made in one thread are immediately reflected in other thread volatile 的意思似乎是有道理的:在一个线程中所做的更改会立即反映在另一个线程中

The explanation from the article says: "Below diagram shows that if two threads are run on different processors, then value of sharedVariable may be different in different threads."文章的解释说:“下图显示如果两个线程在不同的处理器上运行,那么 sharedVariable 的值可能在不同的线程中不同。”

  • My question is how can their be two processors?我的问题是他们怎么可能是两个处理器? Are these part of the same CPU?这些是同一个 CPU 的一部分吗? Wouldn't there only be 1 processor attached with a computer running an application or program?运行应用程序或程序的计算机不会只有一个处理器吗?

  • And upon explanation of this, how would the "volatile" keyword go about ensuring "the changes made in one thread are immediately reflected in other thread"在对此进行解释后,“volatile”关键字 go 如何确保“在一个线程中所做的更改立即反映在另一个线程中”

how can their be two processors?他们怎么可能是两个处理器? Are these part of the same CPU?这些是同一个 CPU 的一部分吗?

"Processor" usually is a synonym for CPU. “处理器”通常是 CPU 的同义词。 Most desktop computers, most servers, and even most cell phones and tablets these days have more than one CPU.如今,大多数台式电脑、大多数服务器,甚至大多数手机和平板电脑都拥有不止一个 CPU。 Typically they comprise a symmetric multiprocessor system (SMP) in which all of the CPUs share the same common memory system.通常,它们包含一个对称多处理器系统(SMP),其中所有 CPU 共享相同的公共 memory 系统。

In some high-performance computing applications, you will find NUMA systems which are a sort of hybrid between an SMP system and a computing cluster .在一些高性能计算应用程序中,您会发现NUMA 系统,它是 SMP 系统和计算集群之间的一种混合体。 In a NUMA system, each CPU has its own dedicated memory area that it can access with low overhead;在 NUMA 系统中,每个 CPU 都有自己专用的 memory 区域,可以低开销访问; but each can also access the memory areas of its peers with somewhat more overhead.但每个人也可以访问其对等方的 memory 区域,但开销稍大。

what is meant by "core?" “核心”是什么意思?

SMP computers are so common these days, that many "CPU"/"processor" chips actually contain more than one CPU .如今,SMP 计算机如此普遍,以至于许多“CPU”/“处理器”芯片实际上包含多个 CPU When you hear "core," that usually means a single processor within a multi-processor chip.当您听到“核心”时,通常是指多处理器芯片中的单个处理器。

But note: These words have become a little bit ambiguous these days, so regardless of whether somebody says "CPU," or "core," or "processor," you might want to ask them to explain exactly what they mean.但请注意:这些词现在变得有点模棱两可,所以无论有人说“CPU”、“核心”还是“处理器”,您都可能想请他们准确解释它们的意思。

What is meant by..."virtual processor"?什么是......“虚拟处理器”?

As if it wasn't already complicated enough!好像还不够复杂!

Some CPUs these days have a feature called Hyper-threading .如今,一些 CPU 具有称为超线程的功能。 A hyper-threaded processor typically has two complete sets of context registers .超线程处理器通常具有两套完整的上下文寄存器 That makes it possible for the single CPU to instantly switch its attention back and forth between two different threads.这使得单个 CPU 可以立即在两个不同的线程之间来回切换其注意力 The CPU may also have duplicates of some of its most heavily used sub-systems, giving it some limited ability to execute instructions for two threads in parallel . CPU 也可能有一些使用最频繁的子系统的副本,这使其在并行执行两个线程的指令方面的能力有限。 Eg, it could be fetching memory operands on behalf of one thread while simultaneously performing register arithmetic on behalf of another.例如,它可以代表一个线程获取 memory 操作数,同时代表另一个线程执行寄存器算术。

From the operating system's point of view, a single hyper-threaded CPU appears as two "virtual" CPUs.从操作系统的角度来看,单个超线程 CPU 表现为两个“虚拟”CPU。 Although, depending on how many of its systems are duplicated, its overall performance could be anywhere on the spectrum from same as a single CPU to same as two complete CPUs.虽然,根据复制的系统数量,它的整体性能可能在范围内的任何地方,从与单个 CPU 相同到与两个完整 CPU 相同。

Virtual CPUs also sometimes are called, "hardware threads."虚拟 CPU 有时也被称为“硬件线程”。

Processor in java means a kernel, of which there may be n in the processor, they can also be virtual, well, there can also be> 1 processors. java中的处理器表示一个kernel,其中处理器中可能有n个,也可以是虚拟的,嗯,也可以>1个处理器。 There is a detailed diagram of how it works with volatile https://www.baeldung.com/java-volatile有一张关于它如何与 volatile https 一起工作的详细图表://www.baeldung.com/java-volatile

That article is innacurate in some way.那篇文章在某种程度上是不准确的。 It can be either processors, cores, virtual processors... Imagine that you have a cpu with 4 cores.它可以是处理器、内核、虚拟处理器……想象一下,你有一个有 4 个内核的 cpu。 Every core has it's own cache.每个核心都有自己的缓存。 Cpu can cache the value of the variable from the main memory into that cache. Cpu 可以将主 memory 中的变量值缓存到该缓存中。 Volatile use an instuction to write back any modification on that variable into the main memory, not just to the cache. Volatile 使用指令将对该变量的任何修改写回主 memory,而不仅仅是缓存。

It also forbid instuction reordering.它还禁止指令重新排序。

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

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