简体   繁体   中英

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/

It seems to make sense that volatile means: that the changes made in one thread are immediately reflected in other thread

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."

  • My question is how can their be two processors? Are these part of the same 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"

how can their be two processors? Are these part of the same CPU?

"Processor" usually is a synonym for CPU. Most desktop computers, most servers, and even most cell phones and tablets these days have more than one CPU. Typically they comprise a symmetric multiprocessor system (SMP) in which all of the CPUs share the same common memory system.

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 . In a NUMA system, each CPU has its own dedicated memory area that it can access with low overhead; but each can also access the memory areas of its peers with somewhat more overhead.

what is meant by "core?"

SMP computers are so common these days, that many "CPU"/"processor" chips actually contain more than one 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.

What is meant by..."virtual processor"?

As if it wasn't already complicated enough!

Some CPUs these days have a feature called Hyper-threading . 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. 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 . Eg, it could be fetching memory operands on behalf of one thread while simultaneously performing register arithmetic on behalf of another.

From the operating system's point of view, a single hyper-threaded CPU appears as two "virtual" CPUs. 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.

Virtual CPUs also sometimes are called, "hardware threads."

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. There is a detailed diagram of how it works with 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. Every core has it's own cache. Cpu can cache the value of the variable from the main memory into that cache. Volatile use an instuction to write back any modification on that variable into the main memory, not just to the cache.

It also forbid instuction reordering.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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