|
32 | 32 | * and resume. It holds the reference of {@link BallItem} to delegate the draw task.
|
33 | 33 | */
|
34 | 34 |
|
35 |
| -@Slf4j |
36 | 35 | import java.util.concurrent.CountDownLatch;
|
37 |
| -import java.util.logging.Logger; |
38 | 36 |
|
39 | 37 | @Slf4j
|
40 | 38 | public class BallThread extends Thread {
|
41 | 39 |
|
42 |
| - @Setter |
43 |
| - private BallItem twin; |
| 40 | + @Setter |
| 41 | + private BallItem twin; |
44 | 42 |
|
45 |
| - private volatile boolean isRunning = true; |
| 43 | + private volatile boolean isRunning = true; |
46 | 44 |
|
47 |
| - private final CountDownLatch suspendLatch = new CountDownLatch(1); |
| 45 | + private static final org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory.getLogger(BallThread.class); |
48 | 46 |
|
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); |
67 | 48 |
|
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 | + } |
71 | 65 | }
|
| 66 | + } |
72 | 67 |
|
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 | + } |
78 | 72 |
|
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 | + } |
83 | 83 | }
|
0 commit comments