Java併發編程之線程池任務監控
當我們提交runnable或者callable<?>到ThreadPoolExecutor時,我們是無法知道這些任務是在什麼時候才真正的執行的,爲了實現這個需求,我們需要擴展ThreadPoolExecutor,重寫beforeExecute和afterExecute,在這兩個方法裏分別做一些任務執行前和任務執行後的相關監控邏輯,還有個terminated方法,是在線程池關閉後回調,,另外,我們可以通過getLargestPoolSize()和getCompletedTaskCount()來分別獲取線程池數的峯值和線程池已完成的任務數。
下面就一個完整的例子來說明如何進行:
自定義MonitorHandler接口,把before和after抽象出來:
- package cc.lixiaohui.demo.concurrent;
- /**
- * 監控處理器, 目的是把before和after抽象出來, 以便在{@link MonitorableThreadPoolExecutor}中形成一條監控處理器鏈
- *
- * @author lixiaohui
- * @date 2016年10月11日 下午7:18:38
- *
- */
- public interface MonitorHandler {
- /**
- * 改監控任務是否可用
- *
- * @return
- */
- boolean usable();
- /**
- * 任務執行前回調
- *
- * @param thread 即將執行該任務的線程
- * @param runnable 即將執行的任務
- */
- void before(Thread thread, Runnable runnable);
- /**
- * <pre>
- * 任務執行後回調
- * 注意:
- * 1.當你往線程池提交的是{@link Runnable} 對象時, 參數runnable就是一個{@link Runnable}對象
- * 2.當你往線程池提交的是{@link java.util.concurrent.Callable<?>} 對象時, 參數runnable實際上就是一個{@link java.util.concurrent.FutureTask<?>}對象
- * 這時你可以通過把參數runnable downcast爲FutureTask<?>或者Future來獲取任務執行結果
- *
- * @param runnable 執行完後的任務
- * @param throwable 異常信息
- */
- void after(Runnable runnable, Throwable throwable);
- /**
- * 線程池關閉後回調
- *
- * @param largestPoolSize
- * @param completedTaskCount
- */
- void terminated(int largestPoolSize, long completedTaskCount);
- }
擴展ThreadPoolExecutor,增加監控的邏輯,如果監控比較耗時的話,爲了不影響業務線程池的執行效率,我們應該將before,after和terminated方法的調用封裝爲統一的Runnable交給非業務線程池內的Thread來跑(新建個Thread或者線程池):
- package cc.lixiaohui.demo.concurrent;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.concurrent.BlockingQueue;
- import java.util.concurrent.RejectedExecutionHandler;
- import java.util.concurrent.ThreadFactory;
- import java.util.concurrent.ThreadPoolExecutor;
- import java.util.concurrent.TimeUnit;
- /**
- * 可監控的線程池, 可有多個監控處理器,如果監控的邏輯是比較耗時的話, 最好另起個線程或者線程池專門用來跑MonitorHandler的方法.
- *
- * @author lixiaohui
- * @date 2016年10月11日 下午7:15:16
- *
- */
- public class MonitorableThreadPoolExecutor extends ThreadPoolExecutor {
- /**
- * 可有多個監控處理器
- */
- private Map<String, MonitorHandler> handlerMap = new HashMap<String, MonitorHandler>();
- private final Object lock = new Object();
- public MonitorableThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) {
- super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
- }
- public MonitorableThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
- super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
- }
- public MonitorableThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
- super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
- }
- public MonitorableThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
- super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
- }
- @Override
- protected void beforeExecute(Thread t, Runnable r) {
- super.beforeExecute(t, r);
- // 依次調用處理器
- for (MonitorHandler handler : handlerMap.values()) {
- if (handler.usable()) {
- handler.before(t, r);
- }
- }
- }
- @Override
- protected void afterExecute(Runnable r, Throwable t) {
- super.afterExecute(r, t);
- // 依次調用處理器
- for (MonitorHandler handler : handlerMap.values()) {
- if (handler.usable()) {
- handler.after(r, t);
- }
- }
- }
- /*
- * @see java.util.concurrent.ThreadPoolExecutor#terminated()
- */
- @Override
- protected void terminated() {
- super.terminated();
- for (MonitorHandler handler : handlerMap.values()) {
- if (handler.usable()) {
- handler.terminated(getLargestPoolSize(), getCompletedTaskCount());
- }
- }
- }
- public MonitorHandler addMonitorTask(String key, MonitorHandler task, boolean overrideIfExist) {
- if (overrideIfExist) {
- synchronized (lock) {
- return handlerMap.put(key, task);
- }
- } else {
- synchronized (lock) {
- return handlerMap.putIfAbsent(key, task);
- }
- }
- }
- public MonitorHandler addMonitorTask(String key, MonitorHandler task) {
- return addMonitorTask(key, task, true);
- }
- public MonitorHandler removeMonitorTask(String key) {
- synchronized (lock) {
- return handlerMap.remove(key);
- }
- }
- }
測試程序:
- package cc.lixiaohui.demo.concurrent;
- import java.io.IOException;
- import java.util.Map;
- import java.util.concurrent.Callable;
- import java.util.concurrent.CancellationException;
- import java.util.concurrent.ConcurrentHashMap;
- import java.util.concurrent.ExecutionException;
- import java.util.concurrent.Future;
- import java.util.concurrent.FutureTask;
- import java.util.concurrent.LinkedBlockingQueue;
- import java.util.concurrent.TimeUnit;
- import cc.lixiaohui.util.RandomUtils;
- /**
- * @author lixiaohui
- * @date 2016年10月11日 下午8:11:39
- *
- */
- public class Tester {
- static volatile boolean stop = false;
- public static void main(String[] args) throws InterruptedException, IOException {
- // fixed size 5
- final MonitorableThreadPoolExecutor pool = new MonitorableThreadPoolExecutor(5, 10, 30, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
- pool.addMonitorTask("TimeMonitorTask", newTimeMonitorHandler());
- // 起一個線程不斷地往線程池丟任務
- Thread t = new Thread(new Runnable() {
- public void run() {
- startAddTask(pool);
- }
- });
- t.start();
- // 丟任務丟20 ms
- Thread.sleep(50);
- stop = true;
- t.join();
- pool.shutdown();
- // 等線程池任務跑完
- pool.awaitTermination(100, TimeUnit.SECONDS);
- }
- private static MonitorHandler newTimeMonitorHandler() {
- return new MonitorHandler() {
- // 任務開始時間記錄map, 多線程增刪, 需用ConcurrentHashMap
- Map<Runnable, Long> timeRecords = new ConcurrentHashMap<Runnable, Long>();
- public boolean usable() {
- return true;
- }
- public void terminated(int largestPoolSize, long completedTaskCount) {
- System.out.println(String.format("%s:largestPoolSize=%d, completedTaskCount=%s", time(), largestPoolSize, completedTaskCount));
- }
- public void before(Thread thread, Runnable runnable) {
- System.out.println(String.format("%s: before[%s -> %s]", time(), thread, runnable));
- timeRecords.put(runnable, System.currentTimeMillis());
- }
- public void after(Runnable runnable, Throwable throwable) {
- long end = System.currentTimeMillis();
- Long start = timeRecords.remove(runnable);
- Object result = null;
- if (throwable == null && runnable instanceof FutureTask<?>) { // 有返回值的異步任務,不一定是Callable<?>,也有可能是Runnable
- try {
- result = ((Future<?>) runnable).get();
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt(); // reset
- } catch (ExecutionException e) {
- throwable = e;
- } catch (CancellationException e) {
- throwable = e;
- }
- }
- if (throwable == null) { // 任務正常結束
- if (result != null) { // 有返回值的異步任務
- System.out.println(String.format("%s: after[%s -> %s], costs %d millisecond, result: %s", time(), Thread.currentThread(), runnable, end - start, result));
- } else {
- System.out.println(String.format("%s: after[%s -> %s], costs %d millisecond", time(), Thread.currentThread(), runnable, end - start));
- }
- } else {
- System.err.println(String.format("%s: after[%s -> %s], costs %d millisecond, exception: %s", time(), Thread.currentThread(), runnable, end - start, throwable));
- }
- }
- };
- }
- // 隨機runnable或者callable<?>, 任務隨機拋異常
- private static void startAddTask(MonitorableThreadPoolExecutor pool) {
- int count = 0;
- while (!stop) {
- if (RandomUtils.randomBoolean()) {// 丟Callable<?>任務
- pool.submit(new Callable<Boolean>() {
- public Boolean call() throws Exception {
- // 隨機拋異常
- boolean bool = RandomUtils.randomBoolean();
- // 隨機耗時 0~100 ms
- Thread.sleep(RandomUtils.randomInt(100));
- if (bool) {
- throw new RuntimeException("thrown randomly");
- }
- return bool;
- }
- });
- } else { // 丟Runnable
- pool.submit(new Runnable() {
- public void run() {
- // 隨機耗時 0~100 ms
- try {
- Thread.sleep(RandomUtils.randomInt(100));
- } catch (InterruptedException e) {}
- // 隨機拋異常
- if (RandomUtils.randomBoolean()) {
- throw new RuntimeException("thrown randomly");
- }
- };
- });
- }
- System.out.println(String.format("%s:submitted %d task", time(), ++count));
- }
- }
- private static String time() {
- return String.valueOf(System.currentTimeMillis());
- }
- }
一個較短的結果:
- 1476253228222: before[Thread[pool-1-thread-1,5,main] -> java.util.concurrent.FutureTask@548bb979]
- 1476253228222:Thread[Thread-0,5,main], submitted 1 task
- 1476253228253:Thread[Thread-0,5,main], submitted 2 task
- 1476253228264: before[Thread[pool-1-thread-2,5,main] -> java.util.concurrent.FutureTask@97e041d]
- 1476253228264:Thread[Thread-0,5,main], submitted 3 task
- 1476253228265: before[Thread[pool-1-thread-3,5,main] -> java.util.concurrent.FutureTask@7d6d5cc]
- 1476253228271: after[Thread[pool-1-thread-2,5,main] -> java.util.concurrent.FutureTask@97e041d], costs 7 millisecond, exception: java.util.concurrent.ExecutionException: java.lang.RuntimeException: thrown randomly
- 1476253228295: after[Thread[pool-1-thread-1,5,main] -> java.util.concurrent.FutureTask@548bb979], costs 42 millisecond
- 1476253228347: after[Thread[pool-1-thread-3,5,main] -> java.util.concurrent.FutureTask@7d6d5cc], costs 82 millisecond, exception: java.util.concurrent.ExecutionException: java.lang.RuntimeException: thrown randomly
- 1476253228347:Thread[pool-1-thread-3,5,main], largestPoolSize=3, completedTaskCount=3