Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9222417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:49:53+00:00 2026-06-18T03:49:53+00:00

// code exactly taken from java concurrency in practice public class TimingThreadPool extends ThreadPoolExecutor

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

Is the ThreadPoolExecutor instance is shared by all the worker threads. If yes then the method beforeExecute(...) and afterExecute(...) shouldn’t be synchronized? If the answer is NO then is the reason is ” the local variables are assigned in thread stack and the state variables like numTasks, totalTime, startTime are thread confined / atomic”.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T03:49:54+00:00Added an answer on June 18, 2026 at 3:49 am

    You do not need to synchronized beforeExecute and afterExecute because all fields used in these two methods are thread-safe java.util.logging.Logger (from API – all methods on Logger are multi-thread safe.) or ThreadLocal and AtocmicLong specially designed for lock-free usage.

    As for local variables they are accessible to only one thread and do not need any synchronization.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code is taken from the JavaDoc of Condition : class BoundedBuffer {
I am trying to port some code from a regular Java program into the
This is a small snippet of code taken from some of the examples that
Is it possible in Java code to divide two obtained values from two EditText
I am an ExtJS beginner, below is the very simple code fragment taken from
I just added some code that was taken from the Apple docs which shows
I've just been experimenting with MPI, and copied and ran this code, taken from
I don't get some of the syntax in this block of code taken from
you can view full source code here dpaste.com/hold/167199 Error: delete() takes exactly 2 arguments
I am getting the following error when running my code: Typeerror: lambda() takes exactly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.