0

I have a producer that produces products and a consumer that consumes them. What I want is, if a product is not consumed in 5 minutes I want it to be destroyed.

This is the part of the producer:

boolean full = false;
public void produce(int p) throws RemoteException {
        //choses a or b randomly 
        //if a or b spot is occupied, thread must wait()

        synchronized(this){
            if ((int)((Math.random()*10)%2) == 1){
                while (a!=-1){try {
                        wait();
                    } catch (InterruptedException ex) {
                        Logger.getLogger(CHServer.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                a = p;
                if (b!=-1) full = true;
                notifyAll();
            }
            else {
                while (b!=-1){try {
                        wait();
                    } catch (InterruptedException ex) {
                        Logger.getLogger(CHServer.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                b = p;
                if (a!=-1) full = true;
                notifyAll();
            }
        }
    }

a & b are supposed to be my products.

I really don't know how can I measure that time for example when the thread is waiting or a client isn't trying to consume that product. This piece of code , is running on a RMI java server.

2 Answers2

2

I'd just using a scheme like this: when you produce something use java.util.Timer() to set a timer for 5 minutes in the future. When the item is consumed, .cancel() the timer. If the timer goes off, do whatever cleanup you need to do.

John Hascall
  • 9,176
  • 6
  • 48
  • 72
  • can you give an example? Cheers – ExceptionCaught Sep 08 '14 at 19:51
  • See http://stackoverflow.com/questions/4044726/how-to-set-a-timer-in-java for info on playing with Timers – John Hascall Sep 08 '14 at 19:56
  • @ExceptionCaught Timer will probably work but nowadays [ScheduledThreadPoolExecutor](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ScheduledThreadPoolExecutor.html) is preferable. See comments in [Timer](http://docs.oracle.com/javase/8/docs/api/java/util/Timer.html) class doc. – Stuart Marks Sep 08 '14 at 21:21
1

It looks like you are implementing a queue with 2 slots, the 2 slots being a and b. But the strategy of chosing a random slot isn't optimal. You might wait for a slot while the other is empty. Also, the consumer cannot tell which one of a or b you produced first.

Anyway, if I understand the code well, you could

  1. save the current time at the time you enter the loop.
  2. every time you wake up from wait(), compute the delay since the entry. If it exceeds your time limit, return or throw an exception. Else, check if the slot is available.
  3. to make sure not to wait forever, you should specify a delay on your wait. You could either wait a fixed time, maybe 1 second, or compute the wait time remaining until the 5-minute deadline.
Florian F
  • 1,300
  • 1
  • 12
  • 28