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();
}
}
输出:
35911135
245483
有人可以向我解释new Thread(() ->{});
幕后发生的事情吗?我想这两个都以相同的速度执行,并尝试在我的main方法中将Runnable类加载到内存中而没有结果。
为什么第一次执行比第二次执行慢?
红利问题(只有在有明确说明的解释时才回答): 当这些确切的行作为我的较大代码库的一部分运行时,我的平均执行时间约为。 60毫秒,而不是上面显示的35毫秒。为什么会这样?