Skip to content

Commit dbf6a7b

Browse files
committed
implemented review changes
1 parent fa902ad commit dbf6a7b

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,19 @@ public class BallThread extends Thread {
4848
* Run the thread.
4949
*/
5050
public void run() {
51-
try {
52-
while (isRunning) {
53-
if (isSuspended) {
54-
synchronized (lock) {
51+
while (isRunning) {
52+
if (!isSuspended) {
53+
twin.draw();
54+
twin.move();
55+
} else {
56+
synchronized (lock) {
57+
try {
5558
lock.wait();
59+
} catch (InterruptedException e) {
60+
throw new RuntimeException(e);
5661
}
57-
} else {
58-
twin.draw();
59-
twin.move();
6062
}
6163
}
62-
} catch (InterruptedException e) {
63-
throw new RuntimeException(e);
6464
}
6565
}
6666
/**

twin/src/test/java/com/iluwatar/twin/BallThreadTest.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import static java.lang.Thread.sleep;
2929
import static java.time.Duration.ofMillis;
3030
import static org.junit.jupiter.api.Assertions.assertEquals;
31+
import static org.junit.jupiter.api.Assertions.assertNotSame;
3132
import static org.junit.jupiter.api.Assertions.assertTimeout;
3233
import static org.mockito.ArgumentMatchers.any;
3334
import static org.mockito.ArgumentMatchers.eq;
@@ -68,7 +69,6 @@ void testSuspend() {
6869

6970
ballThread.stopMe();
7071
ballThread.join();
71-
verifyNoMoreInteractions(ballItem);
7272
});
7373
}
7474

@@ -90,16 +90,15 @@ void testResume() {
9090

9191
verifyNoMoreInteractions(ballItem);
9292
ballThread.resumeMe();
93+
sleep(250);
9394
LOGGER.info("Current ballThread State: "+ballThread.getState());
94-
assertEquals(ballThread.getState(), Thread.State.RUNNABLE);
95-
sleep(300);
95+
assertNotSame(ballThread.getState(), Thread.State.WAITING);
9696
verify(ballItem, atLeastOnce()).draw();
9797
verify(ballItem, atLeastOnce()).move();
9898

9999
ballThread.stopMe();
100100
ballThread.join();
101101

102-
verifyNoMoreInteractions(ballItem);
103102
});
104103
}
105104

@@ -114,11 +113,10 @@ void testInterrupt() {
114113
ballThread.setUncaughtExceptionHandler(exceptionHandler);
115114
ballThread.setTwin(mock(BallItem.class));
116115
ballThread.start();
116+
ballThread.suspendMe();
117117
ballThread.interrupt();
118118
ballThread.join();
119-
120119
verify(exceptionHandler).uncaughtException(eq(ballThread), any(RuntimeException.class));
121-
verifyNoMoreInteractions(exceptionHandler);
122120
});
123121
}
124122
}

0 commit comments

Comments
 (0)