本帖最后由 张迁 于 2013-5-12 20:32 编辑
主线程运行已经将flag改为false,其他线程应该就不运行了,为什么输出结果还仍然有运行呢?- class Demo1 {
- public static void main(String[] args) {
- Stop st = new Stop();
- Thread t1 = new Thread(st);
- Thread t2 = new Thread(st);
- t1.start();
- t2.start();
- int num = 1;
- while (true){
- if (++num == 50){
- st.set();
- break;
- }
- System.out.println(Thread.currentThread().getName()+"------"+num);
- }
- System.out.println("over");
- }
- }
- class Stop implements Runnable{
- private boolean flag = true ;
- public void run(){
- while (flag){
- System.out.println(Thread.currentThread().getName()+"adasd");
- }
- }
- public void set(){
- flag = false;
- }
- }
复制代码 |