当前位置: 移动技术网 > IT编程>开发语言>Java > sleep方法要求处理中断异常:InterruptedException

sleep方法要求处理中断异常:InterruptedException

2019年12月19日  | 移动技术网IT编程  | 我要评论

package seday08.thread;
/**
* @author xingsir
* 当一个线程调用sleep方法处于阻塞状态的过程中,这个线程的中断方法interrupt被调用时,则sleep方法会抛出中断异常
* 此时该线程的睡眠阻塞被打断。
*/
public class sleepdemo2 {

public static void main(string[] args) {
thread wang=new thread() {
public void run() {
system.out.println("呼叫老王中,等待老王接听,嘟嘟嘟。。。。");
try {
thread.sleep(10000);//设置阻塞指定的10000毫秒
} catch (interruptedexception e) {
system.out.println("中断与老王的连线。。。。。");
}
system.out.println("结束");
}
};
thread chen=new thread() {
public void run() {
system.out.println("打小调皮6下");
for(int i=0;i<6;i++) {//循环6次
system.out.println("a~痛");
try {
thread.sleep(1000);//设置阻塞指定的1000毫秒
} catch (interruptedexception e) {
}
}
system.out.println("打完了!");
wang.interrupt();//此时wang还在阻塞中,我们直接将其中断线程的睡眠阻塞
}
};

wang.start();//启动线程要调用start
chen.start();//启动线程要调用start
}

}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网