私はJavaのに少し混乱していますnotify()。次の例は教科書からのものです。
public synchronized consume() {
while(queue.isEmpty()) {
try{ wait(); } catch (InterruptedException e) {}
}
// Assume that getElement() notifies to producers.
element = queue.getElement();
...
}
public synchronized produce() {
while(queue.isFull()) {
try{ wait(); } catch (InterruptedException e) {}
}
element = new Element();
...
queue.addElement(element);
notifyAll();
}
produce()上記の例の方法はよく理解しています。notifyAll()しかし、最初の方法()の最後に使用しない理由を誰かに教えてもらえますconsume()か?要するに、なぜこれが好きではないのですか?
public synchronized consume() {
while(queue.isEmpty()) {
try{ wait(); } catch (InterruptedException e) {}
}
// Assume that getElement() notifies to producers.
element = queue.getElement();
...
notifyAll();
}
どうもありがとう!
よろしくお願いします。