Bounded-Buffer with a TimerTask

Discussion in 'Java' started by antonioatt@yahoo.it, Jun 21, 2007.

  1. Guest

    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
     
    , Jun 21, 2007
    #1
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Liz

    TimerTask priority

    Liz, Apr 17, 2004, in forum: Java
    Replies:
    3
    Views:
    3,508
    Roedy Green
    Apr 18, 2004
  2. iksrazal

    TimerTask inside Runnable?

    iksrazal, Jun 29, 2004, in forum: Java
    Replies:
    5
    Views:
    5,521
    iksrazal
    Jul 1, 2004
  3. Jeff

    TimerTask destruction

    Jeff, Aug 19, 2004, in forum: Java
    Replies:
    4
    Views:
    1,159
  4. tkonrath
    Replies:
    4
    Views:
    6,620
    Roedy Green
    Oct 17, 2005
  5. Replies:
    0
    Views:
    361
  6. smartnhandsome

    TimerTask as Filewatcher.

    smartnhandsome, Oct 31, 2007, in forum: Java
    Replies:
    11
    Views:
    1,103
  7. Szabolcs Ferenczi

    Fast lock-based FIFO alias bounded buffer

    Szabolcs Ferenczi, Mar 21, 2008, in forum: C++
    Replies:
    23
    Views:
    1,981
    Chris Thomasson
    Mar 22, 2008
  8. Larry

    bounded buffer

    Larry, Jan 23, 2010, in forum: C++
    Replies:
    2
    Views:
    520
    Larry
    Jan 25, 2010
Loading...