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

Fix busy-waiting loops #2977

Open
iluwatar opened this issue Jun 1, 2024 · 28 comments
Open

Fix busy-waiting loops #2977

iluwatar opened this issue Jun 1, 2024 · 28 comments

Comments

@iluwatar
Copy link
Owner

iluwatar commented Jun 1, 2024

Description

Busy-waiting, or spinning, is a technique where a process repeatedly checks to see if a condition is true, such as whether keyboard input or a lock is available. While it might seem like a good idea, it has several significant drawbacks:

  1. CPU Usage: Busy-waiting can consume a lot of CPU time. While a process is busy-waiting, it keeps the CPU busy. This can prevent other processes from running and can lead to high CPU usage, especially if the condition being checked doesn't become true for a long time.
  2. Performance: Busy-waiting can lead to performance issues. If a process is busy-waiting, it's not doing any useful work. This can slow down the overall performance of your application.
  3. Responsiveness: Busy-waiting can make your application less responsive. If a process is busy-waiting, it might not be able to respond to user input or other events in a timely manner.
  4. Power Consumption: Busy-waiting can lead to increased power consumption, especially on battery-powered devices. This is because the CPU is kept busy and is not allowed to enter a low-power state.

Instead of busy-waiting, it's generally better to use some form of event-driven programming or blocking. This allows your process to sleep until the condition it's waiting for becomes true, which can save CPU time, improve performance, and make your application more responsive.

Busy-waiting loops are at least in these locations:

  1. Server Session / App.java
  2. Twin / BallThread.java
  3. Log Aggregation / LogAggregator.java
  4. Commander / Retry.java
  5. Retry / Retry.java
  6. Retry / RetryExponentialBackoff.java
  7. Queue-Based Load Leveling / ServiceExecutor.java

Acceptance Criteria

  • Busy-waiting loops refactored
  • README.md of the affected patterns revised as needed
@TATIKONDAVENKATESH
Copy link

i am new here, i want to contribute to this project

@Mehdi-17
Copy link

Mehdi-17 commented Jul 2, 2024

Hello, i'm available to work on it

@Mehdi-17
Copy link

Thanks, I've just started working on this.

Copy link

stale bot commented Sep 11, 2024

This issue has been automatically marked as stale because it has not had recent activity. The issue will be unassigned if no further activity occurs. Thank you for your contributions.

@stale stale bot added the status: stale issues and pull requests that have not had recent interaction label Sep 11, 2024
@shalini-bhandari
Copy link

Hello, this is my first time in open source.
Is this issue still open? and how to go about it?

@stale stale bot removed the status: stale issues and pull requests that have not had recent interaction label Oct 1, 2024
@iluwatar iluwatar moved this from In Progress to Todo in Java Design Patterns project Oct 6, 2024
@AdityaBhendavadekar
Copy link

hey @iluwatar i want to work on this. please assign this issue to me

@keshavMM004
Copy link

Hey @iluwatar i am new to open souce but exited a lot about it . I am good in java , threads , synchronization, deadlocks , starvation and have studied this in operating systems also . I really want to contribute and work upon this . Please assign this to me . I will be very active as this will be my first pull request for hacktober .

@HemantBatra873
Copy link

/assign

@RajathKP
Copy link

Hi @iluwatar
I would like to work on this issue. please assign this issue to me.

@masahiro0000
Copy link

masahiro0000 commented Oct 14, 2024

