为了账号安全,请及时绑定邮箱和手机立即绑定

我想请问关于interrupt()

我想请问关于interrupt()

hy_wang 2017-08-15 20:00:33
我想请问关于interrupt()这个方法调用什么时候才可以抛出异常?老师讲解是在线程被阻塞的时候调用会抛出异常。package one; public class newnew extends Thread {  public void run(){                   System.out.println("线程正在运行");         long time = System.currentTimeMillis();         while((System.currentTimeMillis()-time)<1000){}     }           public static void main(String[] args) {                newnew wwst = new newnew();         System.out.println("启动线程 ");         wwst.start();                   try {             sleep(3000);         } catch (InterruptedException e) {             e.printStackTrace();         }                   System.out.println("中断线程");         wwst.interrupt();                   try {             sleep(3000);         } catch (InterruptedException e) {             e.printStackTrace();         }         System.out.println("线程结束了");           } } 可是请问关于这段代码 我在调用wwst.interrupt();的时候上边明明有  sleep(3000);  运行的时候却没有抛出异常  而如果我 在run()方法中 假如sleep()方法就会抛出异常  请教这是为什么 想不通
查看完整描述

3 回答

?
用户1122125

TA贡献2条经验 获得超0个赞

你在 run 里执行 sleep 是停的run 方法执行的那个线程

查看完整回答
反对 回复 2017-08-16
?
用户1122125

TA贡献2条经验 获得超0个赞

你直接调用 sleep  是把主线程听了3秒…

查看完整回答
反对 回复 2017-08-16
  • 3 回答
  • 0 关注
  • 1359 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信