スレッドsleepメソッド

729 ワード


public class Runnable2 implements Runnable{
	public void run() {
		while (true) {
			System.out.println("----" + new Date()+"=======");
			
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				return;
			}
		}
	}
}

public class TestThread2 {
	public static void main(String[] args) {
		Runnable2 r2 = new Runnable2();
		Thread t = new Thread(r2);
		t.start();
		
		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			
			e.printStackTrace();
		}
		t.interrupt(); //       
	}
	
}