forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-1264 - class and tests for killing a thread (eugenp#3069)
- Loading branch information
1 parent
34ad3ad
commit b30097b
Showing
2 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
core-java-concurrency/src/main/java/com/baeldung/concurrent/stopping/ControlSubThread.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
core-java-concurrency/src/test/java/com/baeldung/concurrent/stopping/StopThreadTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |