Skip to content

Fix busy-waiting loops #2977

Open
Open

Description

@iluwatar

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

Activity

  1. converted this from a draft issue on Jun 1, 2024
  2. TATIKONDAVENKATESH commented on Jul 1, 2024

    @TATIKONDAVENKATESH

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

  3. Mehdi-17 commented on Jul 2, 2024

    @Mehdi-17

    Hello, i'm available to work on it

  4. moved this from Todo to In Progress in Java Design Patterns projecton Jul 7, 2024
  5. Mehdi-17 commented on Jul 12, 2024

    @Mehdi-17

    Thanks, I've just started working on this.

  6. stale commented on Sep 11, 2024

    @stale

    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.

  7. added
    status: staleissues and pull requests that have not had recent interaction
    on Sep 11, 2024
  8. shalini-bhandari commented on Oct 1, 2024

    @shalini-bhandari

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

  9. 59 remaining items

  10. SakshiHanwat commented on Oct 31, 2025

    @SakshiHanwat

    “Hi! I’d love to work on this issue for Hacktoberfest. Could you please assign it to me?”

  11. tulsi-kumari commented on Nov 23, 2025

    @tulsi-kumari

    Hi Would like to solve this one, can you assign it to me?

  12. Varun5157 commented on Dec 22, 2025

    @Varun5157

    Hello maintainers,

    I’m a beginner Java developer and would like to work on this issue.
    I plan to start by identifying one of the busy-waiting loops mentioned
    and refactor it using a proper waiting mechanism.

    If this approach sounds good, please let me know.
    Any guidance on which file would be best to start with is appreciated.

    Thanks!

  13. NavyaNuthalapati commented on Jan 10, 2026

    @NavyaNuthalapati

    Hi @iluwatar!
    I am a student developer and I'm learning Java[Beginner Level]
    I would like to contribute by fixing a busy-waiting loop. Could you please guide me on which module I should look into, or can I pick any available one from the list above

  14. nasreenkhannam commented on Jan 20, 2026

    @nasreenkhannam

    Hello maintainers,

    I’m a beginner Java developer and I’m exploring this project.
    I’d like to understand the busy-waiting patterns better by looking at
    one small example.

    Could you please suggest a file or module that would be suitable
    for a beginner to start with?

    Thank you!

  15. Kaaldut8 commented on Mar 6, 2026

    @Kaaldut8

    Hi @iluwatar , I would like to work on this issue.
    I have started refactoring the busy-waiting loop in all the given locations.

  16. talon1126 commented on Mar 30, 2026

    @talon1126

    Hello, i'm available to work on it

  17. Yashgarg0101 commented on May 2, 2026

    @Yashgarg0101

    Hi, I would like to work on this issue. I am a beginner and would appreciate guidance.

  18. iluwatar commented on May 3, 2026

    @iluwatar
    OwnerAuthor

    This is a hard issue to get right. I wouldn't recommend this for a beginner @Yashgarg0101

  19. eric-muganga commented on May 8, 2026

    @eric-muganga

    Hello, I’m a beginner Java developer and would like to work on this issue.

  20. MohamedMBG commented on Jul 23, 2026

    @MohamedMBG
    Contributor

    @iluwatar i am working on this issue if it's free

  21. MohamedMBG commented on Jul 23, 2026

    @MohamedMBG
    Contributor

    #3548 here is the PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions