java线程中的interrupt,isInterrupt,interrupted方法
来源:互联网 发布:2016京东和淘宝哪个好 编辑:程序博客网 时间:2024/04/30 07:33
(1)interrupt:置线程的中断状态
(2)isInterrupt:线程是否中断
(3)interrupted:返回线程的上次的中断状态,并清除中断状态
interrupt()只是改变中断状态而已. in
这样受阻线程就得以退出阻塞的状态
如果线程没有被阻塞,这时调用interrupt()将不起作用;否则,线程就将得到InterruptedException异常(该线程必须事先预备好处理此状况),接着逃离阻塞状态。
线程A在执行sleep,wait,join时,线程B调用线程A的interrupt方法,的确这一个时候A会有InterruptedException 异常抛出来.
但这其实是在sleep,wait,join这些方法内部会不断检查中断状态的值,而自己抛出的InterruptedException。
如果线程A正在执行一些指定的操作时如赋值,for,while,if,调用方法等,都不会去检查中断状态
,所以线程A不会抛出 InterruptedException,而会一直执行着自己的操作.
当线程A终于执行到wait(),sleep(),join()时,才马上会抛出 InterruptedException.
若没有调用sleep(),wait(),join()这些方法,即没有在线程里自己检查中断状态自己抛出InterruptedException的 话,
那InterruptedException是不会被抛出来的.
具体使用见实例1,实例2。
注意1:当线程A执行到wait(),sleep(),join()时,抛出InterruptedException后,
线程A调用Thread.interrupted()返回的是false
public static boolean interrupted ()
Returns a boolean
indicating whether the current Thread ( currentThread()
) has a pending interrupt request ( true
) or not (false
). It also has the side-effect of clearing the flag.
Returns
- a
boolean
indicating the interrupt status
public boolean isInterrupted ()
Returns a boolean
indicating whether the receiver has a pending interrupt request (true
) or not ( false
)
Returns
- a
boolean
indicating the interrupt status
线程A正在使用sleep()暂停着: Thread.sleep(100000);
如果要取消他的等待状态,可以在正在执行的线程里(比如这里是B)调用
a.interrupt();
令线程A放弃睡眠操作,这里a是线程A对应到的Thread实例
当在sleep中时 线程被调用interrupt()时,就马上会放弃暂停的状态.并抛出InterruptedException.丢出异常的,是A线程.
线程A调用了wait()进入了等待状态,也可以用interrupt()取消.
不过这时候要小心锁定的问题.线程在进入等待区,
,会
当线程以join()等待其他线程结束时,当它被调用interrupt(),它与sleep()时一样, 会马上跳到catch块里.
注意,是对谁调用interrupt()方法,一定是调用被阻塞线程的interrupt方法.如在线程a中调用来线程t.join().
则a会等t执行完后在执行t.join后的代码,当在线程b中调用来 a.interrupt()方法,则会抛出InterruptedException,当然join()也就被取消了。
static void interruptDemo()
{
Thread t1=new Thread(new InterruptDemoRunnable(),"first");
Thread t2=new Thread(new InterruptDemoRunnable(t1),"second");
t1.start();
t2.start();
}
class InterruptDemoRunnable implements Runnable
{
long id=0;
static long count=0;
Thread t;
InterruptDemoRunnable()
{
this(null);
}
InterruptDemoRunnable(Thread t)
{
id=count++;
this.t=t;
}
boolean blRun=true;
public void run()
{ int cnt=0;
while(blRun)
{
if(t!=null)
{
if(cnt==2)
{
t.interrupt();
if(t.isInterrupted())
System.out.println("set the thread "+t.getName()+" to be Interrupted");
}
}
else
{
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println("First Interrupted when sleep");
System.out.println( "Interrupted is "+Thread.interrupted());//@1
}
try
{
Thread.sleep(1);
}
catch(InterruptedException e)
{
System.out.println("second Interrupted when sleep");
}
}
System.out.println(id+"run"+cnt);
cnt++;
if(cnt==5)
{
blRun=false;
}
}
System.out.println("thread:"+id+"exit");
}
}
线程AThread.interrupted()返回的是false
static void interruptDemo()
{
Thread t1=new InterruptDemoThread();
t1.setName("t1");
Thread t2=new InterruptDemoThread(t1);
t1.setName("t2");
t1.start();
t2.start();
}
class InterruptDemoThread extends Thread
{
long id=0;
static long count=0;
Thread t;
InterruptDemoThread()
{
this(null);
}
InterruptDemoThread(Thread t)
{
id=count++;
this.t=t;
}
boolean blRun=true;
public void run()
{ int cnt=0;
while(blRun)
{
if(t!=null)
{
if(cnt==2)
{
t.interrupt();
if(t.isInterrupted())
System.out.println("set the thread "+t.getName()+" to be Interrupted");
}
}
else if(isInterrupted())@1
{
/*Thread.interrupted();//@2
System.out.println("after interrupted(),the state of Interrupted is "+isInterrupted());*/
try{
Thread.sleep(1);
}
catch(InterruptedException e)
{
System.out.println("Interrupted when sleep");
System.out.println( "Interrupted is "+Thread.interrupted());
}
}
Thread.yield();
System.out.println(id+"run"+cnt);
cnt++;
if(cnt==5)
{
blRun=false;
}
}
System.out.println("thread:"+id+"exit");
}
}
下次执行wait(),sleep(),join()时,一样会抛出InterruptedException,当然抛出后该线程的中断状态也回被系统复位。
- java线程中的interrupt,isInterrupt,interrupted方法
- java线程中的interrupt,isInterrupt,interrupted方法
- java线程中的interrupt,isInterrupt,interrupted方法
- java线程中的interrupt,isInterrupt,interrupted方法
- java线程中的interrupt,isInterrupt,interrupted方法
- java线程中的interrupt,isInterrupt,interrupted方法
- java线程中的interrupt,isInterrupt,interrupted方法
- java线程中的interrupt、interrupted、isInterrupt方法
- java线程中的interrupt,isInterrupt,interrupted方法
- java线程中的interrupt,isInterrupt,interrupted方法以及如何终止线程(一)
- java线程中的interrupt,isInterrupt,interrupted方法以及如何终止线程(二)
- Java thread的Interrupt, isInterrupt, interrupted
- 关于Java thread的interrupt, isInterrupt, interrupted
- 关于Java thread的interrupt, isInterrupt, interrupted
- interrupt、isinterrupt、interrupted的理解和使用
- Java中的线程中断:interrupt()、interrupted()和isInterrupted
- Java interrupted()方法和isInterrupt方法的区别
- 区分线程中的 interrupt() interrupted() isInterrupted()
- PAT 1057. Stack
- LeetCode-Binary Tree Maximum Path Sum
- jQuery回到顶部插件jQueryGoUp
- 修正Android摄像头API
- 获取当前时间
- java线程中的interrupt,isInterrupt,interrupted方法
- 编写脚本程序用于监测系统服务http的运行状态
- Qt Model/View 学习笔记 (七)
- JQUERY 选择器
- SQL SERVER 批量导入时候的优化
- 派生类的初始化列表
- STM32相关(基于libopencm3库)
- 一步步教你为网站开发Android客户端
- 控件笔记之鸡啄米