| | |
| | | package com.panzhihua.common.utlis; |
| | | |
| | | import java.util.concurrent.*; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.util.concurrent.*; |
| | | |
| | | /** |
| | | * 线程相关工具类. |
| | |
| | | } |
| | | |
| | | /** |
| | | * 停止线程池 |
| | | * 先使用shutdown, 停止接收新任务并尝试完成所有已存在任务. |
| | | * 如果超时, 则调用shutdownNow, 取消在workQueue中Pending的任务,并中断所有阻塞函数. |
| | | * 如果仍人超時,則強制退出. |
| | | * 停止线程池 先使用shutdown, 停止接收新任务并尝试完成所有已存在任务. 如果超时, 则调用shutdownNow, 取消在workQueue中Pending的任务,并中断所有阻塞函数. 如果仍人超時,則強制退出. |
| | | * 另对在shutdown时线程本身被调用中断做了处理. |
| | | */ |
| | | public static void shutdownAndAwaitTermination(ExecutorService pool) { |
| | |
| | | if (!pool.awaitTermination(120, TimeUnit.SECONDS)) { |
| | | pool.shutdownNow(); |
| | | if (!pool.awaitTermination(120, TimeUnit.SECONDS)) { |
| | | // log.info("Pool did not terminate"); |
| | | // log.info("Pool did not terminate"); |
| | | |
| | | } |
| | | } |
| | |
| | | public static void printException(Runnable r, Throwable t) { |
| | | if (t == null && r instanceof Future<?>) { |
| | | try { |
| | | Future<?> future = (Future<?>) r; |
| | | Future<?> future = (Future<?>)r; |
| | | if (future.isDone()) { |
| | | future.get(); |
| | | } |
| | |
| | | } |
| | | } |
| | | if (t != null) { |
| | | // log.error(t.getMessage(), t); |
| | | // log.error(t.getMessage(), t); |
| | | } |
| | | } |
| | | } |