在下面的代码中:
class Test {
public static void main(String [] args) {
printAll(args);
}
public static void printAll(String[] lines) {
for(int i=0;i<lines.length;i++){
System.out.println(lines[i]);
Thread.currentThread().sleep(1000);
}
}
}
将数组行中的每个String输出:
- 恰好每行之间有 1 秒的停顿?
- 每行之间至少 1 秒的停顿?
最佳答案
大约 1 秒的停顿。线程可以提前被唤醒,你会得到一个InterruptedException
,或者线程可以 hibernate 1000 毫秒,然后不会立即运行,所以它将是 1000 毫秒 + 微秒(或更多,如果有的话)是占用 CPU 的更高优先级线程)。
你也说错了。它是 Thread.sleep(1000);
,作为一个静态方法,它始终作用于当前线程,您不能让其他线程与它一起 hibernate 。
关于java - 如果只有一个线程在运行(main)并且调用了 sleep(1000),线程会 hibernate 1 秒还是至少 1 秒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30257608/