关于Thread类中三个interrupt方法的研究与学习

先看三个方法原型:
 public void interrupt();
 public boolean isInterrupted();
 public static boolean interrupted();

 一、先说interrupt()方法,看注释
 Interrupts this thread.

Unless the current thread is interrupting itself, which is always permitted, the checkAccess method of this thread is invoked, which may cause a SecurityException to be thrown.

If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.

If this thread is blocked in an I/O operation upon an interruptible channel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a java.nio.channels.ClosedByInterruptException.

If this thread is blocked in a java.nio.channels.Selector then the thread's interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector's wakeup method were invoked.

If none of the previous conditions hold then this thread's interrupt status will be set.

Interrupting a thread that is not alive need not have any effect.

Throws:
SecurityException - if the current thread cannot modify this thread

 意思是说,当这个线程刚好或即将被阻塞在wait,join,sleep方法的时候,调用这个方法会引起这个线程的interrupt状态被清空(设为false),并且前者三个方法会抛出InterruptedException。
 除此之外(这个线程不处于wait,join,sleep方法),这个线程的interrupt状态会被设置(设为true)。

 

 二、isInterrupted()方法,看源码:

  public boolean isInterrupted() {
  return isInterrupted(false);
 }
  private native boolean isInterrupted(boolean ClearInterrupted);


 

  看注释:
  Tests whether this thread has been interrupted. The interrupted status of the thread is unaffected by this method. 


  意思是说:回这个线程是否被interrupt了,调用这个方法不会影响这个线程的interrupt状态

 

  三、public static boolean interrupted();看源码:
   

public static boolean interrupted() {
  return currentThread().isInterrupted(true);
 }
 private native boolean isInterrupted(boolean ClearInterrupted);


 

 看注释:
 Tests whether the current thread has been interrupted. The interrupted status 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). 


 意思是说:调用这个方法会返回当前线程的interrupt状态(true或false),并把当前线程的interrupt状态清空(设为false)。
 注意:这个是个静态方法,并且返回的是当前线程状态,并不一定是调用者的线程状态

 

看例子:
 例一:

