From the course: Advanced Java: Threads and Concurrency

Unlock the full course today

Join today to access over 24,400 courses taught by industry experts.

Memory inconsistency: Data race

Memory inconsistency: Data race - Java Tutorial

From the course: Advanced Java: Threads and Concurrency

Memory inconsistency: Data race

- [Instructor] I think the best way to learn about the problems associated with how Java threads access memory is to visualize how the Java memory model can be mapped to the underlying hardware. Of course, when talking about the hardware architectures, they are different, but it's sufficient to consider a generic architecture to visualize what's going on here. So we have the CPU, or it could be CPU cores in which the program execution happens. I'm referring to them as CPU for convenience. Let's say the Thread1 is executed by CPU1 and Thread2 by CPU2. Within the CPU, there are CPU registers that are extremely fast, built-in memory, local to the CPU. Registers store data and/or instructions that the CPU is currently processing. Then there's the CPU cache memory that stores frequently or recently used data and instructions. Then we have the main memory or RAM, where the thread stacks and the heap are located. For example, when executing Thread1 in the CPU, it fetches the data associated…

Contents