Skip to content

Instantly share code, notes, and snippets.

@splatch
Created October 9, 2013 06:26
Show Gist options
  • Select an option

  • Save splatch/6897043 to your computer and use it in GitHub Desktop.

Select an option

Save splatch/6897043 to your computer and use it in GitHub Desktop.
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
public class InstrumentingThreadPoolExecutor extends ThreadPoolExecutor {
private ThreadLocal<Long> startTime = new ThreadLocal<Long>();
private long lastExecutionTime;
private AtomicLong executionsCount = new AtomicLong();
private AtomicLong executionsTime = new AtomicLong();
public InstrumentingThreadPoolExecutor(int corePoolSize,
int maximumPoolSize, long keepAliveTime, TimeUnit unit,
BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
}
@Override
protected void beforeExecute(Thread t, Runnable r) {
startTime.set(System.currentTimeMillis());
super.beforeExecute(t, r);
}
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
long execution = System.currentTimeMillis() - startTime.get();
lastExecutionTime = execution;
executionsCount.incrementAndGet();
executionsTime.addAndGet(execution);
}
public long getLastExecutionTime() {
return lastExecutionTime;
}
public long getExecutionsCount() {
return executionsCount.get();
}
public long getExecutionsTime() {
return executionsTime.get();
}
}
import javax.management.NotCompliantMBeanException;
import javax.management.StandardMBean;
public class InstrumentingThreadPoolExecutorImpl extends StandardMBean implements InstrumentingThreadPoolExecutorMBean {
private final InstrumentingThreadPoolExecutor instr;
public InstrumentingThreadPoolExecutorImpl(InstrumentingThreadPoolExecutor instr) throws NotCompliantMBeanException {
super(InstrumentingThreadPoolExecutorMBean.class);
this.instr = instr;
}
@Override
public long getExecutionsCount() {
return instr.getExecutionsCount();
}
@Override
public long getExecutionsTime() {
return instr.getExecutionsTime();
}
@Override
public long getLastExecutionTime() {
return instr.getLastExecutionTime();
}
@Override
public double getAverageExecutionTime() {
return instr.getExecutionsTime() / instr.getExecutionsCount();
}
@Override
public int getMaximumPoolSize() {
return instr.getMaximumPoolSize();
}
@Override
public int getActiveCount() {
return instr.getActiveCount();
}
}
public interface InstrumentingThreadPoolExecutorMBean {
long getExecutionsCount();
long getExecutionsTime();
long getLastExecutionTime();
double getAverageExecutionTime();
int getMaximumPoolSize();
int getActiveCount();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment