java的interruput

java中主要有3个方法,interrupt(),isInterrupted()和interrupted()。

  • interrupt(),在一个线程中调用另一个线程的interrupt()方法,即会向那个线程发出信号——线程中断状态已被设置。至于那个线程何去何从,由具体的代码实现决定。
  • isInterrupted(),用来判断当前线程的中断状态(true or false)

isInterrupted的实现

/*
    Tests whether this thread has been interrupted. The interrupted status of the thread is unaffected by this method.
    A thread interruption ignored because a thread was not alive at the time of the interrupt will be reflected by this method returning false.
    Returns:
    true if this thread has been interrupted; false otherwise.
*/
public boolean isInterrupted() {
    return isInterrupted(false);
}

 

  • interrupted()是个Thread的static方法,用来清除中断状态。

interrupted方法的实现:

/*
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). 

    A thread interruption ignored because a thread was not alive at the time of the interrupt will be reflected by this method returning false.

    Returns:
    true if the current thread has been interrupted; false otherwise.
*/
public static boolean interrupted() {
    return currentThread().isInterrupted(true);
}
  • interrupted和isInterrupted两个方法一个是static的,一个不是,但实际上都是在调用同一个方法,只是interrupted方法传入的参数为true,而inInterrupted传入的参数为false。那么这个参数到底是什么意思呢?来看下这个isInterrupted(boolean)方法的实现:
/**
 * Tests if some Thread has been interrupted.  The interrupted state
 * is reset or not based on the value of ClearInterrupted that is
 * passed.
 */
private native boolean isInterrupted(boolean ClearInterrupted);

参数名字ClearInterrupted已经清楚的表达了该参数的作用----是否清除中断状态。方法的注释也清晰的表达了“中断状态将会根据传入的ClearInterrupted参数值确定是否重置”。所以,静态方法interrupted将会清除中断状态(传入的参数ClearInterrupted为true),而实例方法isInterrupted则不会(传入的参数ClearInterrupted为false)。

实例代码

package linruby.builder;

public class Interrupt  {
	public static void main(String[] args) throws Exception {
		Thread t = new Thread(new Worker());
		t.start();
		
		Thread.sleep(200);
		t.interrupt();
		
		System.out.println("Main thread stopped.");
	}

	public static class Worker implements Runnable {
		public void run() {
			System.out.println("Worker started.");
			while(!Thread.currentThread().isInterrupted()) {
				try {
					System.out.println("---------xx---------");
					Thread.sleep(500);
					System.out.println("---------ww---------");
				} catch (InterruptedException e) {
					Thread curr = Thread.currentThread();
					//当前线程被中断,但是状态未被设置
					System.out.println("Worker IsInterrupted: " + curr.isInterrupted());
					System.out.println("Worker IsInterrupted: " + curr.isInterrupted());
					System.out.println("---------执行自身线程中断----------");
					//调用interrupt方法中断自己,将中断状态设置为“中断”
					curr.interrupt();
					System.out.println("Worker IsInterrupted: " + curr.isInterrupted());
					System.out.println("Worker IsInterrupted: " + curr.isInterrupted());
					System.out.println("Static Call: " + Thread.interrupted());//clear status
					System.out.println("---------当前线程中断状态清理后----------");
					System.out.println("Static Call: " + Thread.interrupted());
					System.out.println("Worker IsInterrupted: " + curr.isInterrupted());
					System.out.println("Worker IsInterrupted: " + curr.isInterrupted());
				}
			}
			
			System.out.println("Worker stopped.");
		}
	}
}

执行结果

Worker started.
---------xx---------
Main thread stopped.
Worker IsInterrupted: false
Worker IsInterrupted: false
---------执行自身线程中断----------
Worker IsInterrupted: true
Worker IsInterrupted: true
Static Call: true
---------当前线程中断状态清理后----------
Static Call: false
Worker IsInterrupted: false
Worker IsInterrupted: false
---------xx---------

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值