A
antonioatt
I have a Producer/Consumer (or Bounded-Buffer) Problem.
Here the code:
public class Buffer<T>{
int n;
T[] buffer;
int in=0,out=0,count=0;
public Buffer(int n){
this.n=n;buffer=new T[n];
}
public synchronized T get(){
while(count==0)
try{wait();}catch(InterruptedException e){}
T risp=buffer[out];out=(out+1)%n;count--;
notifyAll();
return risp;
}
public synchronized void put(T value){
while(count==n)
try{wait();}catch(InterruptedException e){}
buffer[in]=value;in=(in+1)%n;count++;
notifyAll();
}
}
I must modify using Timer e TimerTask
the get method :
public T get(long timeout) throws TimeoutException
If get riceive from buffer before timeout OK elsewere it terminate
throwing a TimeoutException.
Someone can help me ! tanks
Here the code:
public class Buffer<T>{
int n;
T[] buffer;
int in=0,out=0,count=0;
public Buffer(int n){
this.n=n;buffer=new T[n];
}
public synchronized T get(){
while(count==0)
try{wait();}catch(InterruptedException e){}
T risp=buffer[out];out=(out+1)%n;count--;
notifyAll();
return risp;
}
public synchronized void put(T value){
while(count==n)
try{wait();}catch(InterruptedException e){}
buffer[in]=value;in=(in+1)%n;count++;
notifyAll();
}
}
I must modify using Timer e TimerTask
the get method :
public T get(long timeout) throws TimeoutException
If get riceive from buffer before timeout OK elsewere it terminate
throwing a TimeoutException.
Someone can help me ! tanks