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

Improving loops in BallThread.java & updating its README.md. #3212

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

Conversation

Francisco-G-P
Copy link
Contributor

Pull Request Template

What does this PR do?

This pull request solves the issue of unnecessary CPU usage, as it also updates the README.md, of the file BallThread.java.

Changes apply to Issue #2977 (Fix busy-waiting loops); already existing issue regarding the resources consumption due to inefficient loops.

All the best,
Francisco!

Copy link

github-actions bot commented Mar 18, 2025

PR Summary

This PR improves the efficiency of the BallThread class by fixing busy-waiting loops and adds a Spanish translation for the MapReduce README. The updated BallThread uses synchronization to prevent unnecessary CPU usage.

Changes

File Summary
localization/es/map-reduce/README.md This file contains a Spanish translation of the MapReduce README. It provides a detailed explanation of the MapReduce design pattern, including its intent, real-world examples, Java code implementation, advantages, disadvantages, and related patterns.
twin/README.md Minor updates to the README, including improved formatting and minor wording changes for better clarity and consistency. No functional changes were made.
twin/src/main/java/com/iluwatar/twin/BallThread.java The BallThread class was updated to use synchronization mechanisms (synchronized, wait, notifyAll) to handle thread suspension and improve responsiveness. The isSuspended variable is no longer volatile, and the stopMe method now ensures a clean exit.

autogenerated by presubmit.ai

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

🚨 Pull request needs attention.

Review Summary

Commits Considered (2)
  • 9f34b25: Applied synchronization to prevent CPU usage.
  • b29934d: Added and translated map-reduce README.md to Spanish.
Files Processed (3)
  • localization/es/map-reduce/README.md (1 hunk)
  • twin/README.md (4 hunks)
  • twin/src/main/java/com/iluwatar/twin/BallThread.java (3 hunks)
Actionable Comments (3)
  • twin/src/main/java/com/iluwatar/twin/BallThread.java [73-55]

    possible bug: "Potential for missed wakeup signals in suspendMe() method."

  • twin/src/main/java/com/iluwatar/twin/BallThread.java [78-80]

    possible bug: "Potential for missed wakeup signals in resumeMe() method."

  • twin/src/main/java/com/iluwatar/twin/BallThread.java [84-87]

    possible bug: "Potential for missed wakeup signals in stopMe() method."

Skipped Comments (0)

while (isRunning) {
while (isSuspended) {
try {
wait(); // Puts the thread in waiting state.

Choose a reason for hiding this comment

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

The wait() method should be called inside a loop to handle spurious wakeups. The current implementation might miss wakeup signals, leading to unexpected behavior.

Comment on lines 78 to 80
public synchronized void resumeMe() {
isSuspended = false;
LOGGER.info("Begin to resume BallThread");
notify(); // Wakes up the thread from waiting state.

Choose a reason for hiding this comment

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

The notify() method should be called inside a loop to handle spurious wakeups. The current implementation might miss wakeup signals, leading to unexpected behavior.

Comment on lines 84 to 87
public synchronized void stopMe() {
isRunning = false;
isSuspended = false;
notify(); // Makes sure the thread wakes up and exits (stops).

Choose a reason for hiding this comment

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

The notify() method should be called inside a loop to handle spurious wakeups. The current implementation might miss wakeup signals, leading to unexpected behavior.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

🚨 Pull request needs attention.

Review Summary

Commits Considered (1)
  • dd45cb0: Synchronization updated thanks to GitHub Actions.
Files Processed (2)
  • twin/README.md (4 hunks)
  • twin/src/main/java/com/iluwatar/twin/BallThread.java (3 hunks)
Actionable Comments (1)
  • twin/src/main/java/com/iluwatar/twin/BallThread.java [51-60]

    possible bug: "Potential deadlock in BallThread.java"

Skipped Comments (0)

Comment on lines 51 to 60
synchronized (this) {
while (isRunning) {
while (isSuspended) {
try {
wait(); // Puts the thread in waiting state.
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // Restores the interruption.
return;
}
}

Choose a reason for hiding this comment

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

The while (isSuspended) loop in run() lacks a check for isRunning. If isRunning becomes false while the thread is suspended, the thread will never terminate. Add a check for isRunning inside the while (isSuspended) loop to ensure proper termination.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

🚨 Pull request needs attention.

Review Summary

Commits Considered (1)
  • 0d2cdb1: Updated to GitHub Actions comments.
Files Processed (2)
  • twin/README.md (4 hunks)
  • twin/src/main/java/com/iluwatar/twin/BallThread.java (3 hunks)
Actionable Comments (1)
  • twin/src/main/java/com/iluwatar/twin/BallThread.java [51-60]

    possible bug: "Potential race condition in thread termination."

Skipped Comments (1)
  • twin/README.md [8-245]

    enhancement: "Review README.md additions for clarity."

Comment on lines +51 to +60
synchronized (this) {
while (isRunning) {
while (isSuspended && isRunning) {
try {
wait(); // Puts the thread in waiting state.
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // Restores the interruption.
return;
}
}

Choose a reason for hiding this comment

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

The while (isSuspended && isRunning) loop in run() method lacks a check for isRunning after the wait() call. This could lead to the thread continuing to run even after it's been stopped.

@iluwatar
Copy link
Owner

The build is failing. Please ensure that all the checks pass before requesting review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants