interrupt(), isInterrupted(), interrupted() 方法的区别

Let's start with isInterrupted(). This method returns true if the internal interrupted flag is set for the thread, or false otherwise.
When a Thread is first created, it has never been interrupted, so calling isInterrupted() will return false. If the Thread goes along its merry way and is never interrupted, then calling isInterrupted() will never return true. Ok, this was the easy part.
When you want to interrupt a thread, you need to have a reference to it, ie, a variable that holds a reference to the thread you want to interrupt. Say I have a variable myThread of type Thread, that contains a reference to the thread I want to interrupt. I can now interrupt that thread by calling
myThread.interrupt();
What does this do to the called thread? Like I mentioned above, this sets the internal interrupted flag for the thread. Now if I have code in the myThread Thread that calls isInterrupted(), it will return true.
Now, it starts getting a little more tricky. If myThread is running a loop and not explicitly calling sleep() or wait(), then it must check for the interrupted status by polling the isInterrupted() method continuously. For example:
view plain print ?
  1. while(true) { //endless loop  
  2.   //do a lot of stuff  
  3.   if (isInterrupted()){  
  4.       //deal with the interrupt in any way we want  
  5.   }  
  6.   //do more stuff  
  7. }  


If we don't check the isInterrupted() result, then we will never know if we were interrupted or not. Also, note the interrupt mechanism is a tool for you to use to design your software-what you actually do after being interrupted is up to your algorithm and specific to your program. It's just a way for one Thread to tap another Thread on the shoulder and say "excuse me Mr. Thread..."; after that it's up to you do something with that interruption.
Now, the other method, interrupted() is similar to isInterrupted() in that it returns the true/false status of the interrupted flag, EXCEPT it also clears the flag, so if you call interrupted() and it returns true, the very next time you call interrupted() it will return false, untill your thread is again interrupted. This method is useful when you are going to be interrupted a lot and you want to make sure you only handle each interruption one time, with a specific method for example.
Finally, there are 2 methods that are special when dealing with interrupts, namely wait() and sleep(). If a Thread is blocked, either because it is sleeping, or waiting, then it's not actively running, and so it can't actively respond to an interrupt from another thread. So the designers of java provided a mechanism whereby if you interrupt a Thread that is asleep, or waiting for a lock, it can be "woken up" to handle the interruption. That's why you must wrap calls to sleep() or wait() in a try/catch block and catch the InterruptedException.
Hope this helps.

 

转自:http://www.coderanch.com/t/237332/java-programmer-SCJP/certification/plz-explain-interrupt-isInterrupted-interrupted

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值