持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第17天,点击查看活动详情
使用shutdownNow()方法实现代码如下:
public class ShutdownRun {
public static void main(String[] args) {
SecondRunnable secondRunnable = new SecondRunnable();
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 6, 0, TimeUnit.SECONDS,new LinkedBlockingQueue<>());
for (int i = 0; i < 4; i++) {
threadPoolExecutor.execute(secondRunnable);
}
threadPoolExecutor.shutdownNow();
}
}
运行结果如下:
任务没有完成,就被中断了!
任务没有完成,就被中断了!
进入catch中断了任务
进入catch中断了任务
java.lang.InterruptedException
at com.ozx.concurrentprogram.executor.service.SecondRunnable.run(SecondRunnable.java:17)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
java.lang.InterruptedException
at com.ozx.concurrentprogram.executor.service.SecondRunnable.run(SecondRunnable.java:17)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
从运行结果看出剩下的2个任务被取消,没有被运行。
修改代码如下:
public class SecondRunnable implements Runnable {
@Override
public void run() {
for (int i = 0; i < Integer.MAX_VALUE / 1000; i++) {
Math.random();
Math.random();
Math.random();
Math.random();
}
System.out.println("任务成功完成!");
}
}
运行类代码如下:
public class ShutdownRun {
public static void main(String[] args) {
SecondRunnable secondRunnable = new SecondRunnable();
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 6, 999, TimeUnit.SECONDS,new LinkedBlockingQueue<>());
for (int i = 0; i < 4; i++) {
threadPoolExecutor.execute(secondRunnable);
}
try {
Thread.sleep(1000);
threadPoolExecutor.shutdownNow();
System.out.println("main 结束");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
运行结果如下:
main 结束
任务成功完成!
任务成功完成!
从运行结果看出2个任务被成功执行,其他2个任务被取消运行,并且进程销毁。
修改运行类代码如下:
public class ShutdownRun {
public static void main(String[] args) {
SecondRunnable secondRunnable = new SecondRunnable();
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2, 6, 999, TimeUnit.SECONDS,new LinkedBlockingQueue<>());
for (int i = 0; i < 4; i++) {
threadPoolExecutor.execute(secondRunnable);
}
try {
Thread.sleep(1000);
threadPoolExecutor.shutdownNow();
threadPoolExecutor.execute(secondRunnable);
System.out.println("main 结束");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
运行结果如下:
Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task com.ozx.concurrentprogram.executor.service.SecondRunnable@2280cdac rejected from java.util.concurrent.ThreadPoolExecutor@1517365b[Shutting down, pool size = 2, active threads = 2, queued tasks = 0, 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 com.ozx.concurrentprogram.executor.controller.ShutdownRun.main(ShutdownRun.java:26)
任务成功完成!
任务成功完成!
2个任务被成功执行,其余2个任务被取消执行,而最后一个任务则拒绝执行,运行效果即抛出异常,并且在最后进程销毁。