线程池的关闭
shu
shutdown 方法
shutdown方法关闭线程池, 并不会立即关闭, 而是线程池内存量的任务处理完再关闭.
我们可以调用 isShutdown 方法来查看 线程池是否进入了shutdown 状态. 也可以通过 isTerminate 方法来查看线程池是否仍在运行
public class ShutDown {
public static void main(String[] args) {
ExecutorService threadPool = Executors.newFixedThreadPool(5);
for (int i = 0; i < 30; i++) {
threadPool.execute(new SimpleTask3());
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("isShutDown ? "+threadPool.isShutdown());
threadPool.shutdown();
System.out.println("isShutDown ? "+threadPool.isShutdown());
System.out.println("isTerminated ? "+threadPool.isTerminated());
//shutDown以后提交新任务, 直接报错
threadPool.execute(new SimpleTask3());
}
}
public class SimpleTask3 implements Runnable{
@Override
public void run() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
}
}
部分运行结果, 可以看出在调用 shutdown 方法后, 如果继续向线程池里面提交任务, 会抛出 RejectedExecutionException
isShutDown ? false
isShutDown ? true
isTerminated ? false
Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task indi.chester.threadpool.SimpleTask3@330bedb4 rejected from java.util.concurrent.ThreadPoolExecutor@2503dbd3[Shutting down, pool size = 5, active threads = 5, queued tasks = 25, completed tasks = 0]
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 indi.chester.threadpool.ShutDown.main(ShutDown.java:31)
pool-1-thread-1
pool-1-thread-5
pool-1-thread-3
pool-1-thread-4
pool-1-thread-2
pool-1-thread-3
shutdownNow
shutdownNow 可以立即终止线程池运, 返回一个 实现 Runnable接口的对象list
public class ShutDownNow {
public static void main(String[] args) {
ExecutorService threadPool = Executors.newFixedThreadPool(5);
for (int i = 0; i < 10; i++) {
threadPool.execute(new SimpleTask4());
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
List<Runnable> threadList= threadPool.shutdownNow();
System.out.println("isShutDown ? "+threadPool.isShutdown());
System.out.println("isTerminated ? "+threadPool.isTerminated());
for (Runnable t: threadList) {
System.out.println(t.toString());
}
}
}
public class SimpleTask4 implements Runnable{
@Override
public void run() {
try {
Thread.sleep(100);
System.out.println(Thread.currentThread().getName());
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName()+"被中断!");
}
}
}
运行结果:
pool-1-thread-4
pool-1-thread-5
pool-1-thread-3
pool-1-thread-1
pool-1-thread-2
indi.chester.threadpool.SimpleTask4@330bedb4
indi.chester.threadpool.SimpleTask4@2503dbd3
indi.chester.threadpool.SimpleTask4@4b67cf4d
indi.chester.threadpool.SimpleTask4@7ea987ac
indi.chester.threadpool.SimpleTask4@12a3a380
isShutDown ? true
isTerminated ? true
被 shutDown 后 返回线程对象的List对象, 调用 isShutDown, isTerminated 立即返回 true.
awaitTermination
awaitTermination 并不能关闭线程池, 而是用来判断一段时间内线程池是否执行完所有任务, 返回一个 bool 值
public class AwaitTermination {
public static void main(String[] args) {
ExecutorService threadPool = Executors.newFixedThreadPool(2);
for (int i = 0; i < 10; i++) {
threadPool.execute(new SimpleTask3());
}
threadPool.shutdown();
try {
//两种情况返回 bool 值 1.到达等待时间, 2.所有任务执行完毕
//awaitTermination(parm1, parm2) 方法, 到达parm1(parm2時間單位)后, 线程池内的线程是否已经全部执行完毕
System.out.println(threadPool.awaitTermination(1, TimeUnit.SECONDS));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class SimpleTask3 implements Runnable{
@Override
public void run() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
}
}
输出结果
pool-1-thread-1
pool-1-thread-1
false
pool-1-thread-1
pool-1-thread-1
pool-1-thread-1
pool-1-thread-1
pool-1-thread-1
pool-1-thread-1
pool-1-thread-1
pool-1-thread-1
一秒以后, 线程池内的任务还没有执行完毕, 所以 awaitTermination() 返回 false.
我们将awaitTermination() 传入的参数改大一点 改成 5秒, 再次输出:
pool-1-thread-1
pool-1-thread-2
pool-1-thread-2
pool-1-thread-1
pool-1-thread-1
pool-1-thread-2
pool-1-thread-2
pool-1-thread-1
pool-1-thread-1
pool-1-thread-2
true
可以看出, 在后面的5秒内, 线程池内所有任务都已经处理完毕了
Last updated
Was this helpful?