You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java volatile读可见性保障的多线程场景取值问询

Volatile Visibility Guarantee: What's the Value of p2.b at t=7?

First, let's recap the key JVM guarantee you referenced:

当线程A写入一个volatile变量,随后线程B读取同一个变量时,线程A在写入该volatile变量之前可见的所有变量的值,在线程B读取该volatile变量之后都将对B可见。

Let's walk through the execution timeline you provided to figure out what p2.b will print at t=7:

  • t=1: Th1 writes p1.a = 10 to its local working memory (main memory still holds the initial value a=1).
  • t=2: Th1 writes p1.b = 10 to its local working memory (main memory still has b=1).
  • t=3: Th2 writes p2.a = 30 to its local working memory (main memory remains a=1).
  • t=4: Th2 writes p2.b = 30 to its local working memory (main memory remains b=1).
  • t=5: Th1 writes p1.c = 10—since c is volatile, this triggers a critical JMM behavior: all shared variables in Th1's working memory (a=10, b=10, c=10) get flushed to main memory. Now main memory holds a=10, b=10, c=10.
  • t=6: Th2 runs int x = p2.c; (a volatile read). Here's what happens next per JVM rules:
    1. Th2's local working memory is marked invalid for all shared variables.
    2. Th2 must reload every shared variable from main memory. This overwrites Th2's local values: its working memory now has a=10, b=10, c=10 (replacing the a=30, b=30 it had stored earlier).
  • t=7: Th2 prints p2.b. Since its working memory now holds the main memory value 10 (reloaded at t=6), the output will be 10.

You might be wondering: what about Th2's write of b=30 at t=4? That change was only in Th2's local working memory and never flushed to main memory before the volatile read. When the volatile read forces a refresh from main memory, that unsynchronized local write gets overwritten by the latest valid value from main memory (set by Th1 at t=5).

To tie this to the happens-before relationship (the formal foundation of JVM visibility):

  • Th1's writes to a and b (t=1, t=2) happen-before Th1's write to volatile c (t=5) (per the program order rule: actions in the same thread are ordered by execution order).
  • Th1's write to c (t=5) happens-before Th2's read of c (t=6) (per the volatile variable rule: writes to a volatile variable happen-before all subsequent reads of that variable).
  • By transitivity, Th1's writes to a and b happen-before Th2's read of b (t=7). This enforces that Th2 sees Th1's values for a and b after reading the volatile c.

内容的提问来源于stack exchange,提问作者user3724707

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 18:32:40