public class MyExecutor extends ThreadPoolExecutor {
public MyExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
startTimes=new ConcurrentHashMap<>();
}
private ConcurrentHashMap
startTimes;
@Override
protected void beforeExecute(Thread t, Runnable r) {
startTimes.put(String.valueOf(r.hashCode()), new Date());
}
@Override
protected void afterExecute(Runnable r, Throwable t) {
Future> result=(Future>)r;
try {
Date startDate=startTimes.remove(String.valueOf(r.hashCode()));
Date finishDate=new Date();
long diff=finishDate.getTime()-startDate.getTime();
System.out.printf("MyExecutor: Duration: %d\n",diff);
} catch (InterruptedException | ExecutionException e) { e.printStackTrace();}
}
}
阅读(5146) | 评论(0) | 转发(0) |