Skip to content

Instantly share code, notes, and snippets.

@lawrenceching
Created September 12, 2020 16:49
Show Gist options
  • Save lawrenceching/1b05953f0b28ccd50256f63dc4df959e to your computer and use it in GitHub Desktop.
Save lawrenceching/1b05953f0b28ccd50256f63dc4df959e to your computer and use it in GitHub Desktop.
Measure Runnable execution time
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<Runnable> workQueue) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
}
public MeasurableExecutorService(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
}
public MeasurableExecutorService(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
}
public MeasurableExecutorService(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> 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 <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
return super.newTaskFor(runnable, value);
}
@Override
protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
return super.newTaskFor(callable);
}
@Override
public Future<?> submit(Runnable task) {
return super.submit(task);
}
@Override
public <T> Future<T> submit(Runnable task, T result) {
return super.submit(task, result);
}
@Override
public <T> Future<T> submit(Callable<T> task) {
return super.submit(task);
}
@Override
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException {
return super.invokeAny(tasks);
}
@Override
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
return super.invokeAny(tasks, timeout, unit);
}
@Override
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {
return super.invokeAll(tasks);
}
@Override
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> 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");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment