@@ -84,36 +84,47 @@ public class BallItem extends GameItem {
8484public class BallThread extends Thread {
8585 @Setter
8686 private BallItem twin;
87- private volatile boolean isSuspended;
88- private volatile boolean isRunning = true ;
87+ private boolean isSuspended;
88+ private boolean isRunning = true ;
8989
90+ @Override
9091 public void run () {
91- while (isRunning) {
92- if (! isSuspended) {
92+ synchronized (this ) {
93+ while (isRunning) {
94+ while (isSuspended) {
95+ try {
96+ wait();
97+ } catch (InterruptedException e) {
98+ LOGGER . error(" Thread interrupted" , e);
99+ }
100+ }
93101 twin. draw();
94102 twin. move();
95- }
96- try {
97- Thread . sleep( 250 );
98- } catch ( InterruptedException e) {
99- throw new RuntimeException (e);
103+ try {
104+ Thread . sleep( 250 ); // Optional delay for smooth execution
105+ } catch ( InterruptedException e) {
106+ LOGGER . error( " Sleep interrupted " , e);
107+ }
100108 }
101109 }
102110 }
103111
104- public void suspendMe () {
112+ public synchronized void suspendMe () {
105113 isSuspended = true ;
106- LOGGER . info(" Begin to suspend BallThread" );
114+ LOGGER . info(" BallThread suspended " );
107115 }
108116
109- public void resumeMe () {
117+ public synchronized void resumeMe () {
110118 isSuspended = false ;
111- LOGGER . info(" Begin to resume BallThread" );
119+ LOGGER . info(" BallThread resumed" );
120+ notify();
112121 }
113122
114- public void stopMe () {
115- this . isRunning = false ;
116- this . isSuspended = true ;
123+ public synchronized void stopMe () {
124+ isRunning = false ;
125+ isSuspended = false ;
126+ LOGGER . info(" BallThread stopping" );
127+ notify();
117128 }
118129}
119130```
0 commit comments