BAEL-1264 - class and tests for killing a thread (#3069)

This commit is contained in:
Eric Goebelbecker
2017-11-16 18:41:00 -05:00
committed by maibin
parent 34ad3ad29b
commit b30097b581
2 changed files with 102 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
package com.baeldung.concurrent.stopping;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class StopThreadTest {
@Test
public void whenStoppedThreadIsStopped() throws InterruptedException {
int interval = 100;
ControlSubThread controlSubThread = new ControlSubThread(interval);
controlSubThread.start();
// Give things a chance to get set up
Thread.sleep(interval);
assertTrue(controlSubThread.isRunning());
assertFalse(controlSubThread.isStopped());
// Stop it and make sure the flags have been reversed
controlSubThread.stop();
Thread.sleep(interval);
assertTrue(controlSubThread.isStopped());
}
@Test
public void whenInterruptedThreadIsStopped() throws InterruptedException {
int interval = 5000;
ControlSubThread controlSubThread = new ControlSubThread(interval);
controlSubThread.start();
// Give things a chance to get set up
Thread.sleep(100);
assertTrue(controlSubThread.isRunning());
assertFalse(controlSubThread.isStopped());
// Stop it and make sure the flags have been reversed
controlSubThread.interrupt();
// Wait less than the time we would normally sleep, and make sure we exited.
Thread.sleep(interval/10);
assertTrue(controlSubThread.isStopped());
}
}