I want to measure the full execution time (when ALL threads are done). But my code won't work here, because when the main-method ends while the other threads will still be running because they take longer time to process than the main-method.
class Hello extends Thread {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("Hello");
try {
Thread.sleep(500);
} catch (final Exception e) {
}
}
}
}
class Hi extends Thread {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("Hi");
try {
Thread.sleep(500);
} catch (final Exception e) {
}
}
}
}
public class MultiThread {
public static void main(String[] args) {
final long startTime = System.nanoTime();
final Hello hello = new Hello();
final Hi hi = new Hi();
hello.start();
hi.start();
final long time = System.nanoTime() - startTime;
System.out.println("time to execute whole code: " + time);
}
}
I am trying to find get the execution time when a program is run on a single thread v/s multithread using of System.nanoTime()
to measure time.