Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ExecutorService超时未按预期工作_Java - Fatal编程技术网

Java ExecutorService超时未按预期工作

Java ExecutorService超时未按预期工作,java,Java,超时应该在一秒钟后发生,但不是那样发生的 public class Worker implements Runnable { int workerId; public Worker(int workerId) { super(); this.workerId = workerId; } public void run() { System.out.println(workerId+"Worker Started

超时应该在一秒钟后发生,但不是那样发生的

public class Worker implements Runnable {

    int workerId;

    public Worker(int workerId) {
        super();
        this.workerId = workerId;
    }

    public void run() {
        System.out.println(workerId+"Worker Started ....");
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(workerId+"Worker finishes.....");
    }
}

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class TestExecutor {

    public static void main(String[] args) {
        ExecutorService executorService=Executors.newCachedThreadPool();
        for(int i=0;i<=2;i++){
            executorService.submit(new Worker(i));
        }
        executorService.shutdown();
        try {
            executorService.awaitTermination(1, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            System.out.println("Timeout Happen .....");
        }
    }
}
公共类工作程序实现可运行{
int-workerId;
公共工作者(内部工作者ID){
超级();
this.workerId=workerId;
}
公开募捐{
System.out.println(workerId+“Worker Started…”);
试一试{
睡眠(20000);
}捕捉(中断异常e){
e、 printStackTrace();
}
System.out.println(workerId+“Worker finishs…”);
}
}
导入java.util.concurrent.ExecutorService;
导入java.util.concurrent.Executors;
导入java.util.concurrent.TimeUnit;
公共类测试器{
公共静态void main(字符串[]args){
ExecutorService ExecutorService=Executors.newCachedThreadPool();
对于(int i=0;i此代码

try {
    executorService.awaitTermination(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
    System.out.println("Timeout Happen .....");
}
不会做你期望它做的事

抛出: InterruptedException-如果在等待时中断

从。您的代码不会在第二秒内终止,并且只有在代码等待executorService终止时线程被中断,才会抛出
InterruptedException
。测试
executorService
是否实际终止的正确等待将被终止

try {
    if(executorService.awaitTermination(1, TimeUnit.SECONDS))
        System.out.println("Terminated correctly");
    else
        System.out.println("Termination failed");
} catch (InterruptedException e) {
    e.printStackTrace();
}

executorService.shutdown()
等待运行线程终止。因此,您的代码将等待整个20秒,直到提交的runnable终止,同时不会接受或启动任何新的
runnable
。如果您想以不太优雅的方式终止
线程,您必须使用
executorService.shutdownow()
,这将中断正在运行的线程。

发生了什么情况?在
executorService.awaitTermination(1,TimeUnit.SECONDS)中预期会发生什么情况
?文档的哪一部分使您认为此代码应该按照您期望的方式运行?我同意您的回答,但如何中断正在运行的线程
executorService.shutdownNow()
会这样做。它会阻止服务接受任何进一步的可运行项,并且通常会中断所有正在运行的Threrads。您应该尝试Shutdownow()。这会阻止正在等待的任务启动,并尝试停止当前正在执行的任务。executorService.Shutdownow()会吗这是一个好主意,否则我们应该始终通过Future Object中断通过
Future终止。cancle(true)
将只终止Future Object所属的任务。
executorService.Shutdownow()
将终止在
executorService
中运行的所有任务。但两者都通过中断正在运行的线程来工作。因此,在您的情况下,调用一个线程和调用另一个线程之间不会有太大的区别,请执行通过
Future
的方法将保持
executorService
运行。哪一个o选择取决于您的需求,而不是其他任何东西。