黑马程序员技术交流社区
标题:
关于毕老师多线程中停止线程的疑问
[打印本页]
作者:
﹏゛心被伱牵
时间:
2012-10-22 10:09
标题:
关于毕老师多线程中停止线程的疑问
请问以下代码中t1和t2线程停止后输出的是什么?一直不太明白
class StopThread implements Runnable
{
private boolean flag =true;
public synchronized void run()
{
while(flag)
{
try {
wait();
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName()+"....Exception");
}
System.out.println(Thread.currentThread().getName()+"....run");
}
}
public void changeFlag()
{
flag = false;
}
}
class StopThreadDemo
{
public static void main(String[] args)
{
StopThread st = new StopThread();
Thread t1 = new Thread(st);
Thread t2 = new Thread(st);
t1.start();
t2.start();
int num = 0;
while(true)
{
if(num++ == 100)
{
st.changeFlag();
t1.interrupt();
t2.interrupt();
break;
}
System.out.println(Thread.currentThread().getName()+"......."+num);
}
System.out.println("over");
}
}
作者:
陆建平
时间:
2012-10-22 11:21
输入的内容如下:
其实你自己运行一遍就可以了
main.......1
main.......2
main.......3
main.......4
main.......5
main.......6
main.......7
main.......8
main.......9
main.......10
main.......11
main.......12
main.......13
main.......14
main.......15
main.......16
main.......17
main.......18
main.......19
main.......20
main.......21
main.......22
main.......23
main.......24
main.......25
main.......26
main.......27
main.......28
main.......29
main.......30
main.......31
main.......32
main.......33
main.......34
main.......35
main.......36
main.......37
main.......38
main.......39
main.......40
main.......41
main.......42
main.......43
main.......44
main.......45
main.......46
main.......47
main.......48
main.......49
main.......50
main.......51
main.......52
main.......53
main.......54
main.......55
main.......56
main.......57
main.......58
main.......59
main.......60
main.......61
main.......62
main.......63
main.......64
main.......65
main.......66
main.......67
main.......68
main.......69
main.......70
main.......71
main.......72
main.......73
main.......74
main.......75
main.......76
main.......77
main.......78
main.......79
main.......80
main.......81
main.......82
main.......83
main.......84
main.......85
main.......86
main.......87
main.......88
main.......89
main.......90
main.......91
main.......92
main.......93
main.......94
main.......95
main.......96
main.......97
main.......98
main.......99
main.......100
over
Thread-0....Exception
Thread-0....run
Thread-1....Exception
Thread-1....run
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2