Created
October 9, 2013 06:26
-
-
Save splatch/6897043 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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