欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Thread.sleep

程序员文章站 2022-07-14 16:15:49
...
Thread.sleep 在指定的毫秒数内让当前正在执行的线程休眠(暂停执行),就是线程睡眠一定的时间,也就是交出cpu一段时间,根据参数来决定暂停时间.让给等待序列中的下一个线程.Thread.sleep抛出InterruptedException. This is an exception that sleep throws when another thread interrupts the current thread while sleep is active。

以下代码来自java tutorials


public class SleepMessages {
public static void main(String args[]) throws InterruptedException {
String importantInfo[] = { "Mares eat oats", "Does eat oats",
"Little lambs eat ivy", "A kid will eat ivy too" };
for (int i = 0; i < importantInfo.length; i++) {
Thread.sleep(4000);
System.out.println(importantInfo[i]);
}
}

}
相关标签: thread Java