⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 b02251132.java~2~

📁 SleepingBarber的java文件
💻 JAVA~2~
字号:
//package b02251132;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: </p> * @author :黄增博    b02251132 * @version 1.0 */public class b02251132{  public static Semophore sem=new Semophore(1);  public static void main(String[] args)  {    WaitingRoom myWaitRoom=new WaitingRoom(3);//Three seats at all.    Barber myBarber=new Barber(myWaitRoom);    //Start the two thread.    myWaitRoom.start();    myBarber.start();    while(true)    {	//Whether there is customer and barber is free      if(myWaitRoom.customers>0&&myBarber.status==1)      {        System.out.println("One customer wake up the barber.");        myBarber.resume();//Make the barber's thread continue.        myBarber.status=0;//Reset the status,barber is working.      }    }  }}//That is the Semophore.class Semophore{  private int value;  public Semophore(){	  value=0;  }  public Semophore(int v){	  value=v;  }  public synchronized void p(){	  while(value<=0){		  try{			  wait();		  }		  catch (InterruptedException e){}	  }	  value--;  }    public synchronized void v(){		++value;		notify();	}}class Barber extends Thread{  public int status=0;//Reset the barber's working status.  WaitingRoom room;  public Barber(WaitingRoom room)  {    room=room;  }  public void run()  {    while(true)    {      if(room.customers>0)      {        b02251132.sem.p();        room.customers--;//One customer begin to be servered by the barber.        System.out.println("Barber begin to serve a customer.");        System.out.println("There is "+(room.seats-room.customers)+" Seat(s)");        b02251132.sem.v();                try {		          sleep(2000);//Now,the barber is working,it should be 2 seconds.		        }        catch (InterruptedException e) {}        System.out.println("Barber finished a customer,the customer leaves.");        System.out.println("There is "+(room.seats-room.customers)+" Seat(s)");      }      else      {        status=1;//Set the barber's working status become free.        System.out.println("No more customers,so barber go to sleep.");        suspend();//Make the barber's thread pause.      }    }  }}class WaitingRoom extends Thread{  public int seats;  public int customers;  public WaitingRoom(int n)  {    customers=0;    seats=n;  }  public void run()  {    while(true)    {      try      {        sleep(1000);//Time between two customers' coming time.(1 second)      }catch(InterruptedException e){}    if(customers<seats)//If there is free seats available.    {      b02251132.sem.p();      customers++;//A customer come and occupy the seat.      b02251132.sem.v();      {        System.out.println("One customer comes and sits in the waiting room.");        System.out.println("There is "+(seats-customers)+" Seat(s)");      }     }      else      {        System.out.println("One customer arrives, but no free seats, so he leaves.");      }    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -