用interrupt()终止线程 我想用interrupt()终止一个线程,java文档中说如果线程中有sleep()方法,会抛出一个InterruptedException异常。我的线程中有sleep()方法,我想用catch捕获这个异常,但系统报错说"unreachable catch block for InterruptedException",请大家帮忙。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 把catch(InterruptedException e)这个catch块放到catch块的最上面,也就是说异常捕获的第一个catch为InterruptedException类型。还是解决不了那就贴代码吧。 你把代码贴出来,下面是关于sleep()的代码:/* This class is to implement Runnable interface where method run() is defined and a child thread is created */class NewThread implements Runnable { public void run() { // defining run() try{ for(int i=5;i>0;i--) { // creating child thread System.out.println("Child Thread:" + i); Thread.sleep(1000); // suspending child thread } }catch(InterruptedException e) { System.out.println("Child interrupted."); } System.out.println("Exiting child thread. "); }}class ThreadDemo{ public static void main(String args[]) {/* instantiating an object of type Thread and passing to constructor of class */ Thread thread=new Thread(new NewThread()); thread.start(); // start() executes run() try{ for(int i=5;i>0;i--) { // creating main thread System.out.println("Main Thread: " + i); Thread.sleep(2000); // suspending main thread } }catch(InterruptedException e) { System.out.println("Main thread interrupted."); } System.out.println("Main thread exiting."); }}output:Main Thread: 5Child Thread:5Child Thread:4Child Thread:3Main Thread: 4Child Thread:2Child Thread:1Main Thread: 3Exiting child thread. Main Thread: 2Main Thread: 1Main thread exiting. interrupt() 不能够中止运行中的线程是用来去掉休眠方法的中断标志的 无法catch块的InterruptedException,interrupt() 不能够中止运行中的线程,只能sleep 求一个java绘图程序,高手进!!必有重谢!! 菜鸟问题:就是想问下如果我要在一个类中调用另一个类中的tf.getText()中的内容,该怎么弄呢? 静态代码段起什么作用? 一个简单的正则表达式取值问题 j2ee j2me j2se谁能详细说说? 关于JDK在WIN2K下面的安装问题 一个关于String.split分隔的问题 请教高手:在applet中是否可以控制特定字段或是窗口的显示尺寸,多谢! 如何在Java中用DOM分析HTML页面? 用Java Spring Junit测试,每次运行时都报错,求神相助!! 如何从文本获取指定的字符 两个问题:内存泄露及调用函数
还是解决不了那就贴代码吧。
你把代码贴出来,下面是关于sleep()的代码:/* This class is to implement Runnable interface where method run() is defined and a child thread is created */
class NewThread implements Runnable {
public void run() { // defining run()
try{
for(int i=5;i>0;i--) { // creating child thread
System.out.println("Child Thread:" + i);
Thread.sleep(1000); // suspending child thread
}
}catch(InterruptedException e) {
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread. ");
}
}
class ThreadDemo{
public static void main(String args[]) {
/* instantiating an object of type Thread and passing to constructor of class */
Thread thread=new Thread(new NewThread());
thread.start(); // start() executes run()
try{
for(int i=5;i>0;i--) { // creating main thread
System.out.println("Main Thread: " + i);
Thread.sleep(2000); // suspending main thread
}
}catch(InterruptedException e) {
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}
output:
Main Thread: 5
Child Thread:5
Child Thread:4
Child Thread:3
Main Thread: 4
Child Thread:2
Child Thread:1
Main Thread: 3
Exiting child thread.
Main Thread: 2
Main Thread: 1
Main thread exiting.
是用来去掉休眠方法的中断标志的