Bounded-Buffer with a TimerTask

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top