java 多线程

sunyimaying1 2010-10-16 09:39:19
首先看段代码:

package com.huaxia;

public class Demo {
public static void main(String[] args) {
Thread t=new Thread(new StopRunnable());
System.out.println("子线程是否存活1: "+t.isAlive());
t.start();
System.out.println("子线程是否存活2: "+t.isAlive());
try {
Thread.sleep(10);
} catch (InterruptedException e) {
System.out.println("xxxxxxxxxxx");
e.printStackTrace();
}
System.out.println("子线程是否存活3: "+t.isAlive());
Thread.currentThread().interrupt();
t.interrupt();
System.out.println("子线程是否存活4: "+t.isAlive());
System.out.println("main thread over.");
}
}

class StopRunnable implements Runnable{
@Override
public void run() {
int i=1;
while(!Thread.currentThread().interrupted()){
System.out.println(i++);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}



运行后的一种结果:
子线程是否存活1: false
子线程是否存活2: true
1
子线程是否存活3: true
子线程是否存活4: true
main thread over.

运行后的另外一种结果:
子线程是否存活1: false
子线程是否存活2: true
1
2
子线程是否存活3: true
子线程是否存活4: true
main thread over.
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)3

at com.huaxia.StopRunnable.run(Demo.java:30)
at java.lang.Thread.run(Thread.java:619)
4
5
6
7
...(线程不停,继续输出数字。)


问题:
首先说说自己的理解.
t.start();后,线程t处于可运行状态。分给它时间片之后,开始运行。运行刚开始休眠10ms。
主线程获得时间片开始运行。主线程开始休眠10ms.
然后线程t开始运行。调用t.interrupt()后,线程t中断。

但是第二种输出结果,我就看不懂了。
什么时候会收到“java.lang.InterruptedException: sleep interrupted”

谁能抽丝剥茧的帮我分析下代码啊?说说这两种运行结果的原因


...全文
187 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lxw2100 2010-10-27
  • 打赏
  • 举报
回复
我也遇到这个问题,出现这个错误:java.lang.InterruptedException: sleep interrupted;
请教这个错误描述是什么意思?什么情况下出现这个错误?
myisfei 2010-10-18
  • 打赏
  • 举报
回复
什么时候会收到“java.lang.InterruptedException: sleep interrupted”不可能出现,你都没有优先级凭什么出现中断.
小9 2010-10-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sunyimaying1 的回复:]
帖子啊,你勿要沉沦堕落。要坚挺
[/Quote]

+1
sunyimaying1 2010-10-16
  • 打赏
  • 举报
回复
帖子啊,你勿要沉沦堕落。要坚挺
小9 2010-10-16
  • 打赏
  • 举报
回复
t.interrupt()中断线程但不是停止线程哦,这个一定要清楚
如果线程在调用 Object 类的 wait()、wait(long) 或 wait(long, int) 方法,或者该类的 join()、join(long)、join(long, int)、sleep(long) 或 sleep(long, int) 方法过程中受阻,则其中断状态将被清除,它还将收到一个 InterruptedException。
简单说interrupt主要是用来打破阻塞状态的
再来这个方法
public static boolean interrupted()
测试当前线程是否已经中断。线程的中断状态 由该方法清除。换句话说,如果连续两次调用该方法,则第二次调用将返回 false(在第一次调用已清除了其中断状态之后,且第二次调用检验完中断状态前,当前线程再次中断的情况除外)。
那么来分析一下你的代码吧:
1、你的子线程的while循环一直都为真,在这个线程内部这样调永远都是真,因为你的意思是当前线程没有中断,就执行循环,现在跑的就是这个线程,怎么可能处于中断状态,所以2-3-4都是true
2、Thread.currentThread().interrupt();没有任何作用,主线程是因为正常执行完毕而寿终正寝的
3、就是异常的产生, t.interrupt();代码发生的时候,t可能没有进入sleep,就不会得到异常;代码发生的时候,t正处于sleep阻塞状态,那么阻塞状态会马上被打破,而且会得到一个异常;因为调度时间有差异,所以你这个代码执行有差异,interrupt方法只调了一次,而子线程是一个循环,究竟会不会出异常,那要看运气了
4、主线程挂了之后,这个子线程没有任何干扰悠哉悠哉地睡一下打印一下……直到永远

最后,楼主没有把interrupt方法搞清楚

凉岑玉 2010-10-16
  • 打赏
  • 举报
回复
子线程休眠的时间内··主线程刚执行了一句话就也要休眠了··然后到底谁能更快的苏醒?应该是子线程··子线程苏醒之后打印i然后又继续睡眠··这时候主线程早就醒了··也继续执行·· 执行到这句话Thread.currentThread().interrupt();···额···等等···这句话啥意思···
sunyimaying1 2010-10-16
  • 打赏
  • 举报
回复
帖子啊,你勿要沉沦堕落。要坚挺

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