java 线程中断

import java.util.concurrent.TimeUnit;

public class TestInterrupted {
	public static void main(String[] args) throws Exception {
		Thread t1 = new Thread(new TestRunner());
		Thread t2 = new Thread(new TestRunner2());
		//t1.setDaemon(true);		
		t1.start();
		t2.start();
		TimeUnit.SECONDS.sleep(3);		
		t1.interrupt();
		t2.interrupt();
		System.out.println("t1.isInterrupted(): "+ t1.isInterrupted());
		System.out.println("t2.isInterrupted(): "+ t2.isInterrupted());
	}	
	 static class TestRunner implements Runnable{
		@Override
		public void run() {
			while(!Thread.interrupted()){
			}						
		}		
	}
	 
	 static class TestRunner2 implements Runnable{
		@Override
		public void run() {
			while(true){
			}			
		}
		
	}

}

 输出:

t1.isInterrupted(): false

t2.isInterrupted(): true

并且程序不会停止。TestRunner2仍然在运行 

Thread.interrupted()会清楚中断状态位。

import java.util.concurrent.TimeUnit;

public class TestInterrupted {
	public static void main(String[] args) throws Exception {
		Thread t1 = new Thread(new TestRunner());
		Thread t2 = new Thread(new TestRunner2());
		//t1.setDaemon(true);		
		t1.start();
		t2.start();
		TimeUnit.SECONDS.sleep(3);		
		t1.interrupt();
		t2.interrupt();
		System.out.println("t1.isAlive(): "+ t1.isAlive());
		System.out.println("t2.isAlive(): "+ t2.isAlive());
		System.out.println("t1.isInterrupted(): "+ t1.isInterrupted());
		System.out.println("t2.isInterrupted(): "+ t2.isInterrupted());
	}	
	 static class TestRunner implements Runnable{
		@Override
		public void run() {
			while(!Thread.interrupted()){
			}						
		}		
	}
	 
	 static class TestRunner2 implements Runnable{
		 boolean flag = true;
		@Override
		public void run() {
			while(!Thread.currentThread().isInterrupted()){
				//System.out.println(Thread.currentThread().isInterrupted());
			}
			
		}
		
	}

}

 输出:

t1.isAlive(): false

t2.isAlive(): false

t1.isInterrupted(): false

t2.isInterrupted(): false

我的理解: 线程执行结束后,中断标志位也就被清除了。

import java.util.concurrent.TimeUnit;

public class TestInterrupted2 {
	public static void main(String[] args) throws Exception {
		Thread t1 = new Thread(new TestRunner());
		Thread t2 = new Thread(new TestRunner2());
		//t1.setDaemon(true);		
		t1.start();
		t2.start();
		TimeUnit.SECONDS.sleep(3);		
		t1.interrupt();
		t2.interrupt();
		System.out.println("t1.isAlive(): "+ t1.isAlive());
		System.out.println("t2.isAlive(): "+ t2.isAlive());
		TimeUnit.SECONDS.sleep(3);	
		System.out.println("t1.isInterrupted(): "+ t1.isInterrupted());
		System.out.println("t2.isInterrupted(): "+ t2.isInterrupted());
	}	
	 static class TestRunner implements Runnable{
		@Override
		public void run() {
			while(true){
			}						
		}		
	}
	 
	 static class TestRunner2 implements Runnable{
		 boolean flag = true;
		@Override
		public void run() {
			while(true){
				try {
					TimeUnit.SECONDS.sleep(3);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}	
			}
			
		}
		
	}

}

 输出:

t1.isAlive(): true

t2.isAlive(): true

java.lang.InterruptedException: sleep interrupted

at java.lang.Thread.sleep(Native Method)

at java.lang.Thread.sleep(Thread.java:340)

at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)

at TestInterrupted2$TestRunner2.run(TestInterrupted2.java:33)

at java.lang.Thread.run(Thread.java:745)

t1.isInterrupted(): true

t2.isInterrupted(): false

声明抛出InterruptedException的方法会在抛出InterruptedException之前,清除中断状态位。

猜你喜欢

转载自edgar108.iteye.com/blog/2282977