我想请问关于interrupt()

我想请问关于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()方法就会抛出异常
 请教这是为什么 想不通


hy_wang
浏览 1360回答 3
3回答

用户1122125

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

用户1122125

你直接调用 sleep  是把主线程听了3秒…
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java