1

In Java, I have simple multithreaded code:

public class ThreadedAlgo {

    public static final int threadsCount = 3;

    public static void main(String[] args) {

        // start timer prior computation
        time = System.currentTimeMillis();

        // create threads
        Thread[] threads = new Thread[threadsCount];

        class ToDo implements Runnable {
            public void run() { ... }
        }

        // create job objects
        for (int i = 0; i < threadsCount; i++) {
            ToDo job = new ToDo();
            threads[i] = new Thread(job);
        }

        // start threads
        for (int i = 0; i < threadsCount; i++) {
            threads[i].start();
        }

        // wait for threads above to finish
        for (int i = 0; i < threadsCount; i++) {
            try {
                threads[i].join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        // display time after computation
        System.out.println("Execution time: " + (System.currentTimeMillis() - time));

    }

}

It works fine, now I want to run it for 2 or 3 threads and compute the time spent for computation of each thread. Then I will compare times: note them by t1 and t2, and if |t1 - t2| < small epsilon, I will say that my algorithm performs with fine granularity under some given conditions, that is the time spent by threads is relatively the same.

How can I measure the time of a thread?

  • I think you'd be much better served by using a profiling tool, instead of trying to read a timer. And even with a timer, I honestly wouldn't trust any numbers smaller than milliseconds. "Nanosecond" results are pure Science Fiction: IMHO. SUGGESTION: Here's an excellent tutorial on the Eclipse Profiler: http://www.eclipse.org/tptp/home/documents/tutorials/profilingtool/profilingexample_32.html – paulsm4 Sep 21 '13 at 21:08

2 Answers2

0

Use System.nanoTime() at the beginning and end of the thread (job) methods to calculate the total time spent in each invocation. In your case, all threads will be executed with the same (default) priority, where time slices should be distributed pretty fair. If your threads are interlocked, use 'fair locks' for the same reason; e.g. new ReentrantLock(true);

Sam
  • 7,778
  • 1
  • 23
  • 49
  • No, my threads are independent, so ideal for measuring. Is `System.currentTimeMillis()` not nice way of time measuring? – user2651246 Sep 21 '13 at 21:06
  • Doesn't really matter, I just prefer the highest possible resolution for such things. If threads don't run long enough for millisecond resolution, the overhead of thread creation is possibly more of a problem. – Sam Sep 21 '13 at 21:07
  • 1
    Just a heads up, that System.nanoTime may return seemingly random values. see http://stackoverflow.com/questions/510462/is-system-nanotime-completely-useless – Zagrev Sep 22 '13 at 03:00
0

Add the timing logic inside your Run methods

dkatzel
  • 31,188
  • 3
  • 63
  • 67