本帖最后由 Jiewin 于 2013-6-4 22:57 编辑
- class StopThread implements Runnable
- {
- private boolean flag = true;
- public void run()
- {
- while(flag)
- {
- 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++ ==60)
- {
- st.changeFlag();
- break;
- }
- System.out.println(Thread.currentThread().getName()+"...."+num);
- }
- }
- }
复制代码 视频:第12天-07-多线程(停止线程)
和老师一摸一样的代码,老师运行的结果如下图1,而我运行的是图2那样的结果!多次运行也是这样,代码有错吗?
图1
图2
还有好多同样运行结果异常的:比如说死锁,老师设置1000,到800左右就锁了,我设置了10000,完全结束了都没锁,运行几次也没锁!!!哎~
|