线程的简单问题
public class Test extends Thread {
public void run() {
try {
sleep(3000);
} catch (InterruptedException e) {
System.out.println("中断");
}
System.out.println("继续执行");
}
public static void main(String[] args) {
Test t = new Test();
Thread th = new Thread(t);
th.start();
th.interrupt();
for (int i = 0; i < 1000; i++)
;
System.out.println("中止");
}
}
为什么要我调用了interrupt()这个方法后,
System.out.println("继续执行");这行代码还会打印出来呢?
是因为它我调用interrupt()后出现异常,所以异常后面的代码会执行吗?
问题点数:5、回复次数:20Top
1 楼treeroot(旗鲁特)回复于 2005-11-21 14:16:54 得分 0
为什么不出来?catch中你又没有return!Top
2 楼quincy1984(星禾)回复于 2005-11-21 14:41:12 得分 0
我明白楼主为什么以为:System.out.println("继续执行");会不执行了
只是try{}语句里面如果产生了异常,try{}语句体内剩下的语句才不会被执行,
上面的System.out.println("继续执行");是在try...catch语句后面,当然是会执行的啦Top
3 楼quincy1984(星禾)回复于 2005-11-21 14:42:23 得分 0
如果是这样:
public class TestThreadInter extends Thread
{
public void run()
{
try
{
sleep(3000);
System.out.println("继续执行");
}
catch (InterruptedException e)
{
System.out.println(e.getMessage());
}
}
public static void main(String[] args)
{
TestThreadInter t=new TestThreadInter();
Thread th=new Thread(t);
th.start();
th.interrupt();//中断该线程
for (int i=0;i<1000;i++);
System.out.println("中止");
}
}
这个System.out.println("继续执行");就不会被执行了Top
4 楼skycncomp(闭关修练到年底)回复于 2005-11-21 15:20:40 得分 0
我知道catch后边的代码是会执行的
而我想明白的是我已经中断线程不让它在执行了,
难到中断的意思只是告诉它你可能不充许执行了?
但线程还是把本次执行完毕吧Top
5 楼quincy1984(星禾)回复于 2005-11-21 15:41:30 得分 0
好象是哦,好象那个线程根本就没有被中断了一样的??`Top
6 楼skycncomp(闭关修练到年底)回复于 2005-11-22 08:56:06 得分 0
自己顶一下Top
7 楼skycncomp(闭关修练到年底)回复于 2005-11-22 12:30:09 得分 0
是不是出现异常后,异常处理的优化级高手我设置的那个线程,
所以后边的代码会执行呢?
gs给解释一下Top
8 楼aaa2003gf(珍惜 (MSN:aaa2003gf@hotmail.com))回复于 2005-11-22 12:41:10 得分 0
学习Top
9 楼skycncomp(闭关修练到年底)回复于 2005-11-22 13:27:51 得分 0
再顶一下Top
10 楼shine333(enihs)回复于 2005-11-22 13:35:53 得分 1
try - catch
有点像一个人做了坏事被关了起来(catch),但是一旦刑满释放,他的正常权利照样不能剥夺啊Top
11 楼skycncomp(闭关修练到年底)回复于 2005-11-22 13:40:11 得分 0
那在这个程序里,我interrupt还有什么作用呢?
难到我中断这个线程,
try catch不是通过我这个线程来执行的,
还有其它的线程来控制try catch语句吗?Top
12 楼treeroot(旗鲁特)回复于 2005-11-22 13:43:08 得分 4
事实上interrupt是不能终止一个线程,目前没有很好的办法直接终止一个线程.
当然stop方法是可以的,但是这个方法容易造成死锁,已经强烈建议不要使用了.
interrupt方式只是置一个标志位而已,当然如果线程本身在wait或者sleep就会抛出一个异常.
Top
13 楼skycncomp(闭关修练到年底)回复于 2005-11-22 13:57:22 得分 0
interrupt方式只是置一个标志位而已,当然如果线程本身在wait或者sleep就会抛出一个异常.
设置这个标志有意义是什么呢?
如果我没有sleep()这个方法,而在run()方法里面有很长的一段运行时间,
public void run() {
while (flag) {}
}
我在主程序里调用interrupt方面,对run有什么作用吗?设置这个标记能告诉jvm什么时候中止线程还是一直在等到我的flag为false时才退出呢?Top
14 楼nighthawk(我们孤单,我们并肩)回复于 2005-11-22 15:12:31 得分 0
If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.Top
15 楼zeq258(周二强)回复于 2005-11-22 15:47:16 得分 0
不太懂,但是我觉得这个很有道理:
-----------------------------------------------
回复人: treeroot(旗鲁特) ( ) 信誉:106 2005-11-22 13:43:00 得分: 0
事实上interrupt是不能终止一个线程,目前没有很好的办法直接终止一个线程.
当然stop方法是可以的,但是这个方法容易造成死锁,已经强烈建议不要使用了.
interrupt方式只是置一个标志位而已,当然如果线程本身在wait或者sleep就会抛出一个异常.
Top
16 楼skycncomp(闭关修练到年底)回复于 2005-11-22 15:57:01 得分 0
我觉得楼上你没有明白我的意思,
可以看一下前边我的回复
Top
17 楼skycncomp(闭关修练到年底)回复于 2005-11-22 16:03:09 得分 0
我是说 nighthawk(我们孤单,我们并肩)
Top
18 楼skycncomp(闭关修练到年底)回复于 2005-11-22 17:28:56 得分 0
顶一下Top
19 楼funcreal(为中华之崛起而编程)回复于 2005-11-23 07:58:31 得分 0
try{
while(!interrupted()&&has more work to do){
do more work...
}
}catch(InterruptedException e){
//interrupted while sleep
}
一个线程应该不时地检查自己是否应该退出,如果被别人强制终止,则会带来意想不到的灾难。
可以用interrupted()来检查,别人调用了interrupt(),则他的interrupted()就返回trueTop
20 楼skycncomp(闭关修练到年底)回复于 2005-11-23 11:01:27 得分 0
回复人: treeroot(旗鲁特) ( ) 信誉:106 2005-11-22 13:43:00 得分: 0
事实上interrupt是不能终止一个线程,目前没有很好的办法直接终止一个线程.
当然stop方法是可以的,但是这个方法容易造成死锁,已经强烈建议不要使用了.
interrupt方式只是置一个标志位而已,当然如果线程本身在wait或者sleep就会抛出一个异常.
treeroot(旗鲁特)说的对,interrupt是不能真正终止一个线程的
nighthawk(我们孤单,我们并肩) ( ) 信誉:76 2005-11-22 15:12:00 得分: 0
If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.
nighthawk(我们孤单,我们并肩) 这段JDK描述同样,当调用sleep()方法后,如果调用interrupt之后会抛出异常
昨天我资料说,当sleep()抛出异常后,interrupt设置的终止线程标致将同样清除?
我觉得可能就是这个原因导致后面还可以执行。
不知道对不对,请理解的人给解释一下。
谢Top




