java-design-patterns icon indicating copy to clipboard operation
java-design-patterns copied to clipboard

Fix busy-waiting loops

Open iluwatar opened this issue 1 year ago • 42 comments

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

iluwatar avatar Jun 01 '24 12:06 iluwatar

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

TATIKONDAVENKATESH avatar Jul 01 '24 09:07 TATIKONDAVENKATESH

Hello, i'm available to work on it

Mehdi-17 avatar Jul 02 '24 19:07 Mehdi-17

Thanks, I've just started working on this.

Mehdi-17 avatar Jul 12 '24 23:07 Mehdi-17

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[bot] avatar Sep 11 '24 04:09 stale[bot]

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

shalini-bhandari avatar Oct 01 '24 08:10 shalini-bhandari

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

AdityaBhendavadekar avatar Oct 06 '24 19:10 AdityaBhendavadekar

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 .

keshavMM004 avatar Oct 07 '24 10:10 keshavMM004

/assign

HemantBatra873 avatar Oct 11 '24 07:10 HemantBatra873

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

RajathKP avatar Oct 12 '24 18:10 RajathKP

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.

masahiro0000 avatar Oct 14 '24 05:10 masahiro0000

Hi @iluwatar I would like to work on this project

4nant-5 avatar Oct 16 '24 17:10 4nant-5

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

Abimbola-Jolayemi avatar Oct 17 '24 14:10 Abimbola-Jolayemi

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

Aariz-786 avatar Oct 18 '24 09:10 Aariz-786

Whats happening here ?

akash-yadagouda avatar Oct 25 '24 19:10 akash-yadagouda

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.

alhusseain avatar Nov 26 '24 12:11 alhusseain

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!

Basmalamoustafa avatar Nov 26 '24 15:11 Basmalamoustafa

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!

malak-elbanna avatar Nov 26 '24 21:11 malak-elbanna

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

Karim-Ashraf1 avatar Nov 29 '24 22:11 Karim-Ashraf1

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

ZeyadWaleed7 avatar Dec 01 '24 12:12 ZeyadWaleed7

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

alhusseain avatar Dec 01 '24 21:12 alhusseain

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

Basmala27 avatar Dec 02 '24 18:12 Basmala27

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

mariamsamaha avatar Dec 02 '24 18:12 mariamsamaha

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

Mostafa-Hisham0 avatar Dec 04 '24 17:12 Mostafa-Hisham0

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

TevaTavo avatar Dec 06 '24 15:12 TevaTavo

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.

PlumHeadd avatar Dec 06 '24 18:12 PlumHeadd

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

sama-eldakkash avatar Dec 07 '24 10:12 sama-eldakkash

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!

s-pakinamkhaled avatar Dec 07 '24 10:12 s-pakinamkhaled

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.

BasmalaAmr1 avatar Dec 07 '24 15:12 BasmalaAmr1

@iluwatar Thank you for assigning me the issue :) . I have already submitted a pull request. link

alhusseain avatar Dec 17 '24 01:12 alhusseain

This issue is stale because it has been open 60 days with no activity.

github-actions[bot] avatar Feb 17 '25 02:02 github-actions[bot]