J.U.C--扩展ThreadPoolExecutor

时间:2021-05-11 05:32:13

ThreadPoolExecutor是可以扩展的,它提供了几个在子类中改写的方法,比如:beforeExecute()、afterExecute()、和terminated()方法。这些方法可以扩展ThreadPoolExecutor的执行行为。比如剪监控线程池或则是添加日志之类的。

下面是一个例子,给线程池添加统计信息:

package thread;

import org.apache.log4j.Logger;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;

/** * Created by louyuting on 17/1/12. */
public class TimingThreadPool extends ThreadPoolExecutor{

    public TimingThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    public TimingThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
    }

    public TimingThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
    }

    public TimingThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
    }

    private final ThreadLocal<Long> startTime = new ThreadLocal<Long>();
    private final Logger log = Logger.getLogger("TimingThreadPool");
    private final AtomicLong numTasks = new AtomicLong();
    private final AtomicLong totalTime = new AtomicLong();
    @Override
    protected void beforeExecute(Thread t, Runnable r) {
        super.beforeExecute(t, r);
        System.out.println(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);
            System.out.println(String.format("Thread %s end %s time=%dns", t,r,taskTime));
        } finally {
            super.afterExecute(r,t);
        }
    }

    @Override
    protected void terminated() {
        super.terminated();
    }

    public static void main(String[] args) {
        TimingThreadPool t = new TimingThreadPool(1,1,0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>());
        for(int i=0; i<3; i++){
            t.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("test");
                }
            });
        }

        t.shutdown();
    }
}

运行结果如下
J.U.C--扩展ThreadPoolExecutor