ExecutorService JVM不会终止 [英] ExecutorService JVM doesn't terminate

查看:142
本文介绍了ExecutorService JVM不会终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么我必须调用executorService.shutdown() 明确终止executorService.

I don't understand why I have to call executorService.shutdown() explicitly to terminate executorService.

如果我不调用shutdown(),那么JVM将不会自行终止.

If I will not call shutdown() then the JVM will not terminate on its own.

我的程序有什么问题或缺少什么概念?

public class ExecutorServiceExample {

    public static class Task1 implements Runnable {

        @Override
        public void run() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println("Message from Task1 :"
                    + Thread.currentThread().getName());
        }

    }

    public static class Task2 implements Runnable {

        @Override
        public void run() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Message from Task2 :"
                    + Thread.currentThread().getName());
        }

    }

    public static class Task3 implements Runnable {

        @Override
        public void run() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Message from Task3 :"
                    + Thread.currentThread().getName());
        }

    }

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(10);

        Future future1 = executorService.submit(new Task1());
        Future future2 = executorService.submit(new Task2());
        Future future3 = executorService.submit(new Task3());


    }

}

输出:

任务2发出的消息:pool-1-thread-2

Message from Task2 :pool-1-thread-2

任务1发出的消息:pool-1-thread-1

Message from Task1 :pool-1-thread-1

任务3发出的消息:pool-1-thread-3

Message from Task3 :pool-1-thread-3

JVM仍然存在.如果我要调用shutdown(),那么只有JVM会死.

推荐答案

根据 Thread javadoc :

According to Thread javadoc:

Java虚拟机将继续执行线程,直到发生以下任何一种情况为止:

The Java Virtual Machine continues to execute threads until either of the following occurs:

  • 已调用Runtime类的退出方法,并且安全管理器已允许进行退出操作.
  • 不是守护程序线程的所有线程都已死亡,方法是从调用返回到run方法,或者引发一个传播到run方法之外的异常.
  • The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place.
  • All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.

您的执行程序创建了非守护线程,以防止您的JVM关闭.执行者创建的线程通常被合并以运行提交给执行者的多个任务-通常,这是对性能的优化以重用线程,因此它们执行的任务多于一个任务(因为创建新线程是一项昂贵的操作).取决于执行程序的实现和配置(例如,请参见

Your executor creates non-daemon threads which prevent your JVM from shutting down. Threads created by executors are usually pooled to run more than one task submitted to the executor - often this is a performance optimisation to reuse threads so they run more then one task (as creating new thread is an expensive operation). Depending on the implementation and configuration of the executor (e.g. see ThreadPoolExecutor doc, especially keepalive configuration) it might keep the threads running forever or terminate when they are unused for some time.

您必须在执行程序上调用shutdown或使用自定义的

You must either call shutdown on your executor or create it with a custom ThreadFactory (e.g. using Executors.newFixedThreadPool(int, ThreadFactory)) where that thread factory will configure new threads as daemon ones.

这篇关于ExecutorService JVM不会终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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