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

📄 sleepingbarber.java

📁 一个用java实现的解决理发师睡觉问题的代码
💻 JAVA
字号:
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("First,the barber is sleeping for there is no customer.");
      Database server = new Database();

      Barber[] barberArray = new Barber[NUM_OF_BARBER];

      for (int i = 0; i < NUM_OF_BARBER; 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 < maxCustomer)
        { 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 < server.maxCustomer)
       {
             System.out.println("customer-" + barberNum + " is barbering. There are " + c + " chairs occupied.");
             Database.napping();

             System.out.print("customer-" + barberNum + " is done barbering. ");
             c = server.endWait();

       }
       //System.out.println("customer" + barberNum + " is done babering. There are " + c + " chairs occupied.");
      if(isFull = true)
          break;;

     }
   }

   private Database	server;
   private int       barberNum;
   private boolean   isFull;
}

final class Semaphore//creat the semphore for this program
{
   public Semaphore() {
      value = 0;
   }

   public Semaphore(int v) {
      value = v;
   }

   public synchronized void P() {
      while (value <= 0) {
         try {
            wait();
         }
         catch (InterruptedException e) { }
      }

      value --;
   }

   public synchronized void V() {
      ++value;

      notify();
   }

    private int value;
  }

⌨️ 快捷键说明

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