pai*_*dly 3 java multithreading thread-sleep
在以下代码中:
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
将数组行中的每个String输出:
暂停约1秒钟.线程可以事先被唤醒,你会得到一个InterruptedException,或者线程可以休眠1000毫秒,然后不能立即运行,所以它将是1000毫秒+微秒(或更多,如果有更高优先级的线程占用CPU) .
你也称错了.它Thread.sleep(1000);是一个静态方法,它总是作用于当前线程,你不能让其他线程与它一起睡觉.
| 归档时间: |
|
| 查看次数: |
952 次 |
| 最近记录: |