Ciao a tutti,
Il programma che segue è molto banale tuttavia non riesco a capire alcune cose.
Il programma ha l'obbiettivo di creare 4 produttori (ProducerThread) e 3 consumatori ConsumerThread, i loro accessi alla risorsa comune BufferMonitor è realizzato un "monitor".
Riporto il codice e poi la mia domanda
Codice PHP:
public class BufferMonitor {
private int value;
boolean empty;
public BufferMonitor(){
value=10000;
empty=true;
}
public synchronized void putBuffer(int i){
while(emptyBuffer()==false){ //buffer is full
try {
wait();
System.out.print("Producer waiting \n");
} catch (InterruptedException e) {
System.out.print(e);
}
}
value=i;
empty=false;
notifyAll();
}
public synchronized int getBuffer(){
System.out.print("getBuffer afther "+value +"\n");
while (emptyBuffer()==true){ //buffer is empty
try{
wait();
System.out.print("Consumer waiting \n");
}catch(InterruptedException e){
System.out.print(e);
}
}
System.out.print("getBuffer before "+value +"\n");
empty=true;
notifyAll();
return value;
}
public boolean emptyBuffer(){
return empty;
}
}