java-Thread的interrupt笔记

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ytlviv1985/article/details/79262094

1.interrupt() 中断线程

     * <p> If this thread is blocked in an invocation of the {@link
     * Object#wait(long, int) wait(long, int)} methods of the {@link Object}
     * class, or of the {@link #join()}, {@link #join(long)}, {@link
     * #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)},
     * methods of this class, then its interrupt status will be cleared and it
     * will receive an {@link InterruptedException}.

     * Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link

也就是只有在wait,join,sleep的时候才能抛出interruptedException,并且清除了中断状态,其他情况不会抛出中断异常

通过代码证明下

a.第一种情况 在不是wait,join,sleep的时候调用interrupt方法

public class Interrupt1 {

	public static void main(String[] args) throws InterruptedException {
		ThreadTest thread=new ThreadTest();
		Thread t=new Thread(thread);
		t.start();
		Thread.sleep(1000);
		t.interrupt();
		System.out.println("main 线程执行完毕");
	}
	
	public static class ThreadTest implements Runnable{

		public void run() {
			Thread curr=Thread.currentThread();
			System.out.println("===="+curr.isInterrupted());
			while(true){
				if(curr.isInterrupted()){
					System.out.println("===="+curr.isInterrupted());
					break;
				}
				
				if(1==2)
					break;
			}
			System.out.println("^^^^^^^");
		}
		
	}
}
执行结果:


====false
main 线程执行完毕
====true
^^^^^^^

从结果可以看出并没有抛出interruptException,并且curr.isInterrupted方法返回了ture,说明中断状态也没有被清除

b.在sleep的时候在调用interrupte方法看下什么情况,run方法加下sleep

			Thread curr=Thread.currentThread();
			System.out.println("===="+curr.isInterrupted());
			while(true){
				
				try {
					Thread.sleep(200);
				} catch (InterruptedException e) {
					e.printStackTrace();
					System.out.println("==="+curr.isInterrupted());
				}
				
				if(curr.isInterrupted())
					System.out.println("000000000");
				
				if(1==2)
					break;
			}
			System.out.println("^^^^^^^");

执行结果:

====false
main 线程执行完毕
java.lang.InterruptedException: sleep interrupted
===false
at java.lang.Thread.sleep(Native Method)
at a.Interrupt1$ThreadTest.run(Interrupt1.java:22)
at java.lang.Thread.run(Unknown Source)


确实抛出了interruptedException,并且isinterrupted()返回了false说明中断状态被清除了,后面的判断就不管用了,所以拿这个方法中断线程还是不安全,自己用变量控制更靠谱


2.静态方法public static boolean interrupted()

     * Tests whether the current thread has been interrupted.  The
     * <i>interrupted status</i> of the thread is cleared by this method.  In
     * other words, if this method were to be called twice in succession, the
     * second call would return false (unless the current thread were
     * interrupted again, after the first call had cleared its interrupted
     * status and before the second call had examined it).

检查当前中断状态,并且清除中断状态,连续调用2此的第二次肯定返回false
			Thread curr=Thread.currentThread();
			System.out.println("===="+curr.isInterrupted());
			while(true){
				
				try {
					Thread.sleep(200);
				} catch (InterruptedException e) {
					e.printStackTrace();
					curr.interrupt();
					System.out.println("先变成true==="+curr.isInterrupted());
					System.out.println("curr.isInterrupted==="+Thread.interrupted());
					System.out.println("中断状态清除:"+curr.isInterrupted());
					System.out.println("curr.isInterrupted==="+Thread.interrupted());
					System.out.println("====c"+curr.isInterrupted());
				}
				
				if(curr.isInterrupted())
					System.out.println("000000000");
				
				if(1==2)
					break;
			}
			System.out.println("^^^^^^^");
		}

执行结果:
====false
main 线程执行完毕
java.lang.InterruptedException: sleep interrupted
先变成true===true
curr.isInterrupted===true
====bfalse
curr.isInterrupted===false
====cfalse
at java.lang.Thread.sleep(Native Method)
at a.Interrupt1$ThreadTest.run(Interrupt1.java:22)
at java.lang.Thread.run(Unknown Source)
内部调用是这样的
    public boolean isInterrupted() {
        return isInterrupted(false);
    }
    public static boolean interrupted() {
        return currentThread().isInterrupted(true);
    }
private native boolean isInterrupted(boolean ClearInterrupted);
isInterrupted不清除,静态方法的清除









猜你喜欢

转载自blog.csdn.net/ytlviv1985/article/details/79262094