ExecutorService JVM 不会终止
ExecutorService JVM doesn't terminate
I don't understand why I have to call executorService.shutdown()
explicitly to terminate executorService.
如果我不调用 shutdown(),那么 JVM 将不会自行终止。
我的程序有什么问题或者我缺少什么概念?
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());
}
}
输出:
Message from Task2 :pool-1-thread-2
Message from Task1 :pool-1-thread-1
Message from Task3 :pool-1-thread-3
JVM 仍然存在。如果我调用 shutdown() 那么只有 JVM 会死掉。
The Java Virtual Machine continues to execute threads until either of the following occurs:
- 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 关闭。执行者创建的线程通常汇集到 运行 个以上提交给执行者的任务 - 通常这是重用线程的性能优化,因此它们 运行 多个任务(因为创建新线程是一项昂贵的任务手术)。根据执行程序的实现和配置(例如,参见 ThreadPoolExecutor
doc, especially keepalive
configuration),它可能会永远保持线程 运行ning 或在一段时间未使用时终止。
您必须在执行程序上调用 shutdown
或使用自定义 ThreadFactory
(e.g. using Executors.newFixedThreadPool(int, ThreadFactory)
) 创建它,线程工厂会将新线程配置为守护进程。
I don't understand why I have to call executorService.shutdown() explicitly to terminate executorService.
如果我不调用 shutdown(),那么 JVM 将不会自行终止。
我的程序有什么问题或者我缺少什么概念?
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());
}
}
输出:
Message from Task2 :pool-1-thread-2
Message from Task1 :pool-1-thread-1
Message from Task3 :pool-1-thread-3
JVM 仍然存在。如果我调用 shutdown() 那么只有 JVM 会死掉。
The Java Virtual Machine continues to execute threads until either of the following occurs:
- 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 关闭。执行者创建的线程通常汇集到 运行 个以上提交给执行者的任务 - 通常这是重用线程的性能优化,因此它们 运行 多个任务(因为创建新线程是一项昂贵的任务手术)。根据执行程序的实现和配置(例如,参见 ThreadPoolExecutor
doc, especially keepalive
configuration),它可能会永远保持线程 运行ning 或在一段时间未使用时终止。
您必须在执行程序上调用 shutdown
或使用自定义 ThreadFactory
(e.g. using Executors.newFixedThreadPool(int, ThreadFactory)
) 创建它,线程工厂会将新线程配置为守护进程。