Thread.sleep

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]);
		}
	}

}

你可能感兴趣的:(java,thread)