@Test
	public void test1(){
		Thread t1= new Thread(){
			@Override
			public void run() {
				System.out.println("begin");
				for(int i=0;i<100;i++){
					System.out.println("i="+i+" "+this.isInterrupted());
					
					try {
						Socket socket= new Socket("10.22.1.115",23);//不存在的ip,让这句话执行时间长一些,方便看效果
					} catch (UnknownHostException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					System.out.println("i="+i+"  this.isInterrupted():"+this.isInterrupted());
					System.out.println("i="+i+"  Thread.interrupted():"+Thread.interrupted());
					System.out.println("i="+i+"  this.isInterrupted():"+this.isInterrupted());
					try {
						Thread.sleep(5000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
					//	e.printStackTrace();
						System.out.println("exception:"+this.isInterrupted());
						System.out.println("exception:"+Thread.interrupted());
					//	return;
					}
				}
				System.out.println("end");
			}
		};
		
		t1.start();
		
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(t1.interrupted()+"  "+Thread.interrupted());
		
		
		System.out.println("begin interrupt  t1.isInterrupted():"+t1.isInterrupted());
		t1.interrupt();
		System.out.println(t1.interrupted()+"  "+Thread.interrupted());
		System.out.println("end interrupt  t1.isInterrupted():"+t1.isInterrupted());
		System.out.println(t1.interrupted()+"  "+Thread.interrupted());
		
		
		
		try {
			Thread.sleep(100000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


执行结果:

begin
i=0 false
false  false
begin interrupt  t1.isInterrupted():false
false  false
end interrupt  t1.isInterrupted():true
false  false
java.net.ConnectException: Connection timed out: connect
 at java.net.PlainSocketImpl.socketConnect(Native Method)
 at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
 at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
 at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
 at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
 at java.net.Socket.connect(Socket.java:529)
 at java.net.Socket.connect(Socket.java:478)
 at java.net.Socket.<init>(Socket.java:375)
 at java.net.Socket.<init>(Socket.java:189)
 at baby.thread.InterruptTest$1.run(InterruptTest.java:21)
i=0  this.isInterrupted():true
i=0  Thread.interrupted():true
i=0  this.isInterrupted():false
休息5秒钟
i=1 false
…………

 

例二:注释掉Thread.interrupted()一行

@Test
	public void test13(){
		Thread t1= new Thread(){
			@Override
			public void run() {
				System.out.println("begin");
				for(int i=0;i<100;i++){
					System.out.println("i="+i+" "+this.isInterrupted());
					
					try {
						Socket socket= new Socket("10.22.1.115",23);
					} catch (UnknownHostException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					System.out.println("i="+i+"  this.isInterrupted():"+this.isInterrupted());
				//	System.out.println("i="+i+"  Thread.interrupted():"+Thread.interrupted());
					System.out.println("i="+i+"  this.isInterrupted():"+this.isInterrupted());
					try {
						Thread.sleep(5000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						//	e.printStackTrace();
						System.out.println("exception:"+this.isInterrupted());
						System.out.println("exception:"+Thread.interrupted());
						//	return;
					}
				}
				System.out.println("end");
			}
		};
		
		t1.start();
		
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(t1.interrupted()+"  "+Thread.interrupted());
		
		
		System.out.println("begin interrupt  t1.isInterrupted():"+t1.isInterrupted());
		t1.interrupt();
		System.out.println(t1.interrupted()+"  "+Thread.interrupted());
		System.out.println("end interrupt  t1.isInterrupted():"+t1.isInterrupted());
		System.out.println(t1.interrupted()+"  "+Thread.interrupted());
		
		
		
		try {
			Thread.sleep(100000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


输出:
 begin
i=0 false
false  false
begin interrupt  t1.isInterrupted():false
false  false
end interrupt  t1.isInterrupted():true
false  false
java.net.ConnectException: Connection timed out: connect
 at java.net.PlainSocketImpl.socketConnect(Native Method)
 at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
 at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
 at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
 at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
 at java.net.Socket.connect(Socket.java:529)
 at java.net.Socket.connect(Socket.java:478)
 at java.net.Socket.<init>(Socket.java:375)
 at java.net.Socket.<init>(Socket.java:189)
 at baby.thread.InterruptTest$2.run(InterruptTest.java:82)
i=0  this.isInterrupted():true
i=0  this.isInterrupted():true
没有休息5秒,直接下面输出
exception:false
exception:false
i=1 false

 

例三:自己中断自己

@Test
	public void test12(){
		Thread t1= new Thread(){
			@Override
			public void run() {
				System.out.println("begin");
				for(int i=0;i<100;i++){
					System.out.println("i="+i+" "+this.isInterrupted());
					interrupt();
					try {
						Socket socket= new Socket("10.22.1.115",23);
					} catch (UnknownHostException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					System.out.println("i="+i+"  this.isInterrupted():"+this.isInterrupted());
					System.out.println("i="+i+"  Thread.interrupted():"+Thread.interrupted());
					System.out.println("i="+i+"  this.isInterrupted():"+this.isInterrupted());
					try {
						Thread.sleep(5000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						//	e.printStackTrace();
						System.out.println("exception:"+this.isInterrupted());
						System.out.println("exception:"+Thread.interrupted());
						//	return;
					}
				}
				System.out.println("end");
			}
		};
		
		t1.start();
		
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(t1.interrupted()+"  "+Thread.interrupted());
		
		
		System.out.println("begin interrupt  t1.isInterrupted():"+t1.isInterrupted());
	//	t1.interrupt();
		System.out.println(t1.interrupted()+"  "+Thread.interrupted());
		System.out.println("end interrupt  t1.isInterrupted():"+t1.isInterrupted());
		System.out.println(t1.interrupted()+"  "+Thread.interrupted());
		
		
		
		try {
			Thread.sleep(100000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


结果输出和例一一样:
 begin
i=0 false
false  false
begin interrupt  t1.isInterrupted():true
false  false
end interrupt  t1.isInterrupted():true
false  false
java.net.ConnectException: Connection timed out: connect
 at java.net.PlainSocketImpl.socketConnect(Native Method)
 at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
 at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
 at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
 at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
 at java.net.Socket.connect(Socket.java:529)
 at java.net.Socket.connect(Socket.java:478)
 at java.net.Socket.<init>(Socket.java:375)
 at java.net.Socket.<init>(Socket.java:189)
 at baby.thread.InterruptTest$3.run(InterruptTest.java:143)
i=0  this.isInterrupted():true
i=0  Thread.interrupted():true
i=0  this.isInterrupted():false
休息5秒
i=1 false

 

例四:中断时处于sleep方法

@Test
	public void test14(){
		Thread t1= new Thread(){
			@Override
			public void run() {
				System.out.println("begin");
				for(int i=0;i<100;i++){
					System.out.println("i="+i+" "+this.isInterrupted());
					
					System.out.println("i="+i+"  this.isInterrupted():"+this.isInterrupted());
//					System.out.println("i="+i+"  Thread.interrupted():"+Thread.interrupted());
					System.out.println("i="+i+"  this.isInterrupted():"+this.isInterrupted());
					try {
						Thread.sleep(5000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						//	e.printStackTrace();
						System.out.println("exception:"+this.isInterrupted());
						System.out.println("exception:"+Thread.interrupted());
						//	return;
					}
				}
				System.out.println("end");
			}
		};
		
		t1.start();
		
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	//	System.out.println(t1.interrupted()+"  "+Thread.interrupted());
		
		
		System.out.println("begin interrupt  t1.isInterrupted():"+t1.isInterrupted());
		t1.interrupt();
		//System.out.println(t1.interrupted()+"  "+Thread.interrupted());
		System.out.println("end interrupt  t1.isInterrupted():"+t1.isInterrupted());
	//	System.out.println(t1.interrupted()+"  "+Thread.interrupted());
		
		
		
		try {
			Thread.sleep(100000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

结果输出:
 begin
i=0 false
i=0  this.isInterrupted():false
i=0  this.isInterrupted():false
begin interrupt  t1.isInterrupted():false
没有休息5秒
exception:false
exception:false
i=1 false
i=1  this.isInterrupted():false
i=1  this.isInterrupted():false
end interrupt  t1.isInterrupted():false

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值