Hi I am working on the sleeping barber problem. with the addition of having priority customer when they arrive they go in the front of the line and they are the next ones to get a haircut.
I'm using a linkedlist
and if I see a priority customer I put him in the beginning of the list, if the customer is not priority he goes to the end of the list. then I call the wantHaircut method getting the first element of the list.
my problem is that the customer are being processed in the order they arrive, and the priority customer have to wait. here is the code where it all happens. any ideas what I am doing wrong? thanks
public void arrivedBarbershop(Customer c){
if(waiting < numChairs && c.isPriority()){
System.out.println("Customer " + c.getID() + ": is a priority customer - SITTING -");
mutex.up();
customer_list.addFirst(c);
}
else if(waiting >= numChairs && c.isPriority()){
System.out.println("Customer " + c.getID() + ": is a priority customer - STANDING -");
mutex.up();
customer_list.addFirst(c);
}
else if(waiting < numChairs && !c.isPriority()){
waiting++;
System.out.println("Customer " + c.getID() + ": arrived, sitting in the waiting room");
customer_list.addLast(c);
customers.up(); // increment waiting customers
}
else if(waiting >= numChairs && !c.isPriority()) {
System.out.println("Customer " + c.getID() + ": went to another barber because waiting room was full - " + waiting + " waiting");
mutex.up();
}
if(!customer_list.isEmpty()){
this.wantHairCut(customer_list.removeFirst());
}
}
public void wantHairCut(Customer c) {
mutex.up();
barber.down(); // waits for being allowed in barber chair
System.out.println("Customer " + c.getID() + ": getting haircut");
try {
/** haircut takes between 1 and 2 seconds **/
Thread.sleep(Barbershop.randomInt(1, 2) * 1000);
} catch (InterruptedException e) { }
System.out.println("Barber: finished cutting customer " + c.getID() + "'s hair");
c.gotHaircut = true;
cutting.up(); // signals cutting has finished
/** customer must pay now **/
this.wantToCashout(c);
}