Skip to content
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

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

Open
wants to merge 21 commits into
base: master
Choose a base branch
from

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.

if (buffer.isEmpty()) {
bufferWait.wait();
}
}
Thread.sleep(5000); // Flush every 5 seconds.
Copy link
Owner

Choose a reason for hiding this comment

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

The main problem is this Thread.sleep(5000)

I would fix it with a scheduled executor, e.g.

private void startBufferFlusher() {
  ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
  scheduler.scheduleAtFixedRate(this::flushBuffer, 5, 5, TimeUnit.SECONDS);
}

}

Thread.sleep(1000);
Copy link
Owner

Choose a reason for hiding this comment

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

This busy-waiting loop can also be fixed by using ScheduledExecutorService in App.java

@@ -81,9 +85,16 @@ public static void main(String[] args) throws IOException {
}

private static void sessionExpirationTask() {
new Thread(() -> {
sessionExpirationThread = new Thread(() -> {
Copy link
Owner

Choose a reason for hiding this comment

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

Use ScheduledExecutorService to run this periodically

} 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

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