fix busy waiting in twin ballthread #3414
fix busy waiting in twin ballthread #3414omrajshinde534 wants to merge 1 commit intoiluwatar:masteriluwatar/java-design-patterns:masterfrom omrajshinde534:fix-busy-waiting-2977omrajshinde534/java-design-patterns:fix-busy-waiting-2977Copy head branch name to clipboard
Conversation
PR SummaryConverted 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
autogenerated by presubmit.ai |
|
It looks like the presubmit.ai review check is failing due to an external Happy to make changes if required once the check is fixed. |
|
It looks like the Java PR Builder run was cancelled after a long execution time. Please let me know if you’d like me to re-run the checks or make any changes. |
|
This PR is stale because it has been open 60 days with no activity. |
db485b8 to
2536587
Compare
There was a problem hiding this comment.
🚨 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"
| public void suspendMe() { | ||
| isSuspended = true; | ||
| LOGGER.info("Begin to suspend BallThread"); | ||
| } |
There was a problem hiding this comment.
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.
iluwatar
left a comment
There was a problem hiding this comment.
The code has deadlocking issues, does not work
This PR replaces polling-based suspension with a proper wait/notify
mechanism to eliminate busy-waiting and reduce unnecessary CPU usage.
Fixes #2977