package me.imlc.example; import java.util.Collection; import java.util.List; import java.util.concurrent.*; public class MeasurableExecutorService extends ThreadPoolExecutor { public MeasurableExecutorService(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue) { super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue); } public MeasurableExecutorService(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory) { super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory); } public MeasurableExecutorService(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, RejectedExecutionHandler handler) { super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler); } public MeasurableExecutorService(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) { super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler); } @Override public void execute(Runnable command) { super.execute(new MeasurableRunnable(command)); } @Override protected RunnableFuture newTaskFor(Runnable runnable, T value) { return super.newTaskFor(runnable, value); } @Override protected RunnableFuture newTaskFor(Callable callable) { return super.newTaskFor(callable); } @Override public Future submit(Runnable task) { return super.submit(task); } @Override public Future submit(Runnable task, T result) { return super.submit(task, result); } @Override public Future submit(Callable task) { return super.submit(task); } @Override public T invokeAny(Collection> tasks) throws InterruptedException, ExecutionException { return super.invokeAny(tasks); } @Override public T invokeAny(Collection> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { return super.invokeAny(tasks, timeout, unit); } @Override public List> invokeAll(Collection> tasks) throws InterruptedException { return super.invokeAll(tasks); } @Override public List> invokeAll(Collection> tasks, long timeout, TimeUnit unit) throws InterruptedException { return super.invokeAll(tasks, timeout, unit); } public class MeasurableRunnable implements Runnable { private Runnable runnable; MeasurableRunnable(Runnable runnable) { this.runnable = runnable; } @Override public void run() { long start = System.nanoTime(); runnable.run(); long end = System.nanoTime(); System.out.println(Thread.currentThread() + ": " + ((end - start) / 1000000) + "ms"); } } }