- 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();
- }
复制代码
比如这段代码,你有没有注意到while(flag)循环没有大括号?其实这个循环体就下面那一句话,和if不加大括号是一样道理 |