简体   繁体   English

如何停止java executor类中的所有可运行线程?

[英]How to stop all runnable thread in java executor class?

final ExecutorService executor = Executors.newFixedThreadPool(1);
final Future<?> future = executor.submit(myRunnable);
executor.shutdown();
if(executor.awaitTermination(10, TimeUnit.SECONDS)) {
  System.out.println("task completed");
}else{
  System.out.println("Executor is shutdown now");
}

//MyRunnable method is defined as task which I want to execute in a different thread.

Here is run method of executor class:这是执行程序类的run方法:

public void run() {
try {
     Thread.sleep(20 * 1000);
} catch (InterruptedException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
}}

Here it is waiting for 20 second but when i run the code it throws an exception:这里等待了20秒,但是当我运行代码时,它抛出异常:

java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)

I am not able to close the concurrent thread ruining in Java Executor class .我无法关闭Java Executor class的并发线程破坏。 Here is my Code flow:这是我的代码流程:

  • Created a new Thread with Java executor class to run some task ie written in MyRunnable使用 Java 执行程序类创建了一个新线程来运行一些任务,即用MyRunnable编写
  • executor wait for 10 second to complete the tasks. executor等待 10 秒完成任务。
  • If the task has completed then runnable thread also got terminated.如果任务已完成,则可运行线程也将终止。
  • If the task is not completed within 10 second then executor class should terminate the thread.如果任务未在 10 秒内完成,则executor类应终止线程。

Everything works fine except the termination of tasks in the last scenario.除了最后一个场景中的任务终止外,一切正常。 How should I do it?我该怎么做?

The shutDown() method simply prevents additional tasks from being scheduled. shutDown()方法只是防止安排额外的任务。 Instead, you could call shutDownNow() and check for thread interruption in your Runnable .相反,您可以调用shutDownNow()并检查Runnable线程中断。

// in your Runnable...
if (Thread.interrupted()) {
  // Executor has probably asked us to stop
}

An example, based on your code, might be:根据您的代码,一个示例可能是:

final ExecutorService executor = Executors.newFixedThreadPool(1);
executor.submit(new Runnable() {
  public void run() {
    try {
      Thread.sleep(20 * 1000);
    } catch (InterruptedException e) {
      System.out.println("Interrupted, so exiting.");
    }
  }
});

if (executor.awaitTermination(10, TimeUnit.SECONDS)) {
  System.out.println("task completed");
} else {
  System.out.println("Forcing shutdown...");
  executor.shutdownNow();
}

It is generally a bad idea to terminate a running thread from the outside, because you don't know the state the thread is currently in. It's possible that it needs to do some cleanups, and it won't be able to do that when you forcefully shut it down.从外部终止正在运行的线程通常是一个坏主意,因为您不知道线程当前所处的状态。它可能需要做一些清理工作,而当它无法做到这一点时你强行关闭它。 That's why all methods of Thread which do that are marked as deprecated . 这就是为什么所有执行此操作的 Thread 方法都被标记为 deprecated

It's much better to use one of the many techniques which are available for interprocess communication to signal the procedure running in the thread itself that it has to abort its work and exit normally.最好使用可用于进程间通信的众多技术中的一种来向在线程本身中运行的过程发出信号,表明它必须中止其工作并正常退出。 One way to do this is to add an abort() method to your runnable, which raises a flag declared as volatile .一种方法是向 runnable 添加abort()方法,这会引发一个声明为volatile的标志。 The inner loop of your Runnable checks that flag and exits (in a controlled fashion) when that flag is raised. Runnable 的内部循环检查该标志并在该标志被引发时退出(以受控方式)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM