Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b65c96c

Browse files
committedJun 4, 2024·
busy waiting loops refractored and errors removed
1 parent 0a22629 commit b65c96c

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed
 

‎twin/src/main/java/com/iluwatar/twin/BallThread.java

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,52 +32,52 @@
3232
* and resume. It holds the reference of {@link BallItem} to delegate the draw task.
3333
*/
3434

35-
@Slf4j
3635
import java.util.concurrent.CountDownLatch;
37-
import java.util.logging.Logger;
3836

3937
@Slf4j
4038
public class BallThread extends Thread {
4139

42-
@Setter
43-
private BallItem twin;
40+
@Setter
41+
private BallItem twin;
4442

45-
private volatile boolean isRunning = true;
43+
private volatile boolean isRunning = true;
4644

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

49-
public void run() {
50-
while (isRunning) {
51-
try {
52-
suspendLatch.await(); // Wait until released
53-
} catch (InterruptedException e) {
54-
LOGGER.warning("Thread interrupted.");
55-
Thread.currentThread().interrupt();
56-
}
57-
twin.draw();
58-
twin.move();
59-
try {
60-
Thread.sleep(250);
61-
} catch (InterruptedException e) {
62-
LOGGER.warning("Thread interrupted.");
63-
Thread.currentThread().interrupt();
64-
}
65-
}
66-
}
47+
private CountDownLatch suspendLatch = new CountDownLatch(1);
6748

68-
public void suspendMe() {
69-
suspendLatch.countDown(); // Release the latch
70-
LOGGER.info("Thread suspended.");
49+
public void run() {
50+
while (isRunning) {
51+
try {
52+
suspendLatch.await(); // Wait until released
53+
} catch (InterruptedException e) {
54+
LOGGER.warning("Thread interrupted.");
55+
Thread.currentThread().interrupt();
56+
}
57+
twin.draw();
58+
twin.move();
59+
try {
60+
Thread.sleep(250);
61+
} catch (InterruptedException e) {
62+
LOGGER.warning("Thread interrupted.");
63+
Thread.currentThread().interrupt();
64+
}
7165
}
66+
}
7267

73-
public void resumeMe() {
74-
suspendLatch.countDown(); // In case latch was already released
75-
suspendLatch = new CountDownLatch(1); // Reset the latch
76-
LOGGER.info("Thread resumed.");
77-
}
68+
public void suspendMe() {
69+
suspendLatch.countDown(); // Release the latch
70+
LOGGER.info("Thread suspended.");
71+
}
7872

79-
public void stopMe() {
80-
isRunning = false;
81-
suspendMe(); // Release latch to ensure the thread can terminate
82-
}
73+
public void resumeMe() {
74+
suspendLatch.countDown(); // In case latch was already released
75+
suspendLatch = new CountDownLatch(1); // Reset the latch
76+
LOGGER.info("Thread resumed.");
77+
}
78+
79+
public void stopMe() {
80+
isRunning = false;
81+
suspendMe(); // Release latch to ensure the thread can terminate
82+
}
8383
}

0 commit comments

Comments
 (0)
Please sign in to comment.