Skip to content

fix: replace busy waiting in BallThread - #3615

Open
Sakthinatchiyar wants to merge 6 commits into
iluwatar:masterfrom
Sakthinatchiyar:fix-busy-waiting-2977
Open

Sakthinatchiyar wants to merge 6 commits into
iluwatar:masterfrom
Sakthinatchiyar:fix-busy-waiting-2977

Conversation

@Sakthinatchiyar

Copy link
Copy Markdown

What does this PR do?

This PR replaces the busy-waiting behavior in BallThread with wait/notify for thread suspension and resumption.

Changes

  • Replaced polling of the suspended state with wait().
  • Added notify() when resuming the thread.
  • Updated stopMe() to safely wake the thread when stopping.
  • Preserved the existing 250 ms drawing interval.

Tests

  • .\mvnw.cmd -pl twin test
  • 7 tests passed
  • 0 failures
  • 0 errors

Related to #2977

@github-actions

github-actions Bot commented Oct 4, 2026 •

Copy link
Copy Markdown

PR Summary

Replaced busy-waiting in BallThread with a wait/notify-based suspension mechanism. Introduced a dedicated lock object, synchronized blocks to suspend/resume, and a wait loop in run() that responds to suspendMe()/resumeMe(). Safely wakes the thread on stopMe(), while preserving the 250 ms drawing interval. Added tests for stopping while suspended and interruption while suspended.

Changes

File Summary
twin/src/main/java/com/iluwatar/twin/BallThread.java Introduces a synchronization lock and wait/notify-based suspension: the thread now waits on lock while suspended, resumes with notify(), and safely wakes on stop; preserves 250 ms draw interval and handles interruptions gracefully.
twin/src/test/java/com/iluwatar/twin/BallThreadTest.java Adds tests for stopping a suspended BallThread and for interrupting a suspended BallThread, ensuring proper thread termination and no extra interactions.

autogenerated by presubmit.ai

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

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)
  • c6a0096: fix: replace busy waiting in BallThread
Files Processed (1)
  • twin/src/main/java/com/iluwatar/twin/BallThread.java (2 hunks)
Actionable Comments (1)
  • twin/src/main/java/com/iluwatar/twin/BallThread.java [49-56]

    best_practice: "Graceful handling of InterruptedException during wait"

Skipped Comments (3)
  • twin/src/main/java/com/iluwatar/twin/BallThread.java [43-43]

    best_practice: "Introduce a dedicated lock for suspend/resume coordination"

  • twin/src/main/java/com/iluwatar/twin/BallThread.java [79-85]

    best_practice: "Correct use of wait/notify in resumeMe"

  • twin/src/main/java/com/iluwatar/twin/BallThread.java [88-93]

    maintainability: "Graceful termination synchronization in stopMe"

Comment on lines +49 to +56
synchronized (lock) {
while (isSuspended && isRunning) {
try {
lock.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Wait-for-resume loop uses the lock to suspend, and handle InterruptedException gracefully. The current approach rethrows InterruptedException as a RuntimeException, which may crash the thread. Recommend re-interrupt and exit gracefully.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ LGTM!

Review Summary

Commits Considered (1)
  • 625f3c4: style: format BallThread
Files Processed (1)
  • twin/src/main/java/com/iluwatar/twin/BallThread.java (2 hunks)
Actionable Comments (0)
Skipped Comments (5)
  • twin/src/main/java/com/iluwatar/twin/BallThread.java [52-54]

    best_practice: "Graceful handling of InterruptedException in wait loop."

  • twin/src/main/java/com/iluwatar/twin/BallThread.java [82-82]

    best_practice: "Use notifyAll for waking waiting threads."

  • twin/src/main/java/com/iluwatar/twin/BallThread.java [91-91]

    best_practice: "Wake waiting threads on stop."

  • twin/src/main/java/com/iluwatar/twin/BallThread.java [65-65]

    enhancement: "Conditional sleep to align with running state."

  • twin/src/main/java/com/iluwatar/twin/BallThread.java [73-75]

    maintainability: "Ensure suspendMe uses lock for visibility."

@codecov

codecov Bot commented Oct 4, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.45455% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 84.06%. Comparing base (4cabb20) to head (92803fb).

Files with missing lines Patch % Lines
...in/src/main/java/com/iluwatar/twin/BallThread.java 95.45% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3615      +/-   ##
============================================
- Coverage     84.06%   84.06%   -0.01%     
+ Complexity     4353     4352       -1     
============================================
  Files          1133     1133              
  Lines         15400    15417      +17     
  Branches        739      740       +1     
============================================
+ Hits          12946    12960      +14     
  Misses         2158     2158              
- Partials        296      299       +3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ LGTM!

Review Summary

Commits Considered (1)
  • 8682c01: test: cover BallThread stop while suspended
Files Processed (1)
  • twin/src/test/java/com/iluwatar/twin/BallThreadTest.java (1 hunk)
Actionable Comments (0)
Skipped Comments (1)
  • twin/src/test/java/com/iluwatar/twin/BallThreadTest.java [118-139]

    possible issue: "Flaky test risk due to timing"

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ LGTM!

Review Summary

Commits Considered (1)
  • 5a2d0e9: fix: handle BallThread interruption gracefully
Files Processed (1)
  • twin/src/main/java/com/iluwatar/twin/BallThread.java (2 hunks)
Actionable Comments (0)
Skipped Comments (3)
  • twin/src/main/java/com/iluwatar/twin/BallThread.java [49-58]

    best_practice: "Wait/notify suspension loop uses a dedicated lock"

  • twin/src/main/java/com/iluwatar/twin/BallThread.java [89-93]

    enhancement: "Stop path wake behavior when thread is sleeping"

  • twin/src/main/java/com/iluwatar/twin/BallThread.java [52-56]

    best_practice: "Graceful handling of InterruptedException in wait loop"

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ LGTM!

Review Summary

Commits Considered (1)
  • 0bf6651: test: cover BallThread interruption while suspended
Files Processed (1)
  • twin/src/test/java/com/iluwatar/twin/BallThreadTest.java (1 hunk)
Actionable Comments (0)
Skipped Comments (2)
  • twin/src/test/java/com/iluwatar/twin/BallThreadTest.java [118-139]

    maintainability: "Unreliable timing in testStopWhileSuspended."

  • twin/src/test/java/com/iluwatar/twin/BallThreadTest.java [141-162]

    maintainability: "Unreliable timing in testInterruptWhileSuspended."

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ LGTM!

Review Summary

Commits Considered (1)
  • 92803fb: test: wait for suspended BallThread state
Files Processed (1)
  • twin/src/test/java/com/iluwatar/twin/BallThreadTest.java (1 hunk)
Actionable Comments (0)
Skipped Comments (2)
  • twin/src/test/java/com/iluwatar/twin/BallThreadTest.java [118-145]

    possible bug: "Potential flakiness when detecting suspension via Thread.State.WAITING."

  • twin/src/test/java/com/iluwatar/twin/BallThreadTest.java [147-168]

    possible bug: "Interruption handling during suspension could be fragile if interruption state is swallowed."

This branch has not been deployed

No deployments
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.

1 participant