来自 Callable 的线程处于等待状态。我如何杀死线程?
Thread from Callable stays in waiting state. How do I kill the Thread?
有很多类似的问题,但没有一个解决方案适合我。
我有一个 Callable 需要 运行 一段时间。在 Call 方法的执行过程中,它必须定期在 while 条件中进行一些检查,以检查它是否必须保持 运行ning。我还希望能够从外部停止调用(API 调用)。
下面的代码是一个简化版本,但它有同样的问题:
当callablereturns时,线程停留在WAITING状态。我如何终止此线程?
public class MyCallable implements Callable<Foo> {
private AtomicBoolean stop = new AtomicBoolean(false);
@Override
public Foo call() {
System.out.printf("New thread with ID=%d\n",
Thread.currentThread().getId());
Foo foo = new Foo();
while (!stop.get()) {
try {
Thread.sleep(1000); // Sleep for some time before doing checks again
} catch (InterruptedException e) {
}
}
System.out.printf("State before returning foo: %s\n",
Thread.currentThread().getState());
return foo;
}
public void stop() {
this.stop.set(true);
}
}
public class Main {
public static void main(String[] args) throws InterruptedException {
MyCallable myCallable = new MyCallable();
ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
Future<Foo> future = executorService.submit(myCallable);
printThreads();
System.out.println("Calling stop\n");
myCallable.stop();
while (!future.isDone()) {
Thread.sleep(200);
}
System.out.println("After future is done: ");
printThreads();
}
// Helper method
private static void printThreads() {
List<Thread> threads = Thread.getAllStackTraces().keySet()
.stream()
.filter(t -> t.getName().contains("pool"))
.collect(Collectors.toList());
threads.forEach(t -> System.out.printf("ID=%s STATE=%s\t\n", t.getId(), t.getState()));
System.out.println();
}
}
这是程序的输出
您不需要手动终止由 ExecutorService
管理的线程。您需要正常关闭该服务,它会终止其线程。
executorService.shutdown();
通常,线程工作者不会在完成单个任务后终止。它会移动到 WAITING
状态,直到出现新任务。这些东西由 ExecutorService
管理。关闭它会导致终止它负责的线程。
有很多类似的问题,但没有一个解决方案适合我。
我有一个 Callable 需要 运行 一段时间。在 Call 方法的执行过程中,它必须定期在 while 条件中进行一些检查,以检查它是否必须保持 运行ning。我还希望能够从外部停止调用(API 调用)。
下面的代码是一个简化版本,但它有同样的问题:
当callablereturns时,线程停留在WAITING状态。我如何终止此线程?
public class MyCallable implements Callable<Foo> {
private AtomicBoolean stop = new AtomicBoolean(false);
@Override
public Foo call() {
System.out.printf("New thread with ID=%d\n",
Thread.currentThread().getId());
Foo foo = new Foo();
while (!stop.get()) {
try {
Thread.sleep(1000); // Sleep for some time before doing checks again
} catch (InterruptedException e) {
}
}
System.out.printf("State before returning foo: %s\n",
Thread.currentThread().getState());
return foo;
}
public void stop() {
this.stop.set(true);
}
}
public class Main {
public static void main(String[] args) throws InterruptedException {
MyCallable myCallable = new MyCallable();
ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
Future<Foo> future = executorService.submit(myCallable);
printThreads();
System.out.println("Calling stop\n");
myCallable.stop();
while (!future.isDone()) {
Thread.sleep(200);
}
System.out.println("After future is done: ");
printThreads();
}
// Helper method
private static void printThreads() {
List<Thread> threads = Thread.getAllStackTraces().keySet()
.stream()
.filter(t -> t.getName().contains("pool"))
.collect(Collectors.toList());
threads.forEach(t -> System.out.printf("ID=%s STATE=%s\t\n", t.getId(), t.getState()));
System.out.println();
}
}
这是程序的输出
您不需要手动终止由 ExecutorService
管理的线程。您需要正常关闭该服务,它会终止其线程。
executorService.shutdown();
通常,线程工作者不会在完成单个任务后终止。它会移动到 WAITING
状态,直到出现新任务。这些东西由 ExecutorService
管理。关闭它会导致终止它负责的线程。