停止线程池的正确方法
最后更新于:2022-08-13 12:47:15
1. shutdown
优雅的关闭线程池,会等待线程池内所有线程执行完毕,通知不再接收新的线程加入
示例
public class ShutDown {
public static void main(String[] args) throws InterruptedException {
ExecutorService service = Executors.newFixedThreadPool(1);
for (int i = 0; i < 2; i++) {
service.execute(new ShutDownTask());
}
Thread.sleep(1500);
service.shutdown();
System.out.println("shutDown");
service.execute(new ShutDownTask());
}
}
public class ShutDownTask implements Runnable {
@Override
public void run() {
try {
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
运行结果
pool-1-thread-1
shutDown
Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task com.honor.concurrency.ThreadStop.ShutDownTask@6e0be858 rejected from java.util.concurrent.ThreadPoolExecutor@61bbe9ba[Shutting down, pool size = 10, active threads = 10, queued tasks = 0, completed tasks = 10]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1379)
at com.honor.concurrency.ThreadStop.ShutDown.main(ShutDown.java:18)
pool-1-thread-1
2.isShutdown
查看线程池状态是否关闭,返回true或false,不代表线程已经结束,只是开始结束。
示例
public class ShutDown {
public static void main(String[] args) throws InterruptedException {
ExecutorService service = Executors.newFixedThreadPool(1);
for (int i = 0; i < 2; i++) {
service.execute(new ShutDownTask());
}
Thread.sleep(1500);
System.out.println(service.isShutdown());
System.out.println("shutDown");
service.shutdown();
System.out.println(service.isShutdown());
}
}
class ShutDownTask implements Runnable {
@Override
public void run() {
try {
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
运行结果
pool-1-thread-1
false
shutDown
true
pool-1-thread-1
3.isTerminated
查看线程是否已将完全结束
示例
public class ShutDown {
public static void main(String[] args) throws InterruptedException {
ExecutorService service = Executors.newFixedThreadPool(1);
for (int i = 0; i < 2; i++) {
service.execute(new ShutDownTask());
}
Thread.sleep(1500);
System.out.println("shutDown:" + service.isShutdown());
System.out.println("shutDown");
service.shutdown();
System.out.println("shutDown:" + service.isShutdown());
System.out.println("Terminated:" + service.isTerminated());
Thread.sleep(1000);
System.out.println("Terminated:" + service.isTerminated());
}
}
public class ShutDownTask implements Runnable {
@Override
public void run() {
try {
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
运行结果
pool-1-thread-1
shutDown:false
shutDown
shutDown:true
Terminated:false
pool-1-thread-1
Terminated:true
4.awaitTermination
返回一段时间后,线程是否执行结束,并且调用了shutDown
示例
public class ShutDown {
public static void main(String[] args) throws InterruptedException {
ExecutorService service = Executors.newFixedThreadPool(1);
for (int i = 0; i < 2; i++) {
service.execute(new ShutDownTask());
}
System.out.println(service.awaitTermination(3, TimeUnit.SECONDS));
service.shutdown();
System.out.println(service.awaitTermination(3, TimeUnit.SECONDS));
}
}
public class ShutDownTask implements Runnable {
@Override
public void run() {
try {
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
运行结果
pool-1-thread-1
pool-1-thread-1
false
true
5.shutdownNow
强制中断线程,同时返回未执行的队列中的数据列表
示例
public class ShutDown {
public static void main(String[] args) throws InterruptedException {
ExecutorService service = Executors.newFixedThreadPool(1);
for (int i = 0; i < 3; i++) {
service.execute(new ShutDownTask());
}
Thread.sleep(1500);
List runnables = service.shutdownNow();
System.out.println("队列中剩余未执行线程数量" + runnables.size());
}
}
public class ShutDownTask implements Runnable {
@Override
public void run() {
String name = Thread.currentThread().getName();
try {
Thread.sleep(1000);
System.out.println(name + "执行了");
} catch (InterruptedException e) {
System.out.println(name + "被中断了");
}
}
}
运行结果
pool-1-thread-1执行了
pool-1-thread-1被中断了
队列中剩余未执行线程数量1