iluwatar / iluwatar/java-design-patterns
Fix busy-waiting loops
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 94.7k
- Forks
- 27.4k
- Avg merge
- 3d 4h
- Merged PRs (30d)
- 10
Description
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:
- 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.
- 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.
- 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.
- 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:
- Server Session / App.java
- Twin / BallThread.java
- Log Aggregation / LogAggregator.java
- Commander / Retry.java
- Retry / Retry.java
- Retry / RetryExponentialBackoff.java
- Queue-Based Load Leveling / ServiceExecutor.java
Acceptance Criteria
- Busy-waiting loops refactored
- README.md of the affected patterns revised as needed
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Inspect the busy-waiting loops in Server Session/App.java, Twin/BallThread.java, Log Aggregation/LogAggregator.java, Commander/Retry.java, Retry/Retry.java, Retry/RetryExponentialBackoff.java, and Queue-Based Load Leveling/ServiceExecutor.java. Review each affected pattern's README.md and determine how the loop should wait without spinning. Done means the listed loops are refactored and the affected README files are revised as needed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- performance
- Issue type
- Refactor
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100