devxlogo

Thread Timing

Thread Timing

Question:
How can I determine the total time it takes for a set of threads to execute?

Answer:
When you time a set of threads, you generally want to know the time elapsed between when the first thread starts and the last thread finishes. When you time a single thread, all you have to do is record a timestamp before starting the thread and another timestamp after a call to join(). This timing includes the overhead of creating thethread and joining the thread. If you don’t want to include these overheads, and want to time only the work executed, you have to do the timing inside the thread. The first timing method is noteasily extended to timing multiple threads, but the second method is.

Using the second method, you can create a wrapper class that implements the Runnable interface and places timing calls in its run() method. The TimingWrapper class in the code listing does this. Simple timing calls will not suffice, however, because they will result in timing each individual thread, rather than a group of threads. To solve this problem, you can create a timer class, likeThreadTimer, that tracks the starting and stopping of threads before recording a timestamp.

ThreadTimer is kind of like the guy who starts a car race. First he tells the drivers to start their engines, and then he starts the race. The ThreadTimer start() method makes sure all the threads have been created, and when the last one “starts its engine,” it records a beginning timestamp and tells all the threads they can start running. As the threads finish, they call the ThreadTimer stop() method, which counts the threads until the last one is done. At that point it records an ending timestamp and notifies the master timing thread,which should be waiting in waitForResult(), that the timing result is ready. ThreadTimer is designed to work with threads wrapped with TimingWrapper. The code listing demonstrates the timing approach by timing some threads that execute a dummy loop.

/*** * This program shows how you can time a set of threads.  The main() * program demonstrates the timing classes by spawning and timing a * set of threads that just execute a dummy loop. ***/public class ThreadTiming {  /** Times a group of threads.  Works with TimingWrapper. */  public static class ThreadTimer {    private boolean __resultReady;    private int __monitor[], __numThreads;    private long __begin, __end;    /** Creates a ThreadTimer that can time a given number of threads. */    public ThreadTimer(int numThreads) {      __monitor     = new int[1];      __monitor[0]  = 0;      __numThreads  = numThreads;      __resultReady = false;    }    /** Starts the timer when all the threads are ready to start. */    public void start() {      synchronized(__monitor) {        if(++__monitor[0] >= __numThreads) {          __begin = System.currentTimeMillis();          __monitor.notifyAll();        } else {          while(true) {            try {              __monitor.wait();            } catch(InterruptedException e) {              continue;            }            break;          }        }      }    }    /** Stops the timer when the last thread is done. */    public void stop() {      synchronized(__monitor) {        if(--__monitor[0] 
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist