BAEL-1264 - class and tests for killing a thread (#3069)
This commit is contained in:
parent
34ad3ad29b
commit
b30097b581
|
@ -0,0 +1,52 @@
|
||||||
|
package com.baeldung.concurrent.stopping;
|
||||||
|
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
|
public class ControlSubThread implements Runnable {
|
||||||
|
|
||||||
|
private Thread worker;
|
||||||
|
private int interval = 100;
|
||||||
|
private AtomicBoolean running = new AtomicBoolean(false);
|
||||||
|
private AtomicBoolean stopped = new AtomicBoolean(true);
|
||||||
|
|
||||||
|
|
||||||
|
public ControlSubThread(int sleepInterval) {
|
||||||
|
interval = sleepInterval;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start() {
|
||||||
|
worker = new Thread(this);
|
||||||
|
worker.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stop() {
|
||||||
|
running.set(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void interrupt() {
|
||||||
|
running.set(false);
|
||||||
|
worker.interrupt();
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean isRunning() {
|
||||||
|
return running.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean isStopped() {
|
||||||
|
return stopped.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
running.set(true);
|
||||||
|
stopped.set(false);
|
||||||
|
while (running.get()) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(interval);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
// no-op, just loop again
|
||||||
|
}
|
||||||
|
// do something
|
||||||
|
}
|
||||||
|
stopped.set(true);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue