Skip to content

Fixed busy waiting in points 1,2,3,7 #3101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

alhusseain
Copy link

@alhusseain alhusseain commented Nov 30, 2024

Close issue #2977
Implemented wait and notify functionalities on threads that have busy waiting issues

No. 1 : Server Session / App.java

sessionExpirationTask now waits until at least a single login request is sent, since there would be unnecessary overhead if it runs with no login requests available.

No. 2 : Twin / BallThread.java

run() now waits if suspendMe is called, and resumes if resumeMe is called, since there would be unnecessary overhead if run() is running when ball item is suspended. ( no need to always check if suspended)

No. 3: Log Aggregation / LogAggregator.java
startBufferFlusher runs if there is at least one log is present, similiar to No.1's fix

No. 7: Queue-Based Load Leveling / ServiceExecutor.java

similiar to No.3 and No.1, at least a single msg is added to queue for it to work, until then run() waits.

I did not refactor No. 4,5,6 since I think no unnecessary overhead is present ( as the retry pattern implemented loops until certain number of errors occur, doesn't loop needlessly)

I would much appreciate your feedback, which I would consider and act upon quickly.

} else {
twin.draw();
twin.move();
Thread.sleep(250);
Copy link
Owner

Choose a reason for hiding this comment

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

This Thread.sleep(250) is the main problem. Instead of using a thread, we could utilize the same aforementioned ScheduledExecutorService

Copy link
Author

Choose a reason for hiding this comment

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

On it, Thanks for the feedback

Copy link
Author

@alhusseain alhusseain Feb 18, 2025

Choose a reason for hiding this comment

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

I have implemented all requested changes except only in BallThread.java. The only change was removing sleep since it is unnecessary. In BallThread.java, There is no need to run it every fixed amount of time. Instead, waking the thread up only when there is a request to resume and making it wait when there is a request to suspend is sufficient to reduce overhead. This way, I implement event-based logic as requested in the issue.

Copy link

github-actions bot commented Feb 18, 2025

PR Summary

This PR addresses busy-waiting issues in several Java applications by implementing wait/notify mechanisms. The changes prevent unnecessary overhead in threads by making them wait until work is available, improving efficiency and resource utilization. This involved adding synchronization primitives and modifying thread execution logic across multiple files.

Changes

File Summary
microservices-log-aggregation/src/main/java/com/iluwatar/logaggregation/LogAggregator.java The startBufferFlusher method is modified to wait until at least one log entry is present in the buffer before starting to flush. A bufferWait object is introduced for synchronization, and a bufferWake method is added to notify the waiting thread when a new log entry is added.
microservices-log-aggregation/src/test/java/com/iluwatar/logaggregation/LogAggregatorTest.java Added tests to verify the behavior of the LogAggregator class, particularly focusing on the new wait/notify mechanism implemented to prevent busy-waiting. The tests cover scenarios where the buffer is empty and when it contains log entries.
queue-based-load-leveling/src/main/java/com/iluwatar/queue/load/leveling/App.java The main application now uses ScheduledExecutorService to schedule the ServiceExecutor with a fixed rate, ensuring it runs periodically to process messages. This change enhances the application's responsiveness and efficiency.
queue-based-load-leveling/src/main/java/com/iluwatar/queue/load/leveling/MessageQueue.java Added a serviceExecutorWait object to synchronize access to the queue and notify the ServiceExecutor when a new message is added. This prevents the ServiceExecutor from constantly checking for messages.
queue-based-load-leveling/src/main/java/com/iluwatar/queue/load/leveling/ServiceExecutor.java The run method now waits using wait() if the queue is empty, preventing busy-waiting. It's woken up by notifyAll() when a new message is added to the queue. This significantly improves efficiency.
queue-based-load-leveling/src/main/java/com/iluwatar/queue/load/leveling/TaskGenerator.java Minor change to improve code clarity and maintain consistency with other parts of the application. No functional changes were made.
queue-based-load-leveling/src/test/java/com/iluwatar/queue/load/leveling/TaskGenSrvExeTest.java Added tests to verify the ServiceExecutor's behavior, specifically its ability to wait when the queue is empty and resume execution when a message is added. This ensures the fix for busy-waiting works as intended.
server-session/README.md The README is updated to reflect the changes made to the App class, specifically the addition of a wait/notify mechanism to prevent busy-waiting in the session expiration task. The updated documentation clarifies the improved efficiency and resource utilization.
server-session/pom.xml Removed an extra newline at the end of the file. This is a minor change that improves the file's formatting and readability.
server-session/src/main/java/com/iluwatar/sessionserver/App.java The sessionExpirationTask now waits using wait() if there are no active sessions, preventing busy-waiting. It's woken up by notify() when a new login request is received. The task is now scheduled using ScheduledExecutorService.
server-session/src/main/java/com/iluwatar/sessionserver/LoginHandler.java Added documentation to clarify the purpose of the LoginHandler class. No functional changes were made.
server-session/src/main/java/com/iluwatar/sessionserver/LogoutHandler.java Added documentation to clarify the purpose of the LogoutHandler class. No functional changes were made.
twin/README.md The README is updated to reflect the changes made to the BallThread class, specifically the addition of a wait/notify mechanism to prevent busy-waiting. The updated documentation clarifies the improved efficiency and resource utilization.
twin/src/main/java/com/iluwatar/twin/BallThread.java The run method now waits using wait() if isSuspended is true, preventing busy-waiting. It's woken up by notify() when resumeMe is called. This significantly improves efficiency.
twin/src/test/java/com/iluwatar/twin/BallThreadTest.java Added tests to verify the BallThread's behavior, specifically its ability to wait when suspended and resume execution when resumed. This ensures the fix for busy-waiting works as intended.

autogenerated by presubmit.ai

Copy link

@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 (23)
  • fa902ad: Merge remote-tracking branch 'origin/busy-looping---Alhusseain' into busy-looping---Alhusseain
  • 1c04548: implemented review changes
  • 18a4c02: Merge branch 'master' into busy-looping---Alhusseain
  • b5073d6: LogoutHandlerTest correct
  • 7afa9c8: LogoutHandlerTest correct
  • 4751cb8: Add Service Executor Start and Wake State Test
  • d0e0d87: Add Service Executor Start and Wake State Test
  • 30d6eb3: Add BallThread resume and suspend state test
  • 7be96ca: Add Expiration Task state test (should be sleeping after it is woken)
  • e31b12b: Add Expiration Task state test (should be sleeping after it is woken)
  • 986130a: Add Expiration Task state test (should be waiting at the start)
  • fca409f: Add Expiration Task state test (should be waiting at the start)
  • a8fb4b2: Merge remote-tracking branch 'origin/busy-looping---Alhusseain' into busy-looping---Alhusseain
  • d84dc96: SonarQube fix try 4 (Reliability and coverage in twin)
  • ffd4773: Merge branch 'master' into busy-looping---Alhusseain
  • ec0d423: SonarQube fix try 3
  • 992d899: SonarQube fix try 2
  • 2109002: SonarQube fix try 1
  • 27325f2: Checkstyle fix try 3
  • 42e4c32: Checkstyle fix try 2
  • 9415119: Checkstyle fix try 1
  • 58e5c62: Fixed busy waiting in points 1,2,3,7 (1)
  • f20a582: Fixed busy waiting in points 1,2,3,7
Files Processed (15)
  • microservices-log-aggregation/src/main/java/com/iluwatar/logaggregation/LogAggregator.java (4 hunks)
  • microservices-log-aggregation/src/test/java/com/iluwatar/logaggregation/LogAggregatorTest.java (1 hunk)
  • queue-based-load-leveling/src/main/java/com/iluwatar/queue/load/leveling/App.java (2 hunks)
  • queue-based-load-leveling/src/main/java/com/iluwatar/queue/load/leveling/MessageQueue.java (2 hunks)
  • queue-based-load-leveling/src/main/java/com/iluwatar/queue/load/leveling/ServiceExecutor.java (2 hunks)
  • queue-based-load-leveling/src/main/java/com/iluwatar/queue/load/leveling/TaskGenerator.java (1 hunk)
  • queue-based-load-leveling/src/test/java/com/iluwatar/queue/load/leveling/TaskGenSrvExeTest.java (2 hunks)
  • server-session/README.md (2 hunks)
  • server-session/pom.xml (1 hunk)
  • server-session/src/main/java/com/iluwatar/sessionserver/App.java (3 hunks)
  • server-session/src/main/java/com/iluwatar/sessionserver/LoginHandler.java (2 hunks)
  • server-session/src/main/java/com/iluwatar/sessionserver/LogoutHandler.java (1 hunk)
  • twin/README.md (2 hunks)
  • twin/src/main/java/com/iluwatar/twin/BallThread.java (1 hunk)
  • twin/src/test/java/com/iluwatar/twin/BallThreadTest.java (4 hunks)
Actionable Comments (1)
  • twin/src/main/java/com/iluwatar/twin/BallThread.java [78-84]

    possible bug: "Potential deadlock in thread synchronization."

Skipped Comments (0)

Copy link

@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)
  • dbf6a7b: implemented review changes
