聊聊Java 中的线程中断

网友投稿 209 2022-11-13


聊聊Java 中的线程中断

java如何实现线程中断?

通过调用Thread类的实例方法interrupt。如下:

Thread thread = new Thread(){

@Override

public void run() {

if(isInterrupted()){

System.out.println("interrupt");

}

}

};

thread.start();

thread.interrupt();

线程中断后线程会立即停止执行吗?

NO。 而如果线程未阻塞,或未关心中断状态,则线程会正常执行,不会被打断。

Thread.interrupt()的官方解释是这样的:

If this thread is blocked in an invocation of the

Object#wait() wait(), { Object#wait(long) wait(long)}, or { Object#wait(long, int) wait(long, int)} methods of the { Object} class, or of the { #join(QioiMHeWB)}, { #join(long)}, { #join(long, int)}, { #sleep(long)}, or { #sleep(long, int)}, methods of this class, then its interrupt status will be cleared and it will receive an { InterruptedException}.

也就是:处于阻塞的线程,即在执行Object对象的wait()、wait(long)、wait(long, int),或者线程类的join()、join(long)、join(long, int)、sleep(long)、sleep(long,int)方法后线程的状态,当线程调用interrupt()方法后,这些方法将抛出InterruptedException异常,并清http://空线程的中断状态。

比如下面的例子会中断两次,第一次sleep方法收到中断信号后抛出了InterruptedException,捕获异常后中断状态清空,然后继续执行下一次:

public static void main(String[] args) throws InterruptedException {

Thread thread = new Thread(){

@Override

public void run() {

try {

Thread.sleep(10000);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("interrupt");

try {

Thread.sleep(10000);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

};

thread.start();

thread.interrupt();

Thread.sleep(5000);

thread.interrupt();

}

而下面这个例子则会一直执行,不会被打断:

public static void main(String[] args) throws InterruptedException {

Thread thread = new Thread(){

@Override

public void run() {

while (true)

System.out.println("interrupt");

}

};

thread.start();

thread.interrupt();

}

interrupted与isInterrupted方法啥区别?

Thread类的静态方法interrupted:测试当前线程是否已经中断。如果线程处于中断状态返回true,否则返回false。同时该方法将清除的线程的中断状态。即:如果连续两次调用该方法,则第二次调用将返回false。该方法可用于清除线程中断状态使用。

Thread类的实例方法isInterrupted:测试线程是否已经中断。线程的中断状态不受该方法的影响。

Thread类并没有提供单独清除中断状态的方法,所以有两种方式来达到此目的:

对于sleep等阻塞方法,catch InterruptedException异常;

调用Thread类的静态方法interrupted

线程中断有哪些实际应用?

线程中断的几个实际应用场景:

在处理Web请求时,可能将请求分配到多个线程去处理,实现请求执行的超时机制;

实现线程池时,关闭线程池中的线程任务。

以上就是聊聊Java 中的线程中断的详细内容,更多关于Java 线程中断的资料请关注我们其它相关文章!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:嵌入式软件接口设计(嵌入式软件设计基础)
下一篇:api接口监控平台(监控接入平台)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~