Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

fix: Issue#2650 - 修复CAS示例代码的并发打印livelock问题#2799

Closed
Senrian wants to merge 9 commits into
Snailclimb:mainSnailclimb/JavaGuide:mainfrom
Senrian:masterSenrian/JavaGuide:masterCopy head branch name to clipboard
Closed

fix: Issue#2650 - 修复CAS示例代码的并发打印livelock问题#2799
Senrian wants to merge 9 commits into
Snailclimb:mainSnailclimb/JavaGuide:mainfrom
Senrian:masterSenrian/JavaGuide:masterCopy head branch name to clipboard

Conversation

@Senrian

@Senrian Senrian commented Mar 20, 2026

Copy link
Copy Markdown
Contributor

修复 Issue #2650

问题

现有代码使用 Thread.yield()currentValue == targetValue - 1 条件存在 livelock 风险:当线程读取值后被其他线程抢先更新,当前线程的 CAS 会永远失败。

解决方案

  1. 将跳过条件改为 currentValue >= targetValue,避免 livelock
  2. System.out.print 移入 CAS 成功块内,确保打印值与设置值一致
  3. 移除 Thread.yield() 避免不必要的 CPU 让出

@Snailclimb

Copy link
Copy Markdown
Owner

修复 Issue #2650

问题

现有代码使用 Thread.yield()currentValue == targetValue - 1 条件存在 livelock 风险:当线程读取值后被其他线程抢先更新,当前线程的 CAS 会永远失败。

解决方案

  1. 将跳过条件改为 currentValue >= targetValue,避免 livelock
  2. System.out.print 移入 CAS 成功块内,确保打印值与设置值一致
  3. 移除 Thread.yield() 避免不必要的 CPU 让出

很用心,但是这个分支好像弄错了,应该是main分支哈~

@Senrian Senrian closed this Mar 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.