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 busy waiting in twin ballthread #3414

Open
omrajshinde534 wants to merge 1 commit into
iluwatar:masteriluwatar/java-design-patterns:masterfrom
omrajshinde534:fix-busy-waiting-2977omrajshinde534/java-design-patterns:fix-busy-waiting-2977Copy head branch name to clipboard
Open

fix busy waiting in twin ballthread #3414
omrajshinde534 wants to merge 1 commit into
iluwatar:masteriluwatar/java-design-patterns:masterfrom
omrajshinde534:fix-busy-waiting-2977omrajshinde534/java-design-patterns:fix-busy-waiting-2977Copy head branch name to clipboard

Conversation

@omrajshinde534
Copy link
Copy Markdown

This PR replaces polling-based suspension with a proper wait/notify
mechanism to eliminate busy-waiting and reduce unnecessary CPU usage.

Fixes #2977

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Dec 27, 2025

PR Summary

Converted the BallThread to use a synchronized wait/notify mechanism to replace the previous polling-based suspension. The thread now waits when suspended and wakes on resume, removing busy-waiting and reducing CPU usage. Stop also wakes the thread if it's waiting. Fixes #2977.

Changes

File Summary
twin/src/main/java/com/iluwatar/twin/BallThread.java Converted run() to synchronized and added a wait loop on isSuspended to replace busy-waiting. The thread now waits when suspended and resumes on notification, then performs twin.draw(), twin.move(), and a 250ms sleep. resumeMe() and stopMe() are synchronized to notify() waiting thread and ensure clean exit.

autogenerated by presubmit.ai

@omrajshinde534
Copy link
Copy Markdown
Author

It looks like the presubmit.ai review check is failing due to an external
AI model error (models/gemini-1.5-flash not found), unrelated to this PR.

Happy to make changes if required once the check is fixed.

@omrajshinde534
Copy link
Copy Markdown
Author

It looks like the Java PR Builder run was cancelled after a long execution time.
Earlier presubmit.ai also failed due to an external AI model error.

Please let me know if you’d like me to re-run the checks or make any changes.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 2, 2026

This PR is stale because it has been open 60 days with no activity.

@github-actions github-actions Bot added the Stale label Mar 2, 2026
@iluwatar iluwatar force-pushed the fix-busy-waiting-2977 branch from db485b8 to 2536587 Compare June 2, 2026 18:08
Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 Pull request needs attention.

Review Summary

Commits Considered (1)
  • 2536587: fix busy waiting in ballthread wait/notify
Files Processed (1)
  • twin/src/main/java/com/iluwatar/twin/BallThread.java (1 hunk)
Actionable Comments (1)
  • twin/src/main/java/com/iluwatar/twin/BallThread.java [63-66]

    maintainability: "Synchronize suspendMe() to ensure visibility"

Skipped Comments (1)
  • twin/src/main/java/com/iluwatar/twin/BallThread.java [49-51]

    maintainability: "Improve visibility of the suspension flag"

Comment on lines 63 to 66
public void suspendMe() {
isSuspended = true;
LOGGER.info("Begin to suspend BallThread");
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suspendMe() is currently not synchronized, while run() is. This can create visibility issues for isSuspended updates across threads. Make suspendMe() synchronized (or declare isSuspended as volatile) to ensure the suspension request is observed by the running thread.

Copy link
Copy Markdown
Owner

@iluwatar iluwatar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code has deadlocking issues, does not work

@github-actions github-actions Bot removed the Stale label Jun 4, 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.

Fix busy-waiting loops

2 participants

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