Java自定義線程池-記錄每個線程執行耗時


  ThreadPoolExecutor是可擴展的,其提供了幾個可在子類化中改寫的方法,如下:

protected void beforeExecute(Thread t, Runnable r) { }
protected void afterExecute(Runnable r, Throwable t) { }
protected void terminated() { }

  現基於此,完成一個統計每個線程執行耗時,並計算平均耗時的 自定義線程池樣例。通過 beforeExecute、afterExecute、terminated 方法來添加日志記錄和統計信息收集。為了測量任務的運行時間,beforeExecute必須記錄開始時間並把它保存到一個ThreadLocal變量中,然后由afterExecute來讀取。同時,使用兩個 AtomicLong變量,分別用以記錄已處理的任務數和總的處理時間,並通過terminated來輸出包含平均任務時間的日志消息。

  自定義線程池代碼如下:

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Logger;

/**
 * 自定義線程池
 */
public class TimingThreadPool extends ThreadPoolExecutor {

    private final ThreadLocal<Long> startTime = new ThreadLocal<>();
    private final Logger log = Logger.getLogger("TimingThreadPool");
    private final AtomicLong numTasks = new AtomicLong();
    private final AtomicLong totalTime = new AtomicLong();

    public TimingThreadPool(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);
        log.info(String.format("Thread %s: start %s",t,r));
        startTime.set(System.nanoTime());
    }

    @Override
    protected void afterExecute(Runnable r, Throwable t) {
        try {
            long endTime = System.nanoTime();
            long taskTime = endTime - startTime.get();
            numTasks.incrementAndGet();
            totalTime.addAndGet(taskTime);
            log.info(String.format("Thread %s: end %s, time=%dns",t,r,taskTime));

        } finally {
            super.afterExecute(r,t);
        }
    }

    @Override
    protected void terminated() {
        try {
            log.info(String.format("Terminated: avg time=%dns",totalTime.get() / numTasks.get()));
        } finally {
            super.terminated();
        }
    }
}

  測試執行效果代碼如下:

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * 測試自定義線程池
 */
public class TestCustomThreadPool {


    public static void main(String[] args) {

        try {
            TimingThreadPool threadPool = new TimingThreadPool(10,10,0L, TimeUnit.MILLISECONDS,
                    new LinkedBlockingQueue<Runnable>());

            List<TestCallable> tasks = new ArrayList<>();

            for (int i = 0 ; i < 100 ; i++) {
                tasks.add(new TestCallable());
            }

            List<Future<Long>> futures = threadPool.invokeAll(tasks);
            for (Future<Long> future :
                    futures) {
                System.out.print(" - "+future.get());
            }
            threadPool.shutdown();

        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    static class TestCallable implements Callable<java.lang.Long> {

        @Override
        public Long call() throws Exception {
            long total = 0 ;
            for (int i = 0 ; i < 100 ; i++) {
                long now = getRandom();
                total += now;
            }
            Thread.sleep(total);
            return total;
        }

        public long getRandom () {
            return Math.round(Math.random() * 10);
        }
    }

}

執行結果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM