We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents ed5f2b7 + c9ffd03 commit 10033d3Copy full SHA for 10033d3
1 file changed
docs/java/basis/unsafe.md
@@ -568,10 +568,12 @@ private void incrementAndPrint(int targetValue) {
568
return;
569
}
570
// 尝试 CAS 操作:如果当前值等于 targetValue - 1,则原子地设置为 targetValue
571
- if (unsafe.compareAndSwapInt(this, fieldOffset, currentValue, targetValue)) {
572
- // CAS 成功后立即打印,确保打印的就是本次设置的值
573
- System.out.print(targetValue + " ");
574
- return;
+ if (currentValue == targetValue - 1) {
+ if (unsafe.compareAndSwapInt(this, fieldOffset, currentValue, targetValue)) {
+ // CAS 成功后立即打印,确保打印的就是本次设置的值
+ System.out.print(targetValue + " ");
575
+ return;
576
+ }
577
578
// CAS 失败,重新读取并重试
579
0 commit comments