有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

从ThreadPoolExecutor重写用于日志记录的java函数

// 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(...)不应该同步吗?如果答案是否定的,那么原因是“局部变量被分配到线程堆栈中,状态变量如numtask、totalTime、startTime是线程受限/原子的”


共 (1) 个答案

  1. # 1 楼答案

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

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