20200205

import java.util.Date;

public class TestSleepInterrupt {
	public static void main(String[] args) {
		MyThread thread = new MyThread();
		Thread t = new Thread(thread);
		t.start();
		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
//		t.interrupt();
		thread.setFlag(false);
	}
}

class MyThread implements Runnable {
	private boolean flag = true;

	@Override
	public void run() {
		while (flag) {
			System.out.println("===" + new Date() + "===");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				return;
			}
		}
	}
	
	public void setFlag(boolean b) {
		this.flag = b;
	}
}
本站公眾號
   歡迎關注本站公眾號,獲取更多信息