Files Processed (14)
  • microservices-log-aggregation/src/main/java/com/iluwatar/logaggregation/LogAggregator.java (4 hunks)
  • microservices-log-aggregation/src/test/java/com/iluwatar/logaggregation/LogAggregatorTest.java (1 hunk)
  • queue-based-load-leveling/src/main/java/com/iluwatar/queue/load/leveling/App.java (2 hunks)
  • queue-based-load-leveling/src/main/java/com/iluwatar/queue/load/leveling/MessageQueue.java (2 hunks)
  • queue-based-load-leveling/src/main/java/com/iluwatar/queue/load/leveling/ServiceExecutor.java (2 hunks)
  • queue-based-load-leveling/src/main/java/com/iluwatar/queue/load/leveling/TaskGenerator.java (1 hunk)
  • queue-based-load-leveling/src/test/java/com/iluwatar/queue/load/leveling/TaskGenSrvExeTest.java (2 hunks)
  • server-session/pom.xml (1 hunk)
  • server-session/src/main/java/com/iluwatar/sessionserver/App.java (3 hunks)
  • server-session/src/main/java/com/iluwatar/sessionserver/LoginHandler.java (2 hunks)
  • server-session/src/main/java/com/iluwatar/sessionserver/LogoutHandler.java (1 hunk)
  • twin/README.md (2 hunks)
  • twin/src/main/java/com/iluwatar/twin/BallThread.java (1 hunk)
  • twin/src/test/java/com/iluwatar/twin/BallThreadTest.java (5 hunks)
Actionable Comments (1)
  • server-session/src/main/java/com/iluwatar/sessionserver/App.java [91-108]

    bug: "Infinite loop in session expiration task."

Skipped Comments (0)

@alhusseain alhusseain requested a review from iluwatar February 18, 2025 19:03
Copy link

@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.

LGTM!

Review Summary

Commits Considered (1)
  • 9671120: Merge branch 'master' into busy-looping---Alhusseain
Files Processed (0)
Actionable Comments (0)
Skipped Comments (0)

Copy link

@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.

LGTM!

Review Summary

Commits Considered (2)
  • e4d0f53: Merge remote-tracking branch 'origin/busy-looping---Alhusseain' into busy-looping---Alhusseain
  • 8407245: implemented review changes
Files Processed (1)
  • server-session/src/main/java/com/iluwatar/sessionserver/App.java (3 hunks)
Actionable Comments (0)
Skipped Comments (1)
  • server-session/src/main/java/com/iluwatar/sessionserver/App.java [87-88]

    enhancement: "Improve scheduler shutdown handling."

Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
68.4% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

@iluwatar
Copy link
Owner

I'm sorry, but it doesn't look correct. The solution needs a complete rewrite, so I'll close this.

@iluwatar iluwatar closed this Feb 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants