スレッドの停止

public class test3 extends Thread {
	
	static boolean flag = false;
	
	public static void main(String[] args) throws InterruptedException{
		test3 test = new test3();
		test.start();
		Thread.sleep(30);
		System.out.println("flag sets true.");
		flag = true;
	}
	
	public void run() {
		while(!flag) {
			System.out.println(Thread.currentThread().getName() + " is running.");
		}
		System.out.println(Thread.currentThread().getName() + " is terminated.");
	}
}

実行結果

省略...
Thread-0 is running.
Thread-0 is running.
Thread-0 is running.
Thread-0 is running.
Thread-0 is running.
Thread-0 is running.
Thread-0 is running.
Thread-0 is running.
flag sets true.
Thread-0 is running.
Thread-0 is terminated.