Site Search:

TimingThreadPool.java

Back>

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

public class TimingThreadPool extends ThreadPoolExecutor {

    public TimingThreadPool() {
        super(1, 1, 0L, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(5));
        log.addHandler(new ConsoleHandler());
    }
    
    public static void main(String...args) {
        ExecutorService exec = new TimingThreadPool();
        exec.submit(() -> System.out.println("a"));
        exec.submit(() -> System.out.println("b"));
        exec.submit(() -> System.out.println("c"));
        exec.submit(() -> System.out.println("d"));
        exec.shutdown();
    }

    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();

    protected void beforeExecute(Thread t, Runnable r) {
        super.beforeExecute(t, r);
        log.fine(String.format("Thread %s: start %s", t, r));
        startTime.set(System.nanoTime());
    }

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

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

}


TimingThreadPool extends ThreadPoolExecutor, added logging to collect information about thread lifecycle.