Java中的线程中断方法怎么用


本篇内容介绍了“Java中的线程中断方法怎么用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!Java多线程编程中的interrupt()方法、isInterrupted()方法和interrupted()方法都是跟线程中断相关的方法,都非常重要。这三个方法名称非常相似,不理解原理时容易混淆,这里分别介绍下,以加以区分。由于interrupt()方法和isInterrupted()方法都是实例方法(非类上的静态方法),因此我在前面加了个thread1,表示一个实例化的具体线程:thread1.interrupt()方法用来中断线程,所谓的中断,大家可以通俗的理解为打断。比如有两个线程ab,当线程a因为某些原因想打断线程b时,a线程内部可以调用b.interrupt()。不过要注意,实现上是通过设置线程b中断状态标记实现的。b线程代码运行期间,可以在一个循环体内不断的判断该中断状态标记,以确认是否真正响应a的中断请求(比如退出执行等等)。thread1.isInterrupted()方法用来获取一个线程的中断状态。比如有两个线程ab,当线程a因为某些原因想打断线程b时,可以通过b.interrupt()b进行中断。在线程b内部,可以判断自己的中断状态,是否是被中断的,然后根据中断状态确认是否响应中断请求(比如退出当前线程的循环体等等)。thread1.isInterrupted()方法内部直接调用了native方法,传入的ClearInterrupted参数是false,表示不清空中断状态标记:

publicbooleanisInterrupted(){
returnisInterrupted(false);
}//ClearInterrupted表示是否清楚中断状态标记privatenativebooleanisInterrupted(booleanClearInterrupted);

因此调用完该方法后,中断标志位不清除。Thread.interrupted()是定义在Thread类上的静态方法,用来判断当前线程的中断状态,跟thread1.isInterrupted()不同的是,该方法返回中断状态之后,会复位(reset)中断状态标记,所谓的复位即恢复默认状态,也可以说是清空中断状态标记。看Thread类源码可以看到Thread.interrupted()方法实现非常简单,内部直接调用了native方法,只不过ClearInterrupted参数传的是true,表示清空中断状态标记:

publicstaticbooleaninterrupted(){
returncurrentThread().isInterrupted(true);
}//ClearInterrupted表示是否清楚中断状态标记privatenativebooleanisInterrupted(booleanClearInterrupted);

可以看出thread1.isInterrupted()Thread.interrupted()的区别其实就在于获取完中断状态标记之后,是否复位。大家可以根据需要进行选择使用。要想优雅的停止某个线程的运行,需要前面介绍的中断机制。比如两个线程ab,当线程a想中断线程b时,可以通过调用b.interrupt()方法,来设置线程b的中断标记,以达到通知线程b的目的。而线程b需要不断的检查自己的中断标记,以随时响应其他线程的中断,比如下面的实现所示:

publicclassTestMain{
publicstaticvoidmain(String[]args)throwsInterruptedException{
Threadb=newThread(newRunnable(){
@Override
publicvoidrun(){
intnum=0;
while(true){
if(Thread.interrupted()){
break;
}
System.out.println("threadbrunning,numis:"+num++);
}
}
});
b.start();
//主线程sleep1ms,让线程b循环一小会
Thread.sleep(1);
//中断线程b
b.interrupt();
}
}

这里在主线程内新创建了一个线程b,线程b内部是一个循环体,每次循环开始都检查一下中断状态,确认是否被中断,如果被中断了即退出循环,结束线程的执行。主线程sleep了1ms,让线程b先循环几次。接着主线程通过b.interrupt()对线程b进行中断。运行程序可以看到输出如下(不同机器运行结果不一样):

threadbrunning,numis:0threadbrunning,numis:1threadbrunning,numis:2...
threadbrunning,numis:25threadbrunning,numis:26threadbrunning,numis:27Processfinishedwithexitcode0免费云主机域名

