Thread.sleep

767 ワード

Thread.sleepは、現在実行中のスレッドを所定のミリ秒以内に休止させる(実行を一時停止する)。つまり、スレッド睡眠は一定の時間、つまり、cpuを出すまでの間、パラメータによって一時停止時間を決定します。次のスレッドを待つために、Thread.sleepにInterruptedException.This is an exception throws when Another thread interrupts the current thad read while sleepis activeをスローします。
以下のコードはjava tutorisから来ます。

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

}