- public synchronized void set(String name)
- {
- while(flag)
- try{this.wait();}catch(Exception e){}//t1(放弃资格) t2(获取资格)
- this.name = name+"--"+count++;
- System.out.println(Thread.currentThread().getName()+"...生产者.."+this.name);
- flag = true;
- this.notifyAll();
- }
- // t3 t4
- public synchronized void out()
- {
- while(!flag)
- try{wait();}catch(Exception e){}//t3(放弃资格) t4(放弃资格)
- System.out.println(Thread.currentThread().getName()+"...消费者........."+this.name);
- flag = false;
- this.notifyAll();
- }
复制代码
如上代码,我想请问的是,如果用notifyAll()唤醒所有的线程,那么线程t3,t4一起被唤醒了,那么还是会存在连续两次打印输出System.out.println(Thread.currentThread().getName()+"...消费者........."+this.name)的情况吧,为什么? |