由于调用了interrupt方法,所以抛出了InterruptedException异常
来源:互联网 发布:淘宝删除邀请我的回答 编辑:程序博客网 时间:2024/05/29 10:01
在java中,开启一个多线程是很简单的,只需要new一个runnable就可以了,但是要停止一个线程,却不能简单的使用Thread.stop()方法。
首先来说说java中的中断机制,Java
中断机制是一种协作机制,也就是说通过中断并不能直接终止另一个线程,而需要被中断的线程自己处理中断。当调用interrupt()方法的时候,只是设
置了要中断线程的中断状态,而此时被中断的线程的可以通过isInterrupted()或者是interrupted()方法判断当前线程的中断状态是
否标志为中断。我们可以从interrupt()方法来看:
public void interrupt() {
if (this != Thread.currentThread())
checkAccess();
synchronized (blockerLock) {
Interruptible b = blocker;
if (b != null) {
interrupt0(); // Just to set the interrupt flag
b.interrupt();
return;
}
}
interrupt0();
}
从这个方法中我们可以看到,最直接的调用时interrupt0()这个方法,而这个方法仅仅是设置了线程中断状态。
我们再看看isInterrupted()方法:
public boolean isInterrupted() {
return isInterrupted(false);
}
/**
* Tests if some Thread has been interrupted. The interrupted state
* is reset or not based on the value of ClearInterrupted that is
* passed.
*/
private native boolean isInterrupted(boolean ClearInterrupted);
从这个方法中,我们可以猜测到,isInterrupted()方法仅仅是检查了当前线程的中断状态,但是不会清除这个状态。
我们再来看看静态方法interrupted()
public static boolean interrupted() {
return currentThread().isInterrupted(true);
}
这个方法同样是检测当前线程的中断状态,但是这个方法会产生一个副作用,就是会清除当前线程的中断状态。
Thread.interrupt() VS Thread.stop()
这两个方法最大的区别在于:interrupt()方法是设置线程的中断状态,让用户自己选择时间地点去结束线程;而stop()方法会在代码的运行处直接抛出一个ThreadDeath错误,这是一个java.lang.Error的子类。所以直接使用stop()方法就有可能造成对象的不一致性。
调用Thread.sleep()方法的时候,如果当前线程处于中断那状态,那么sleep()方法不会执行,同时会清除掉该状态,并且抛出interruptedException异常。
中断的使用demo:
package com.app.basic;
public class InterruptTest {
public static void main(String[] args) {
Runnable runnable1 = new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(i);
if (Thread.currentThread().isInterrupted()) {
System.out.println("aa");
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
//sleep方法抛出这个异常之后会清除中断状态,所以需要重新设置中断状态
Thread.currentThread().interrupt();
}
}
}
};
final Thread t1 = new Thread(runnable1);
Runnable runnable2 = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000);
t1.interrupt();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Thread t2 = new Thread(runnable2);
t1.start();
首先来说说java中的中断机制,Java
中断机制是一种协作机制,也就是说通过中断并不能直接终止另一个线程,而需要被中断的线程自己处理中断。当调用interrupt()方法的时候,只是设
置了要中断线程的中断状态,而此时被中断的线程的可以通过isInterrupted()或者是interrupted()方法判断当前线程的中断状态是
否标志为中断。我们可以从interrupt()方法来看:
public void interrupt() {
if (this != Thread.currentThread())
checkAccess();
synchronized (blockerLock) {
Interruptible b = blocker;
if (b != null) {
interrupt0(); // Just to set the interrupt flag
b.interrupt();
return;
}
}
interrupt0();
}
从这个方法中我们可以看到,最直接的调用时interrupt0()这个方法,而这个方法仅仅是设置了线程中断状态。
我们再看看isInterrupted()方法:
public boolean isInterrupted() {
return isInterrupted(false);
}
/**
* Tests if some Thread has been interrupted. The interrupted state
* is reset or not based on the value of ClearInterrupted that is
* passed.
*/
private native boolean isInterrupted(boolean ClearInterrupted);
从这个方法中,我们可以猜测到,isInterrupted()方法仅仅是检查了当前线程的中断状态,但是不会清除这个状态。
我们再来看看静态方法interrupted()
public static boolean interrupted() {
return currentThread().isInterrupted(true);
}
这个方法同样是检测当前线程的中断状态,但是这个方法会产生一个副作用,就是会清除当前线程的中断状态。
Thread.interrupt() VS Thread.stop()
这两个方法最大的区别在于:interrupt()方法是设置线程的中断状态,让用户自己选择时间地点去结束线程;而stop()方法会在代码的运行处直接抛出一个ThreadDeath错误,这是一个java.lang.Error的子类。所以直接使用stop()方法就有可能造成对象的不一致性。
调用Thread.sleep()方法的时候,如果当前线程处于中断那状态,那么sleep()方法不会执行,同时会清除掉该状态,并且抛出interruptedException异常。
中断的使用demo:
package com.app.basic;
public class InterruptTest {
public static void main(String[] args) {
Runnable runnable1 = new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(i);
if (Thread.currentThread().isInterrupted()) {
System.out.println("aa");
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
//sleep方法抛出这个异常之后会清除中断状态,所以需要重新设置中断状态
Thread.currentThread().interrupt();
}
}
}
};
final Thread t1 = new Thread(runnable1);
Runnable runnable2 = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000);
t1.interrupt();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Thread t2 = new Thread(runnable2);
t1.start();
t2.start();
注:出处为:http://zhidao.baidu.com/link?url=VTnptkq0mExWEBJ4YJJ-M9HJRQcHyBZTvAX9nSuudYHMGHr251rZbPO6t3UpQ56hmY6xcLdTiB8uvo87Dj27Fq
0 0
- 由于调用了interrupt方法,所以抛出了InterruptedException异常
- 监控一个方法的执行时间(不用抛出InterruptedException异常)
- interrupt遇到wait抛异常InterruptedException
- 由于csdn网站太,所以以后开始写笔记了。
- try ,finally都抛出异常如何处理.如果try中抛出了异常,在控制权转移到调用栈上一层代码之前, finally 语句块也会执行,如果finally抛出异常,try语句快抛出的那个异常就
- JNI调用Native方法,抛出UnsatisfiedLinkError异常
- 未抛出的原因是在catch中使用了return语句,导致异常无法抛出insert*方法之外
- Thread.interrupt():中断阻塞(的)线程,(跳出阻塞)使其抛出InterruptedException
- interrupt、interrupted 、isInterrupted、InterruptedException
- Rxjava1.x中在onNext()中抛出了异常,会执行onError()方法
- 捕获异常然后抛出另一个异常发生了什么?
- InterruptedException 异常
- 由于最近看企鹅打架,所以想一个XMPP客户端了.
- 由于不知道哪里可以放源程序,所以只有在这里说说思路了。
- PowerManagerService framework层的分析-------由于sensor涉及这个文件所以转过来了
- 由于被鄙视了,所以研究下sizeof求结构体大小的算法
- 所以还是来了
- 回复[请问一些异常抛出的问题,谢谢了]
- c# 中button加图片
- 手把手教你如何设计打地鼠游戏(技术教程)
- 最小生成树-Kruskal算法
- hadoop程序开发之——单机配置
- 微课播放器
- 由于调用了interrupt方法,所以抛出了InterruptedException异常
- ZJOJ Highway Project 3946(单源最短路+贪心)
- uva 562 Dividing coins
- 数据结构算法3_2 迷宫求解
- Java String 常量池问题的几个例子 ,与堆,与栈
- 串
- http-关于application/x-www-form-urlencoded等字符编码的解释说明
- java之多线程
- StringIO和BytesIO