11/*
2- * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3- *
4- * The MIT License
5- * Copyright © 2014-2022 Ilkka Seppälä
6- *
7- * Permission is hereby granted, free of charge, to any person obtaining a copy
8- * of this software and associated documentation files (the "Software"), to deal
9- * in the Software without restriction, including without limitation the rights
10- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11- * copies of the Software, and to permit persons to whom the Software is
12- * furnished to do so, subject to the following conditions:
13- *
14- * The above copyright notice and this permission notice shall be included in
15- * all copies or substantial portions of the Software.
16- *
17- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23- * THE SOFTWARE.
2+ * This project is licensed under the MIT license. For more information see the LICENSE.md file.
243 */
254package com .iluwatar .twin ;
265
276import lombok .Setter ;
287import lombok .extern .slf4j .Slf4j ;
298
309/**
31- * This class is a UI thread for drawing the {@link BallItem}, and provide the method for suspend
32- * and resume. It holds the reference of {@link BallItem} to delegate the draw task.
10+ * This class is a UI thread for drawing the {@link BallItem}, and provides methods for suspension
11+ * and resumption. It holds a reference to a {@link BallItem} to delegate the drawing task.
12+ * This implementation uses wait/notify to avoid busy-waiting.
3313 */
3414@ Slf4j
3515public class BallThread extends Thread {
3616
37- @ Setter private BallItem twin ;
17+ @ Setter
18+ private BallItem twin ;
3819
20+ private final Object lock = new Object ();
3921 private volatile boolean isSuspended ;
40-
4122 private volatile boolean isRunning = true ;
4223
43- /** Run the thread. */
24+ /**
25+ * The main execution method for the thread. It continuously draws and moves the twin object
26+ * unless suspended or stopped.
27+ */
28+ @ Override
4429 public void run () {
45-
4630 while (isRunning ) {
47- if (!isSuspended ) {
31+ try {
32+ synchronized (lock ) {
33+ // Wait while the thread is suspended.
34+ while (isSuspended ) {
35+ lock .wait ();
36+ }
37+ }
38+
39+ // Check if the thread should terminate after being woken up.
40+ if (!isRunning ) {
41+ break ;
42+ }
43+
44+ // Perform the work.
4845 twin .draw ();
4946 twin .move ();
50- }
51- try {
47+
48+ // Control the animation speed.
5249 Thread .sleep (250 );
5350 } catch (InterruptedException e ) {
54- throw new RuntimeException (e );
51+ // Restore the interrupted status and exit gracefully.
52+ Thread .currentThread ().interrupt ();
53+ LOGGER .error ("BallThread was interrupted" , e );
54+ isRunning = false ;
5555 }
5656 }
5757 }
5858
59+ /**
60+ * Suspends the thread's execution. The thread will pause its work and wait until resumed.
61+ */
5962 public void suspendMe () {
6063 isSuspended = true ;
61- LOGGER .info ("Begin to suspend BallThread" );
64+ LOGGER .info ("Suspending BallThread. " );
6265 }
6366
67+ /**
68+ * Resumes the thread's execution. It notifies the waiting thread to continue its work.
69+ */
6470 public void resumeMe () {
65- isSuspended = false ;
66- LOGGER .info ("Begin to resume BallThread" );
71+ synchronized (lock ) {
72+ isSuspended = false ;
73+ // Wake up the waiting thread.
74+ lock .notifyAll ();
75+ }
76+ LOGGER .info ("Resuming BallThread." );
6777 }
6878
79+ /**
80+ * Stops the thread's execution permanently. It ensures that if the thread is suspended,
81+ * it will be woken up to terminate gracefully.
82+ */
6983 public void stopMe () {
70- this .isRunning = false ;
71- this .isSuspended = true ;
84+ isRunning = false ;
85+ // Wake up the thread if it is suspended so it can terminate.
86+ resumeMe ();
7287 }
73- }
88+ }
0 commit comments