SomeKittens
SomeKittens

Reputation: 39522

Finding execution time for multicore code in Java

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?

Upvotes: 0

Views: 182

Answers (1)

Mat
Mat

Reputation: 206689

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.

Upvotes: 2

Related Questions