Skip to content

Commit

Permalink
busy waiting loops refractored and errors removed
Browse files Browse the repository at this point in the history
  • Loading branch information
AnmolxSingh committed Jun 4, 2024
1 parent 0a22629 commit b65c96c
Showing 1 changed file with 36 additions and 36 deletions.
72 changes: 36 additions & 36 deletions twin/src/main/java/com/iluwatar/twin/BallThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,52 +32,52 @@
* and resume. It holds the reference of {@link BallItem} to delegate the draw task.
*/

@Slf4j
import java.util.concurrent.CountDownLatch;
import java.util.logging.Logger;

@Slf4j
public class BallThread extends Thread {

@Setter
private BallItem twin;
@Setter
private BallItem twin;

private volatile boolean isRunning = true;
private volatile boolean isRunning = true;

private final CountDownLatch suspendLatch = new CountDownLatch(1);
private static final org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory.getLogger(BallThread.class);

public void run() {
while (isRunning) {
try {
suspendLatch.await(); // Wait until released
} catch (InterruptedException e) {
LOGGER.warning("Thread interrupted.");
Thread.currentThread().interrupt();
}
twin.draw();
twin.move();
try {
Thread.sleep(250);
} catch (InterruptedException e) {
LOGGER.warning("Thread interrupted.");
Thread.currentThread().interrupt();
}
}
}
private CountDownLatch suspendLatch = new CountDownLatch(1);

public void suspendMe() {
suspendLatch.countDown(); // Release the latch
LOGGER.info("Thread suspended.");
public void run() {
while (isRunning) {
try {
suspendLatch.await(); // Wait until released
} catch (InterruptedException e) {
LOGGER.warning("Thread interrupted.");
Thread.currentThread().interrupt();
}
twin.draw();
twin.move();
try {
Thread.sleep(250);
} catch (InterruptedException e) {
LOGGER.warning("Thread interrupted.");
Thread.currentThread().interrupt();
}
}
}

public void resumeMe() {
suspendLatch.countDown(); // In case latch was already released
suspendLatch = new CountDownLatch(1); // Reset the latch
LOGGER.info("Thread resumed.");
}
public void suspendMe() {
suspendLatch.countDown(); // Release the latch
LOGGER.info("Thread suspended.");
}

public void stopMe() {
isRunning = false;
suspendMe(); // Release latch to ensure the thread can terminate
}
public void resumeMe() {
suspendLatch.countDown(); // In case latch was already released
suspendLatch = new CountDownLatch(1); // Reset the latch
LOGGER.info("Thread resumed.");
}

public void stopMe() {
isRunning = false;
suspendMe(); // Release latch to ensure the thread can terminate
}
}

0 comments on commit b65c96c

Please sign in to comment.