为什么 executor.isShutdown() 在其池中仍有线程运行时返回 true? [英] Why executor.isShutdown() returns true when there are threads still running in it's pool?

查看:35
本文介绍了为什么 executor.isShutdown() 在其池中仍有线程运行时返回 true?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

shutdown() 的 Java 文档说:

The Java doc for shutdown() says:

关闭

无效关机()

启动有序关闭,其中执行先前提交的任务,但不会接受新任务.如果已经关闭,则调用没有额外效果.此方法不等待先前提交的任务完成执行.使用 awaitTermination 来做到这一点.

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.Invocation has no additional effect if already shut down. This method does not wait for previously submitted tasks to complete execution. Use awaitTermination to do that.

这两个说法是不是矛盾的?(...之前提交的任务被执行 Vs 不等待之前提交的任务完成执行) ?

Aren't the two statements contradictory? (...previously submitted tasks are executed Vs does not wait for previously submitted tasks to complete execution) ?

我用 shutdown() 尝试了一个示例程序.与 shutdownNow()!

I tried out a sample program with shutdown(). And it does wait for previously submitted tasks to complete unlike shutdownNow()!

为什么下面的代码在应该等待阶乘任务完成时返回I have shutdown true?

Why the below code returns I have shutdown true when it is supposed to wait for the factorial task to complete?

public class RunnableMain {

    public static void main(String[] args) {
        ExecutorService executor = Executors.newSingleThreadExecutor();
        RunnableFactorialTask runnableFactorialTask = new RunnableFactorialTask(5);
        executor.submit(runnableFactorialTask);
        executor.shutdown();
        System.out.println(" I have shutdown.."+executor.isShutdown());
    }
}

class RunnableFactorialTask implements Runnable {

    private int num = 0;

    public RunnableFactorialTask(int num) {
        this.num = num;
    }

    @Override
    public void run() {
        int factorial = 1;
        try {
            Thread.currentThread().sleep(6000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for (int i = 2; i <= num; i++) {
            factorial *= i;
        }
        System.out.println("factorial of :" + num + " =" + factorial);
    }
}

推荐答案

shutdownNow()shutdown() 的调用不会阻塞.这就是文档中提到的内容.因此,如果您调用 shutdown()shutdownNow(),您会立即重新获得控制权,而不仅仅是在一切正常关闭之后.如果您想在调用 shutdown[Now]() 后等待所有执行程序/线程终止,则必须调用 awaitTermination(...),这将阻塞直到一切都被清理干净.这可能就是你想要的.另请参阅此处类似问题的答案:Executor 的 shutdown 和 shutdownNow 之间的区别服务

The calls to shutdownNow() or shutdown() are not blocking. That is what is referred to in the docs. So if you call shutdown() or shutdownNow(), you get control back immediately, not only after everything has gracefully shut down. If you want to wait for all executors/threads to terminate after the call to shutdown[Now](), you have to call awaitTermination(...), which will block until everything is cleaned up. This is probably what you want. Also see answers to a similar question here: Difference between shutdown and shutdownNow of Executor Service

这篇关于为什么 executor.isShutdown() 在其池中仍有线程运行时返回 true?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