📄 barbershop.java
字号:
/* BarberShop.java
*
* This class is the program that controls all the activities of customers and barbers.
* This barber shop has maximum capacity of 8 customers. There are 3 barbers in the
* shop.
* There are 4 sofa seats and 3 barber chairs in the shop. Three barbers will spend
* their time on sleeping for a while, cutting hair and performing as a cashier while
* he/she is not cutting hair.
* The customer will wait outside of the shop if there are already 8 customers in the shop.
* After the customer enters into the shop, he/she will wait in the standing area first, then
* sit on sofa in the order that he/she arrives. He/she will sit on the barber chair if there
* is any barber chair available. After the barber finishes the hair cut for him/her, this
* customer will wait to pay, then exit from the barbershop.
*
*
* @author: Jie Zhang
* Last Updated: 07/19/2002
*/
import java.awt.*;
public class BarberShop extends Canvas
{
private int chairSize = 3;
private int sofaSize = 4;
private int frameDelay = 3560;
private int[] customerSofaQ; //the queue to hold the customers on the sofa
private int[] customerStandQ; //the queue to hold the customers on the standing area
private int[] customerChairQ; //the queue to hold the customers on Barber Chairs
private int[] customerPayQ; //the queue to hold the customers waiting for paying.
private int[] customerReady; //cutomerReady[i] = 1, customer i is ready for barber 1
private int[] finishedCustomerQ; //the array to hold the cut finish flags
private int[] paidCustomerQ;
private int[] exitArray; //the array hold all the customers in the order they exit shop
private int sofaTop, sofaBottom; //for customerSofaQ
private int chairTop, chairBottom; //for customerChairQ
private int payTop, payBottom; //for customerPayQ
private int customerTop, customerBottom;//for customersQ
private int customerOnSofa; //the count of customers on the sofa
private int customerOnChair; //the count of customers on the barber chairs
private int customerStandCount; //the count of customers standing
private int wantPayCount; //the count of customers waiting for paying
private int hasCashier;
private int cashierID; //the barber ID for who is performing as a cashier
private int exitID; //the customer ID who is leaving the barbershop
private int exitTop;
private int customerCount;
private int size;
private int[] customerOut;
private int outTop;
private int outBottom;
private int repaintFlag = 0;
private Font font;
private FontMetrics fm;
private int x; //customer consumed item
public BarberShop( ){
size = 4; //default buffer size
customerTop = customerBottom = 1;
payTop = payBottom = 1;
chairTop = chairBottom = 0;
sofaTop = sofaBottom = 0;
customerCount = 0;
customerOnSofa = 0;
customerOnChair = 0;
customerStandCount = 0;
wantPayCount = 0;
hasCashier = 0;
cashierID = 0;
exitID = 0;
exitTop = 0;
finishedCustomerQ = new int[11];
customerOut = new int[2];
outTop = outBottom = 0;
setSize(size);
resize(500, 300);
setBackground(Color.white);
font = new Font("TimesRoman", Font.BOLD, 18);
fm = getFontMetrics(font);
}
public void setSize(int s)
{
size = s;
if(size > 8) customerStandCount = 8;
else customerStandCount = size;
int tmpCount = 0;
if(size > 8)
{ tmpCount = size - 8;
System.out.println("the tmpCount is " + tmpCount);
for(int i = 0; i < tmpCount; i++)
{
customerOut[i] = 9+i;
}
}
outBottom = 0;
outTop = 1;
customerSofaQ = new int[sofaSize];
customerChairQ = new int[chairSize+1];
customerPayQ = new int[11];
customerReady = new int[size+1]; //the maximum customer size is 10
paidCustomerQ = new int[size+1];
exitArray = new int[size];
/* Initialize the array*/
for(int i = 1; i <=size ; i++)
{
customerReady[i] = 0;
}
repaint();
}
public synchronized boolean chairFull(){
return customerOnChair == chairSize;
}
public synchronized boolean sofaFull(){
return customerOnSofa == sofaSize;
}
/**
* If there is no customer on this barber chair, then check if there is a cashier's job.
* If he/she is doing a cashier's job, he/she will finish that first. Then the barber
* will wait for the customer ready. After customer ready, he/she will cutting the hair
* for a random time period.
* The barber will just cutting hair for the customer sitting on
* his/her chair. i.e. barber 1 will just cutting hair for the customer
* sitting on the barber chair 1.
*/
public synchronized void cutHair(BarberShopApplet applet, int id)
{
if(customerReady[id] == 0) getCashierLock(applet, id);
if(cashierID == id) performCashier(applet, id);
while(customerReady[id] == 0) //if there is no customer is waiting
{
updateBarberStatus(applet, id, 4);
try{ wait(); }catch(InterruptedException e){
System.err.println("Exception " + e.toString());
}
}
System.out.println("customerReady are: ");
for(int i = 0; i <= 3; i++)
{
System.out.println(Integer.toString(customerReady[i]));
}
int x = customerReady[id];
applet.b[id].customerID = x;
applet.c[x].barberID = id;
System.out.println("x is " + x);
applet.b[id].status = 1;
applet.mc.println(applet.b[id].status, "b", id, x);
updateCustomerStatus (applet, x, 1); //cutting Hair
//repaint();
notifyAll();
}
/* When the barber finishes the haircutting, he/she will signal the customer finish flag
* and update corresponding variables.
*/
public synchronized void finishCut(BarberShopApplet applet, int id)
{
customerReady[id] = 0;
int y = applet.b[id].customerID;
/* The following codes added to animate one of the unfair situation:
* The process hold the resources unnecessarily.
* The finished process has to wait until the prior process to finish
* (the first process in this program).
*/
if(applet.haltFlag == 1)
{
if(y != 1)
{
updateCustomerStatus(applet, y, 10);
updateBarberStatus(applet, id, 1);
}
else
{
while(true) /* to keep the barber status in cutting hair */
{
try { wait(); } catch(InterruptedException e) {}
}
}
}
else if(applet.requestFlag == 1)
{
System.out.println("process is " + y);
if(y == 1)
{
while(finishedCustomerQ[2] != 1)
{ try { wait(); } catch(InterruptedException e) {}
}
updateCustomerStatus(applet, y, 11);
}
else if(y==3)
{
while(finishedCustomerQ[2] != 1)
{
try { wait(); } catch(InterruptedException e) {}
}
updateCustomerStatus(applet, y, 7);
}
else
{
updateCustomerStatus(applet, y, 7); //waiting for pay
}
customerChairQ[id] = 0;
applet.b[id].customerID = 0;
applet.c[y].barberID = 0;
repaint();
finishedCustomerQ[y] = 1;
notifyAll();
wantPayCount ++;
customerPayQ[y] = y;
repaint();
customerOnChair --;
notifyAll();
if(wantPayCount > 0) getCashierLock(applet, id);
if(cashierID == id) performCashier(applet, id);
else updateBarberStatus(applet, id, 4);
}
else // To handle the processes in fair situation
{
updateCustomerStatus(applet, y, 7);
customerChairQ[id] = 0;
applet.b[id].customerID = 0;
applet.c[y].barberID = 0;
repaint();
System.out.println("customer " + y + " finish cutting");
finishedCustomerQ[y] = 1;
wantPayCount ++;
customerPayQ[payTop] = y;
payTop++;
repaint();
customerOnChair --;
notifyAll();
if(wantPayCount > 0) getCashierLock(applet, id);
if(cashierID == id) performCashier(applet, id);
else updateBarberStatus(applet, id, 4);
}
}
/* The customer will first wait for his/her turn, then a available seat on the sofa.
*/
public synchronized void sitSofa(BarberShopApplet applet, int id)
{
while(customerBottom != id)
{
System.out.println("customer " + id + " is waiting for the turn");
try{ wait(); } catch(InterruptedException e) {}
}
customerCount++;
notifyAll();
if(id > 8)
{ customerStandCount ++;
outBottom ++;
repaint();
}
while(sofaFull())
{
try { wait(); }catch(InterruptedException e) {}
}
customerBottom++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -