user1177969
user1177969

Reputation: 105

Shopping queue-line with some priority

I have a typical problem which I suppose can be handled quite easily in Java.

I have a shopping queue mainly two, express and normal. And then there are customers normal and priority. Priority customers are assigned express lane if it is free else they can queue in express lane. Express lane as name suggest takes 1 min to process item and normal lane take couple of min to process an item.

Now let’s suppose time start at zero and in first min a normal customer arrives with 5 items, He should be assigned to normal lane and should checkout in 10 min. In the mean time in 5th min priority customer comes with 2 items then he should be assigned to express lane and should be processed in 2 min.

Some thoughts, direction, pointers around this typical threads queue related problem would be great.

Upvotes: 0

Views: 370

Answers (2)

xagyg
xagyg

Reputation: 9711

This may help with formulating an answer. This gives priority to writers over readers. You may be able to get some ideas from it.

//@author: j.n.magee 11/11/96
//
// The Read Write Monitor Class - Writers priority
//
class ReadWritePriority implements ReadWrite{
  private int readers =0;
  private boolean writing = false;
  private int waitingW = 0; // no of waiting Writers.

  public synchronized void acquireRead()
             throws InterruptedException {
    while (writing || waitingW>0) wait();
     ++readers;
  }

  public synchronized void releaseRead() {
    --readers;
    if (readers==0) notifyAll();  // corrected
  }

  public synchronized void acquireWrite()
             throws InterruptedException {
    ++waitingW;
    while (readers>0 || writing) wait();
    --waitingW;
    writing = true;
  }

  public synchronized void releaseWrite() {
    writing = false;
    notifyAll();
  }
}

Upvotes: 0

Kaushik Sivakumar
Kaushik Sivakumar

Reputation: 205

my suggestion

have an interface - customer

and 2 classes which will extend that normal and express

have 2 queues one which takes in only express and other which takes in customer(both normal and express)

for a express customer

check estimated times he would need to wait/finish in express lane and normal lane)there can be 1 in express and 0 in normal lane, which means he might finish faster in normal

if express is quicker,check if express queue has a slot

if yes put him in dat

else

try in normal if queue has a slot

if yes put him in dat

else

drive him away

for normal customer

try in normal

if yes put him in dat if queue has a slot

else

drive him away

Upvotes: 1

Related Questions