public class Demo {
public Demo() {
long start = System.nanoTime();
new Thread(() ->{});
System.out.println(System.nanoTime()-start);
start = System.nanoTime();
new Thread(() ->{});
System.out.println(System.nanoTime()-start);
}
public static void main(String[] args) throws InterruptedException {
new Demo();
}
}
output:
35911135
245483
Can someone explain to me what's going on behind the scenes of new Thread(() ->{});
? I would like for both of these to execute at the same speed and have tried loading the Runnable class into memory in my main method with no results.
Why is the first execution slower than the second?
bonus question (answer only if there's an immediately apparant explanation): When these exact lines are run as part of my larger code base, my average execution time is approx. 60 ms, as opposed to the above demonstrated 35ms. Why might this be?