class SimpleConsumer extends Threads {
public SyncQueue q;
SimpleConsumer(SyncQueue q) { this.q = q; }
public void run () { doit(); }
public synchronized void doit() {
while(true){
try{
while(q.isEmpty()) { wait(); }
System.out.println((String)q.Dequeue());
}
catch (Exception e) { System.out.println("Got exception:" +e); }
}
}
}
而且我有另一个类将项目添加到同一个对象 SyncQueue 并执行 notifyAll();
class SimpleProducer extends Threads {
public SyncQueue q;
SimpleProducer(SyncQueue q) { this.q = q; }
public void run() { doit(); }
public synchronized void doit() {
while(true){
try{
sleep(1000);
q.Enqueue("Item");
notifyAll();
} catch(Exception e) { System.out.println("Got exception:" +e); }
}
}
}
}
如果我从不同的类方法执行 notifyAll(),SimpleConsumer 会唤醒吗?