📄 barbershop.java
字号:
notifyAll();
customerOnSofa ++;
customerStandCount --;
customerSofaQ[sofaTop] = id;
sofaTop =(sofaTop+1)%sofaSize;
repaint();
updateCustomerStatus(applet, id, 5); //sitting on sofa
notifyAll();
}
/* The customer will first wait for his/her turn, then an available barber chair.
* He/she will spend a random time on the chair before send the ready flag to the barber.
*/
public synchronized void sitBarberChair(BarberShopApplet applet, int id)
{
while(customerSofaQ[sofaBottom] != id)
{
System.out.println("Customer " + id + "is waiting for the chair turn");
try{ wait(); } catch(InterruptedException e) { }
}
while(chairFull())
{
try { wait(); }catch(InterruptedException e) {}
}
customerSofaQ[sofaBottom] = 0;
sofaBottom =(sofaBottom+1)%sofaSize; //get up from sofa
customerOnSofa --;
customerOnChair ++;
for(int i = 1; i <= chairSize; i++)
{
if(customerChairQ[i] == 0)
{
customerChairQ[i] = id;
customerReady[i] = id;
i = chairSize; // get out of the loop
}
}
updateCustomerStatus(applet,id, 6);
repaint();
try{
applet.c[id].sleep((int) (Math.random()*frameDelay));
}catch(InterruptedException e) { }
notifyAll();
}
/* If there is a customer waiting and no cashier, a barber will be a cashier when he/she
* is not cutting hair.
*/
public synchronized void getCashierLock(BarberShopApplet applet, int bid)
{
if((wantPayCount > 0) && (hasCashier!= 1))
{
hasCashier= 1;
cashierID = bid;
//updateBarberStatus(applet, bid, 9); // a cashier right now
repaint();
System.out.println("Barber " + bid + " got the cashier Lock right now");
notifyAll();
}
}
public synchronized void performCashier(BarberShopApplet applet, int bid)
{
while(wantPayCount > 0)
{
System.out.println("Barber " + bid + " is a cashier right now");
updateBarberStatus(applet, bid, 2);
try{ wait(); } catch(InterruptedException e) {}
}
cashierID = 0;
hasCashier= 0;
notifyAll();
}
/* The customer will wait for a cashier first, then wait for his/her turn to pay.
* It will take random time to get the receipt, the customer then will leave the shop.
*/
public synchronized void waitPay(BarberShopApplet applet, int cid)
{
while(customerPayQ[payBottom] != cid)
{
if((applet.requestFlag == 1) && (cid == 3))
repaintFlag = 1;
try{ wait(); } catch(InterruptedException e) { }
}
if(applet.requestFlag == 1) // for 1st process in unfair situation
{
while(true)
{
try{ applet.c[cid].sleep((int) (Math.random()*frameDelay)); }
catch(InterruptedException e) { }
}
}
while(hasCashier!= 1)
{
try{ wait(); } catch(InterruptedException e) { }
}
try{ applet.c[cid].sleep((int) (Math.random()*frameDelay)); }
catch(InterruptedException e) {}
updateCustomerStatus(applet, cid, 9);
payBottom++;
wantPayCount --;
exitID = cid;
exitArray[exitTop] = cid;
exitTop ++;
customerCount --;
repaint();
notifyAll();
}
public synchronized void updateCustomerStatus(BarberShopApplet applet, int cid, int status)
{
applet.c[cid].status = status;
applet.mc.println(status, "c", cid);
}
public synchronized void updateBarberStatus(BarberShopApplet applet, int bid, int status)
{
applet.b[bid].status = status;
applet.mc.println(status, "b", bid);
}
public void clear()
{
size = 4; //default buffer size
customerCount = 0;
payTop = payBottom = 1;
chairTop = chairBottom = 0;
sofaTop = sofaBottom = 0;
customerTop= customerBottom= 1;
outTop = outBottom = 0;
finishedCustomerQ = new int[11];
customerOut = new int[2];
customerOnSofa = 0; //the count of customers on the sofa
customerOnChair = 0; //the count of customers on the barber chairs
customerStandCount = 0; //the count of customers standing
wantPayCount = 0; //the count of customers waiting for paying
hasCashier = 0;
cashierID = 0; //the barber ID for who is performing as a cashier
exitID = 0;
exitTop = 0;
repaintFlag = 0;
}
/*
* Draw the barber shop on the canvas
*/
public void paint(Graphics g){
g.setFont(new Font("TimesRoman", Font.BOLD, 12));
g.setColor(Color.blue);
int xpos = 120;
int ypos = 10;
g.setFont(new Font("TimesRoman", Font.BOLD, 18));
/************************************/
/* Draw Barber Chairs on the canvas */
/************************************/
g.drawString("Barber Chairs", xpos+150, ypos+5);
for(int i = 1; i <= chairSize; i++)
{
g.draw3DRect(xpos+100+70*(i-1), ypos+20, 28, 28, true);
if(i != cashierID) g.drawString("B"+i, xpos+103+70*(i-1), ypos+70);
}
g.setColor(Color.red);
for(int j=1; j <= chairSize; j ++)
{
if(customerChairQ[j] != 0)
{
g.drawString(Integer.toString(customerChairQ[j]), xpos + 105 + 70*(j-1), ypos+35);
g.draw3DRect(xpos+100+70*(j-1), ypos+20, 28, 28, true);
}
}
/**********************************/
/* Draw Cashier's waiting queue */
/*********************************/
g.setColor(Color.blue);
g.drawString("Cashier", xpos+410, ypos+45);
g.setFont(new Font("TimesRoman", Font.BOLD, 14));
if(cashierID != 0)
{
g.drawString("B "+cashierID, xpos+430, ypos+20);
}
g.draw3DRect(xpos+410, ypos+60, 60, 20, true);
g.setFont(new Font("TimesRoman", Font.BOLD, 12));
int b = payBottom;
System.out.println("wantPaycount is " + wantPayCount);
if(repaintFlag == 1)
{
for(int i = 0; i < 3; i++)
{
if(customerPayQ[i+b] != 0)
g.drawString("C"+customerPayQ[i+b], xpos+430, ypos+100);
ypos += 20;
}
}
else
{
for(int i = 0; i < wantPayCount; i++)
{
if(customerPayQ[i+b] != 0)
g.drawString("C"+customerPayQ[i+b], xpos+430, ypos+100);
ypos += 20;
}
}
/**********************************/
/* Draw standing room on canvas */
/**********************************/
g.setFont(new Font("TimesRoman", Font.BOLD, 12));
ypos = 10;
g.drawString("Standing Room Area", xpos-100, ypos+160);
b = customerBottom;
for(int i = 0; i < customerStandCount; i++)
{
g.drawString("C"+(i+b), xpos+80-25*i, ypos+120);
}
g.setColor(Color.green);
g.drawString("Entrance", xpos-110, ypos+100);
g.drawString("--------->", xpos-110, ypos+105);
g.setColor(Color.red);
System.out.println("outTop is: " + outTop);
for(int i = outBottom; i <= outTop; i++)
{
if(customerOut[i] > 0)
g.drawString("C "+customerOut[i], xpos-80, ypos+80-20*i);
}
g.setColor(Color.red);
g.drawString("Exit", xpos+530, ypos+10);
for(int i = 0; i < exitTop; i++)
{
if(exitArray[i] != 0)
g.drawString("C" + exitArray[i], xpos+530, ypos+25+15*i);
}
/**********************************/
/* Draw waiting Sofa on canvas */
/**********************************/
xpos = 100;
ypos = 10;
g.setColor(Color.blue);
g.drawString("Sofa", xpos+225, ypos+185);
for(int i = 0; i < sofaSize; i++)
{
g.draw3DRect(xpos+180+28*(i), ypos+140, 28, 28, true);
}
g.setColor(Color.red);
int k = sofaBottom;
for(int j=0; j < customerOnSofa; j++)
{
g.drawString(Integer.toString(customerSofaQ[k]), xpos + (190+28*3) -28*(j), ypos+155);
g.draw3DRect(xpos+180+28*3 - 28*(j), ypos+140, 28, 28, true);
k = (k+1)%sofaSize;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -