interrupt

1) Each thread has a boolean interrupted status.

 

2) 3 interrupt related methods defined in Thread class

 

public class Thread

{

    public void interrupt() {...} // interrupts the target thread

 

    public boolean isInterrupted() {...} //return the interrupted status of the target thread.

 

     //clear the interrupted status of the current thread, and returns its previous value.

     //This is the 1st way to clear the interrupted status

    public static boolean interrupted() {...}

}

 

3) Another way the clear the interrupted status is:

When the thread is in blocked status, if you call interrupt() on it, the thread will automatically go to Catch(InterruptedException e){...} block. As soon as the exception is thrown out, the interrupted status will be cleared.

 

4) interrupt() method acutally does just one thing --- set interrupted status to true. Nothing else. From this point of view, it only makes sense when the thread is monitoring the interrupted status.

The thread will monitor the interrupted status only under 1 situation --- the thread is in blocked status which is wrapped with catch(InterruptedException e){}

1) thread.sleeping()

2) object.waiting()

3) BlockedQueue operations

4) Lock.lockInteruptibly()

 

What I want to say is, very likely, interrupt() method doesn't work for some thread, it means those threads are not interruptable.

 

So, just one sentence to summary,

 

If the current line of code is monitored by InterruptedException, then, if you interrupt it at the time or before that, it will work. Otherwise, just everthing in vain---setting interrupted status to true means nothing!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值