I’m currently working on this issue.
I believe I have found a solution to this problem.
For example, in the case of App.java, I’d like to propose the following solution.
◼︎Before change
new Thread(() -> { while (true) {

◼︎After change
private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(() -> {

Change of benefits
・Reduced CPU Usage: The thread no longer continuously runs in a busy-waiting loop.
・Better Performance: The session expiration check runs only when needed, making the application more efficient.
・Improved Responsiveness: The main thread is free from unnecessary loops and can better handle incoming requests.

Could you please review my proposed solution and let me know if you have any feedback?
Feel free to assign the issue to me.

PS Sorry for google translate.

@4nant-5
Copy link

4nant-5 commented Oct 16, 2024

Hi @iluwatar I would like to work on this project

@Abimbola-Jolayemi
Copy link

Hello @iluwatar . I have been able to study part of the code implementation. And, I will like to work on this issue.

@Aariz-786
Copy link

Hi!, @iluwatar . I'm interested in this issue please assign this issue to me .

@akash-yadagouda
Copy link

Whats happening here ?

@alhusseain
Copy link

alhusseain commented Nov 26, 2024

Hi @iluwatar. My first guess is that this issue would be best solved by utilising wait, notify, and other concurrency Java libraries. I am free this week to check out and refactor mentioned files. I would appreciate it if you assigned me this task. Much thanks.

@Basmalamoustafa
Copy link

Hello @iluwatar, I would love the opportunity to work on this issue. I have some ideas to reduce constant CPU usage effectively and improve the overall efficiency.
Could you please assign it to me?
Thank you!

@malak-elbanna
Copy link

Hi @iluwatar, I’m interested in tackling the busy-waiting issue in the repo. I'll try to replace busy-waiting with efficient alternatives like wait()/notify() or ScheduledExecutorService while ensuring thread safety and improved performance.
Could you please assign it to me? Thank you!

@Karim-Ashraf1
Copy link

Hi @iluwatar, I want to contribute to this issue could you please assign it to me ?

@ZeyadWaleed7
Copy link

Hello @iluwatar , I would like to work on this issue. Could you please assign it to me?
Thank you

@alhusseain
Copy link

I have submitted a pull request. Would appreciate your review @iluwatar

@Basmala27
Copy link

Hello @iluwatar I would love to work on this issue, would you please assign it to me?

@mariamsamaha
Copy link

Hello @iluwatar I want to contribute to this issue, Could you please assign it to me?
Thank you

@Mostafa-Hisham0
Copy link

Hello @iluwatar , I would like to work on this issue. Could you please assign it to me?
Thank you

@TevaTavo
Copy link

TevaTavo commented Dec 6, 2024

Hi @iluwatar , I would like to work on this issue. Could you please assign it to me?

@PlumHeadd
Copy link

PlumHeadd commented Dec 6, 2024

Hi @iluwatar,

I’d like to work on this issue as it aligns with my current learning goals in improving Java code quality and refactoring techniques. I understand the impact of busy-waiting on CPU usage, performance, and responsiveness, as outlined in the issue description.

My plan for addressing this issue includes:

  1. Refactoring the identified busy-waiting loops in App.java, Retry.java, and other affected files.
  2. Replacing the busy-waiting logic with event-driven or blocking mechanisms (e.g., wait()/notify() or condition variables), ensuring efficient CPU utilization and responsiveness.
  3. Updating the README.md to reflect the changes in affected patterns, if required.
  4. Testing the modifications thoroughly to ensure functionality is not disrupted.
  5. Please let me know if there are any additional guidelines or expectations for this task. I’m eager to contribute and would appreciate the opportunity to work on it!

Thank you.

@sama-eldakkash
Copy link

Hello @iluwatar, I’m interested in working on this issue ,Could you please assign it to me?

@s-pakinamkhaled
Copy link

Hello @iluwatar, I would love the opportunity to contribute and work on fixing this issue. Could you kindly assign it to me so I can get started?
Thank you for considering my request, and I look forward to your response!

@BasmalaAmr1
Copy link

BasmalaAmr1 commented Dec 7, 2024

Hello @iluwatar ,
I would like to work on this issue and contribute to resolving the busy-waiting loops mentioned. I can refactor the affected areas by implementing efficient alternatives such as event-driven programming or proper blocking mechanisms. These changes will help reduce CPU usage, improve performance, and enhance responsiveness.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: In Progress
Development

Successfully merging a pull request may close this issue.