阅读背景:

一个例子说明Thread interrupt() interrupted() isInterrupted()三个方法关系和区别

来源:互联网 

直接贴上例子

 

public class InteruptTest extends Thread {
	static int i = 0;

	@Override
	public void run() {
		while (!Thread.currentThread().isInterrupted()) {
			// i happy run , please break me
			System.out.println("I"m runing " + i++);
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				Thread.currentThread().interrupt();//①:发出中止要求,设置中止状况
				System.out.println(Thread.currentThread().isInterrupted());//②:断定中止状况(不消除中止状况)
				System.out.println(Thread.interrupted());//③:断定中止状况(消除中止状况)
			}
			System.out.println("current thread haven"t been broken");
		}

	}

	public static void main(String[] args) throws InterruptedException {
		Thread t1 = new InteruptTest();
		t1.start();
		Thread.sleep(1000);
		t1.interrupt();
	}
}public class InteruptTest extends Thread




你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: