public class ThreadTest08 {
public static void main(String[] args) {
Runnable r1 = new Processor();
Thread t1 = new Thread(r1, "t1");
t1.start();
try {
//设置为500毫秒,没有出现中断异常,因为
//500毫秒之后再次调用t1.interrupt()时,
//此时的睡眠线程已经执行完成
//如果sleep的时间设置的小一些,会出现中断异常,
//因为存在睡眠线程
Thread.sleep(500);
}catch(Exception e) {
e.printStackTrace();
}
//中断睡眠中的线程
t1.interrupt();
}
}
class Processor implements Runnable {
public void run() {
for (int i=1; i<100; i++) {
System.out.println(Thread.currentThread().getName() + "," + i);
if (i % 50 == 0) {
try {
Thread.sleep(200);
}catch(Exception e) {
System.out.println("-------中断-------");
break;
}
}
}
}
}
我是刚学java的想问一下我运行出来就是t1.1----99,为什么没有中断的字样,还有在窗口好像看不出睡眠和中断的过程
public class ThreadTest08 { public static void main(String[] args) { Runnable r1 = new Processor
答案:2 悬赏:30
解决时间 2021-04-09 05:58
- 提问者网友:浪子生来ˇ性放荡²↘
- 2021-04-08 13:02
最佳答案
- 二级知识专家网友:丢不掉的轻狂
- 2021-04-08 13:47
兄弟,我执行了一下,显示结果如下:
t1,1
t1,2
t1,3
t1,4
t1,5
t1,6
t1,7
t1,8
t1,9
t1,10
t1,11
t1,12
t1,13
t1,14
t1,15
t1,16
t1,17
t1,18
t1,19
t1,20
t1,21
t1,22
t1,23
t1,24
t1,25
t1,26
t1,27
t1,28
t1,29
t1,30
t1,31
t1,32
t1,33
t1,34
t1,35
t1,36
t1,37
t1,38
t1,39
t1,40
t1,41
t1,42
t1,43
t1,44
t1,45
t1,46
t1,47
t1,48
t1,49
t1,50
-------中断-------
t1,1
t1,2
t1,3
t1,4
t1,5
t1,6
t1,7
t1,8
t1,9
t1,10
t1,11
t1,12
t1,13
t1,14
t1,15
t1,16
t1,17
t1,18
t1,19
t1,20
t1,21
t1,22
t1,23
t1,24
t1,25
t1,26
t1,27
t1,28
t1,29
t1,30
t1,31
t1,32
t1,33
t1,34
t1,35
t1,36
t1,37
t1,38
t1,39
t1,40
t1,41
t1,42
t1,43
t1,44
t1,45
t1,46
t1,47
t1,48
t1,49
t1,50
-------中断-------
全部回答
- 1楼网友:丢不掉的轻狂
- 2021-04-08 15:26
你说呢...
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