函数覆盖表单ThreadPoolExecutor以进行日志记录

时间:2013-02-01 12:09:35

标签: java concurrency

// code exactly taken from java concurrency in practice

public class TimingThreadPool extends ThreadPoolExecutor {
  private final ThreadLocal<Long> startTime
    = new ThreadLocal<Long>();
  private final Logger log = Logger.getLogger("TimingThreadPool");
  private final AtomicLong numTasks = new AtomicLong();
  private final AtomicLong totalTime = new AtomicLong();

  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());
  }

  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();
    }
}
}

ThreadPoolExecutor实例是否由所有工作线程共享。如果是,则方法beforeExecute(...)afterExecute(...)不应同步?如果答案为否则原因是“局部变量在线程堆栈中分配,状态变量如numTasks,totalTime,startTime是线程限制/原子”。

1 个答案:

答案 0 :(得分:0)

您不需要同步beforeExecute和afterExecute,因为这两个方法中使用的所有字段都是线程安全的java.util.logging.Logger(来自API - Logger上的所有方法都是多线程安全的。)或ThreadLocal和AtocmicLong专为无锁使用而设计。

对于局部变量,它们只能被一个线程访问,不需要任何同步。