From 87d61b59523f5106daba1098a1c6644c058ef9b3 Mon Sep 17 00:00:00 2001 From: Senrian <47714364+Senrian@users.noreply.github.com> Date: Tue, 14 Apr 2026 12:30:23 +0800 Subject: [PATCH] fix: correct volatile description for AQS state variable (issue #2516) The previous description only mentioned thread visibility as the reason for using volatile to modify the state variable. However, volatile's more important role here is preventing instruction reordering through the happens-before rule (volatile write happens-before subsequent read), which ensures the correctness of lock semantics. Fixes #2516 --- docs/java/concurrent/aqs.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/java/concurrent/aqs.md b/docs/java/concurrent/aqs.md index 8f45336ebbc..19c8744c7f0 100644 --- a/docs/java/concurrent/aqs.md +++ b/docs/java/concurrent/aqs.md @@ -106,10 +106,10 @@ AQS(`AbstractQueuedSynchronizer`)的核心原理图: AQS 使用 **int 成员变量 `state` 表示同步状态**,通过内置的 **FIFO 线程等待/等待队列** 来完成获取资源线程的排队工作。 -`state` 变量由 `volatile` 修饰,用于展示当前临界资源的获取情况。 +`state` 变量由 `volatile` 修饰,用于展示当前临界资源的获取情况。这里 `volatile` 的作用不仅仅是保证可见性,更重要的是通过 happens-before 规则(volatile 变量的写操作先行发生于后续的读操作)防止编译器和处理器对指令进行重排序,从而保证锁语义的正确性。 ```java -// 共享变量,使用volatile修饰保证线程可见性 +// 共享变量,使用volatile修饰,保证线程可见性并防止指令重排序 private volatile int state; ```