0

I'm attempting to find the difference in running time between multi and single core solutions in Java. So far, the single core runs fine. However, I'm getting erratic reports from the multicore solution, some reporting that it took 0 nanoseconds, or slightly more.

The multicore reads as follow: (Notice that I'm not doing anything too advanced, just trying to find the length of time it ran. CPU execution time in Java convinced me to use System.nanoTime() instead of .currentTimeMillis() )

 long start = System.nanoTime();
 Runnable r0 = new FindMagicBrute(n);
 Thread t0 = new Thread(r0);
 Thread t1 = new Thread(r0);
 Thread t2 = new Thread(r0);
 t0.start();
 t1.start();
 t2.start();
 long end = System.nanoTime();
 System.out.println("Multithreaded run ended in " + (end-start) + " nanoseconds.");

Is there a way to determine when a thread has stopped executing?

Community
  • 1
  • 1
SomeKittens
  • 38,868
  • 19
  • 114
  • 143
  • Are you sure what you are doing here? You are starting three threads and then measure the time of the thread which started the other three. No suprise it could be 0. – Jagger Feb 28 '12 at 19:07

1 Answers1

2

You should wait for the threads to finish by calling join on each of them.

Otherwise, you're not timing anything but the time it takes to create the threads.

Mat
  • 202,337
  • 40
  • 393
  • 406