BAEL-1264 - class and tests for killing a thread (#3069)
This commit is contained in:
committed by
maibin
parent
34ad3ad29b
commit
b30097b581
+52
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user