Skip to content

Commit b30097b

Browse files
egoebelbeckermaibin
authored andcommitted
BAEL-1264 - class and tests for killing a thread (eugenp#3069)
1 parent 34ad3ad commit b30097b

2 files changed

Lines changed: 102 additions & 0 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.concurrent.stopping;
2+
3+
import java.util.concurrent.atomic.AtomicBoolean;
4+
5+
public class ControlSubThread implements Runnable {
6+
7+
private Thread worker;
8+
private int interval = 100;
9+
private AtomicBoolean running = new AtomicBoolean(false);
10+
private AtomicBoolean stopped = new AtomicBoolean(true);
11+
12+
13+
public ControlSubThread(int sleepInterval) {
14+
interval = sleepInterval;
15+
}
16+
17+
public void start() {
18+
worker = new Thread(this);
19+
worker.start();
20+
}
21+
22+
public void stop() {
23+
running.set(false);
24+
}
25+
26+
public void interrupt() {
27+
running.set(false);
28+
worker.interrupt();
29+
}
30+
31+
boolean isRunning() {
32+
return running.get();
33+
}
34+
35+
boolean isStopped() {
36+
return stopped.get();
37+
}
38+
39+
public void run() {
40+
running.set(true);
41+
stopped.set(false);
42+
while (running.get()) {
43+
try {
44+
Thread.sleep(interval);
45+
} catch (InterruptedException e) {
46+
// no-op, just loop again
47+
}
48+
// do something
49+
}
50+
stopped.set(true);
51+
}
52+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.baeldung.concurrent.stopping;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertFalse;
6+
import static org.junit.Assert.assertTrue;
7+
8+
public class StopThreadTest {
9+
10+
@Test
11+
public void whenStoppedThreadIsStopped() throws InterruptedException {
12+
13+
int interval = 100;
14+
15+
ControlSubThread controlSubThread = new ControlSubThread(interval);
16+
controlSubThread.start();
17+
18+
// Give things a chance to get set up
19+
Thread.sleep(interval);
20+
assertTrue(controlSubThread.isRunning());
21+
assertFalse(controlSubThread.isStopped());
22+
23+
// Stop it and make sure the flags have been reversed
24+
controlSubThread.stop();
25+
Thread.sleep(interval);
26+
assertTrue(controlSubThread.isStopped());
27+
}
28+
29+
30+
@Test
31+
public void whenInterruptedThreadIsStopped() throws InterruptedException {
32+
33+
int interval = 5000;
34+
35+
ControlSubThread controlSubThread = new ControlSubThread(interval);
36+
controlSubThread.start();
37+
38+
// Give things a chance to get set up
39+
Thread.sleep(100);
40+
assertTrue(controlSubThread.isRunning());
41+
assertFalse(controlSubThread.isStopped());
42+
43+
// Stop it and make sure the flags have been reversed
44+
controlSubThread.interrupt();
45+
46+
// Wait less than the time we would normally sleep, and make sure we exited.
47+
Thread.sleep(interval/10);
48+
assertTrue(controlSubThread.isStopped());
49+
}
50+
}

0 commit comments

Comments
 (0)