标签:err rmi 记录 prot 操作 cond long owa ide
ThreadPoolExecutor是可扩展的,它提供了几个可以在子类中改写的方法:beforeExecute、afterExecute、terminated。这些方法可以用于扩展ThreadPoolExecutor的行为。
在执行任务的线程中会调用beforeExecute和afterExecute等方法,在这些方法中可以添加日志、计时、监视或统计信息收集等功能。
新建一个自己的线程池类,继承ThreadPoolExecutor。按自己的需要重写方法。
/**
* 《Java并发编程实战》程序清单8-9
* 扩展ThreadPoolExecutor,实现一个自定义的线程池,
* 它通过beforeExecute、afterExecutor和terminated等方法添加日志记录和统计信息收集。
*
*/
public class _08_09_TimingThreadPool extends ThreadPoolExecutor {
public _08_09_TimingThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
super(1, 1, 0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
private final ThreadLocal<Long> startTime = new ThreadLocal<>();
private final Logger log = Logger.getLogger("_08_09_TimingThreadPool");
private final AtomicLong numTasks = new AtomicLong();
private final AtomicLong totalTime = new AtomicLong();
@Override
protected void beforeExecute(Thread t, Runnable r) {
super.beforeExecute(t, r);
log.fine(String.format("Thread %s: start %s", t, r));
startTime.set(System.nanoTime());
}
@Override
protected void afterExecute(Runnable r, Throwable t) {
try {
long endTime = System.nanoTime();
long taskTime = endTime - startTime.get();
numTasks.incrementAndGet();
totalTime.addAndGet(taskTime);
log.fine(String.format("Thread %s: end %s, time=%dns", t, r, taskTime));
} finally {
super.afterExecute(r, t);
}
}
protected void terminated() {
try {
log.info(String.format("Terminated: avg time=%dns", totalTime.get() / numTasks.get()));
} finally {
super.terminated();
}
Executors.newFixedThreadPool(10);
}
}
标签:err rmi 记录 prot 操作 cond long owa ide
原文地址:https://www.cnblogs.com/mengHeJiuQIan/p/11121131.html