  |
Il ciclo while non può diventare un if |
  |
non è detto che dopo una notify() le condizioni siano
soddisfatte |
  |
La notify riattiva un solo thread |
  |
Per riattivare tutti i thread in attesa si usa notifyAll() |
  |
Però è più inefficiente. |
class Stack {
int[] stack = new int[10]; int top=0;
synchronized void push(int x) {
while(top>stack.length)
wait();
stack[top++]=x;
notify();
}
synchronized void pop() {
while(top==0)
wait();
int r = stack[--top];
notify();
return r;
}
}
|