可以看到主线程成功的中断了线程b。当然,主线程之所以能成功的中断线程b,是因为线程b一直在检查自己的中断状态(如果线程b太自我,不考虑其他线程,只考虑自己运行,那主线程就无法成功打断线程b了)。前面我们成功的在主线程中中断了线程b,然后如果线程b中存在阻塞,比如下面的代码所示,线程b在sleep时被主线程中断:

publicclassTestMain{
publicstaticvoidmain(String[]args)throwsInterruptedException{
Threadb=newThread(newRunnable(){
@Override
publicvoidrun(){
intnum=0;
while(true){
if(Thread.interrupted()){
break;
}
try{
Thread.sleep(1000);
}catch(InterruptedExceptione){
//Thread.currentThread().interrupt();
e.printStackTrace();
}
System.out.println("threadbrunning,numis:"+num++);
}
}
});
b.start();
//主线程sleep5.5秒,让线程b循环5次
Thread.sleep(5500);
//中断线程b
b.interrupt();
}
}

这时线程b会抛出InterruptedException异常,上面的代码中我们仅仅打印了下该异常,相当于什么都没做。运行该代码结果如下:

threadbrunning,numis:0threadbrunning,numis:1threadbrunning,numis:2threadbrunning,numis:3threadbrunning,numis:4java.lang.InterruptedException:sleepinterrupted
atjava.lang.Thread.sleep(NativeMethod)
attest.TestMain$1.run(TestMain.java:25)
atjava.lang.Thread.run(Thread.java:748)
threadbrunning,numis:5threadbrunning,numis:6threadbrunning,numis:7threadbrunning,numis:8threadbrunning,numis:9...

可以看出,主线程未能成功中断线程b。线程内调用waitjoinsleep时都会进入阻塞状态。当线程处于阻塞状态时被中断,这时线程就会抛出InterruptedException异常,其实大家可以通俗的理解为一种通知即可。以sleep方法为例,大家可以按如下模拟实现来理解(底层是native实现):

publicstaticvoidsleep(longmillis)throwsInterruptedException
{
while(/*stillwaitingformillistobecomezero*/)
{
if(Thread.interrupted())
{
thrownewInterruptedException();
}
//Keepwaiting
}
}

有了InterruptedException异常通知,线程就可以在阻塞时立即知道被中断了,进而采取一定的措施响应中断。需要注意的一点是,由于抛出了InterruptedException异常,因此不会在设置中断标志位。理解了InterruptedException异常,我们就可以在线程即使发生阻塞时也能成功进行中断了,如下所示:

publicclassTestMain{
publicstaticvoidmain(String[]args)throwsInterruptedException{
Threadb=newThread(newRunnable(){
@Override
publicvoidrun(){
intnum=0;
while(true){
if(Thread.interrupted()){
break;
}
try{
Thread.sleep(1000);//用sleep来模拟线程的执行
}catch(InterruptedExceptione){
Thread.currentThread().interrupt();//注意这里是重点!
}
System.out.println("threadbrunning,numis:"+num++);
}
}
});
b.start();
//主线程sleep5.5秒,让线程b先循环5次
Thread.sleep(5500);
//中断线程b
b.interrupt();
}
}

这里我们在检查到InterruptedException异常时,重新设置了中断标志位,这样下次循环一开始时,即可判断被中断了,进而退出循环体。当然我们可以在InterruptedException异常catch时直接退出。“Java中的线程中断方法怎么用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注百云主机网站,小编将为大家输出更多高质量的实用文章!

相关推荐: JS中this指的是什么

这篇文章主要介绍“JS中this指的是什么”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“JS中this指的是什么”文章能帮助大家解决问题。关于“JS中this指的是什么”的内容就介绍到这免费云主机域名里了,感谢大家的阅…

免责声明:本站发布的图片视频文字,以转载和分享为主,文章观点不代表本站立场,本站不承担相关法律责任;如果涉及侵权请联系邮箱:360163164@qq.com举报,并提供相关证据,经查实将立刻删除涉嫌侵权内容。

(0)
打赏 微信扫一扫 微信扫一扫
上一篇 02/19 23:51
下一篇 02/20 18:25

相关推荐