資源簡介
,同步互斥P、V操作。理發店里有5把椅子,顧客進來理發,此時理發師在睡覺,顧客叫醒理發師開始理發,再有顧客進來就在椅子上等候,如果沒有椅子了,就離開

代碼片段和文件信息
public?class?idb02251210
{
???public?static?void?main(String?args[])
???{
??????//Here?is?the?main?function
??????System.out.println(“This?is?a?barber?shop?with?5?chairs?for?waiting.“);
??????System.out.println(“Here?is?20?customers?want?to?barber.“);
??????System.out.println(“Firstthe?barber?is?sleeping?for?there?is?no?customer.“);
??????Database?server?=?new?Database();
??????Barber[]?barberArray?=?new?Barber[NUM_OF_BARBER];
??????for?(int?i?=?0;?i???????{
?????????barberArray[i]?=?new?Barber(i?server);
?????????barberArray[i].start();
?????????try?{//this?try?is?for?controling?the?speed?of?everyone?who?go?into?the?barber?shop
??????????barberArray[i].sleep(1000-20*i);
????????}
????????catch?(InterruptedException?f)?{
????????}
??????}
???}
???private?static?final?int?NUM_OF_BARBER?=?20;
}
class?Database
{
???public?Database()?{//set?the?data
??????waitCount?=?0;
??????maxCustomer?=?5;
??????mutex?=?new?Semaphore(1);
??????db?=?new?Semaphore(maxCustomer?+?1);
???}
???//?customers?and?barber?will?call?this?to?nap
???public?static?void?napping()?{
?????int?sleepTime?=?(int)?(NAP_TIME?*?Math.random()?);
?????try?{?Thread.sleep(sleepTime*100);?}
?????catch(InterruptedException?e)?{}
???}
???public?int?startWait()?{//customers?begin?to?go?into?the?barber?shop
??????db.P();
??????++waitCount;//the?number?of?customers?add?one
??????System.out.println(“There?are?“?+?waitCount?+?“?chairs?occupied.“);
??????if?(waitCount?????????{?mutex.P();}
??????waitCount--;
??????db.V();//the?number?of?customers?substract?one
??????return?waitCount;
???}
???public?int?endWait()?{//there?are?no?cuostomers?and?barber?begin?to?sleep
??????db.P();
??????if?(waitCount?==?0)
??????{
???????????System.out.println(“The?barber?begin?to?sleep?again.“);
??????}
??????mutex.V();
??????db.V();
??????System.out.println(“There?are?“?+?waitCount?+?“?chairs?occupied.“);
??????return?waitCount;
???}
???private?int?waitCount;
???public?int?maxCustomer;
???Semaphore?mutex;??//?controls?access?to?readerCount
???Semaphore?db;?????//?controls?access?to?the?database
???private?static?final?int?NAP_TIME?=?25;
}
class?Barber?extends?Thread
{
???public?Barber(int?r?Database?db)
???{
??????barberNum?=?r;
??????server?=?db;
??????isFull?=?false;
???}
public?void?run()
???{
???int?c;
?????while?(true)//people?is?begin?the?detail?behavior
?????{
???????//System.out.println(“customer-“?+?barberNum?+?“?is?going.“);
???????Database.napping();
???????System.out.println(“customer-“?+?barberNum?+?“?enter?shop.“);
???????c?=?server.startWait();
???????if(c?>=?server.maxCustomer)
???????{
?????????????System.out.println(“customer-“?+?barberNum?+?“have?to?go?away?for?no?chairs.“);
?????????????isFull?=?true;
???????}
???????//?you?have?access?to?have?barbering
???????if(c????????{
?????????????System.out
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????4001??2004-06-10?22:43??SleepingBarber.java
-----------?---------??----------?-----??----
?????????????????4219????????????????????2
- 上一篇:數據結構大作業貪吃蛇和實驗報告
- 下一篇:A*最短路徑算法 最短路徑
評論
共有 條評論