2015-11-07 15 views
-1

我正在做一个项目,它要求我们采用休眠的理发器Java代码Here并将其重新制作成酒店。有一些规则,但我具有3对similiar那些麻烦:从并发的Java信号量中去除sleep()

  • 线程可以不使用睡眠作为协调
  • 忙等待(轮询)的手段是不允许
  • 每个线程应该只打印自己的活动
  • 如何去消除睡眠方法,以及什么我代替他们?

    我想出了这些信号灯:

    import java.util.concurrent.Semaphore; 
    import java.util.*; 
    
    public class Hotel extends Thread { 
    
    public static Semaphore deskClerk = new Semaphore(0, false); //semaphore representing the deskClerk 
    public static Semaphore guestList = new Semaphore(0);  //semaphore representing the guests. 
    public static Semaphore bellHop = new Semaphore(0, false); //semaphore representing the bellHop. 
    public static Semaphore accessSeats = new Semaphore(1); //the seats that the guestList sit down on upon entering the hotel 
    int roomCounter = 0; // supposed to be an incrementing global variable integer that increments with each customer acquisition by the front desk employee 
    int guestID, bellhopID, clerkID; 
    
    // the number of chairs in this hotel is 15. 
    public static final int CHAIRS = 15; 
    
    public static int numberOfFreeSeats = CHAIRS; 
    

    而且这些类:

    游客

    class Guest extends Thread { 
        // we create the integer iD which is a unique ID number for every guest and a boolean notServed which is used in the Guest waiting loop 
        int iD; 
        boolean notServed = true; 
        int baggage; 
    
        // Guest Constructor 
        public Guest(int name, Hotel h) { 
        iD = name; 
        guestID = iD; 
        Random rn = new Random(); 
        baggage = rn.nextInt(6); 
        //need a random number from 0-5. Represents number of bags. 
        } 
    
        public int getBaggage() { 
         return baggage; 
        } 
    
        public boolean checkBaggage() { 
         if(this.getBaggage() > 2) { 
          return true; 
         } 
         else { 
          return false; 
         } 
        } 
    
        public void run() { 
        while (notServed) { // as long as the customer has not been served 
        try { 
        accessSeats.acquire(); // tries to get access to the hotel chairs 
        if (numberOfFreeSeats > 0) { // if there are any free seats 
         System.out.println("Guest " + this.iD + " has entered hotel with " + this.getBaggage() + " bags"); 
         numberOfFreeSeats--; // sitting down on a chair 
         guestList.release(); // notify the front desk salesmen that there is a customer 
         accessSeats.release(); // don't need to lock the chairs 
          // anymore 
         try { 
          roomCounter++; 
          deskClerk.acquire(); // now it's this guestList turn 
          // but we have to wait if the deskClerk is busy 
          notServed = false; // the guest will now leave after they are served. 
          this.get_Served(); // Serving the guests 
          if (this.checkBaggage() == true){ 
           bellHop.release(); 
           System.out.println("Guest " + this.iD + " requests help with bags"); 
           //from here, need a way for the bellhop to signal the guest that they should tip the bellhop, receive the bags, and retire. 
          } 
          else { 
           System.out.println("Guest " + this.iD + " enters room #" + roomCounter + " and retires for the evening"); 
          } 
         } 
         catch (InterruptedException ex) { 
         } 
        } 
        else { // there are no free seats 
         System.out.println("There are no free seats. Guest " + this.iD + " has left the hotel."); 
         accessSeats.release(); // release the lock on the seats 
         notServed = false; // the customer will leave since there are no spots in the queue left. 
        } 
        } catch (InterruptedException ex) { 
        } 
        } 
        } 
    
        /* this method simulates a front desk employee processing a guest */ 
        public void get_Served() { 
        System.out.println("Guest " + this.iD 
        + " receives room key for room " + roomCounter + " from front desk employee " + clerkID); 
        try { 
        deskClerk.release(); 
        } 
        finally{ 
        } 
        } 
    

    书记

     /* THE CLERK THREAD */ 
    
    class Clerk extends Thread { 
        boolean busy = false; 
        public Clerk(int name, boolean bus) { 
         clerkID = name; 
         busy = bus; 
        } 
    
        // this method will simulate booking rooms 
    public void serve() { 
        System.out.println("Front desk employee " + this.getId() + " registers guest " + guestID + " and assigns room " + roomCounter); 
        try { 
        sleep(3000); 
        } catch (InterruptedException ex) { 
        } 
    } 
    
        public void run() { 
        while (true) { // runs in an infinite loop 
        try { 
        guestList.acquire(); // tries to acquire a customer - if none is available he goes to sleep 
        accessSeats.acquire(); // at this time he has been awaken -> want to modify the number of available seats 
        numberOfFreeSeats++; // one chair gets free 
        deskClerk.release(); // the clerk is ready to serve the customer 
        accessSeats.release(); // we don't need the lock on the hotel chairs  anymore 
        this.serve(); // serving the customer 
        } 
        catch (InterruptedException ex) { 
        } 
        } 
        } 
    } 
    

    侍者

     /*THE BELLHOP THREAD */ 
    class bellHop extends Thread { 
        boolean busy = false; 
        public bellHop(int name, boolean f) { 
         bellhopID = name; 
         busy = f; 
        } 
    
        public void handleBaggage() { 
         if(bellHop.availablePermits() != 0){ 
         System.out.println("Bellhop " + this.getId() + " receives bags from guest " + guestID); 
         System.out.println("Bellhop " + this.getId() + " delivers bags to guest " + guestID); 
    
         try { 
          bellHop.acquire(); 
         } 
         catch (InterruptedException ex){ 
          System.out.println(this.getId() + " Cannot handle baggage"); 
         } 
         } 
        } 
    
        public void run() { 
        while (true) { // runs in an infinite loop 
        try { 
        this.handleBaggage(); // take baggage up to guests room and receive a tip 
        sleep(100); 
        } 
        catch (InterruptedException ex) { 
        } 
        } 
        } 
    } 
    

    主要&酒店的run()

    // main method 
    public static void main(String args[]) { 
        System.out.println("Simulation Starts"); 
        Hotel hotel = new Hotel(); // Creates a new hotel 
        Clerk george = hotel.new Clerk(1, false); System.out.println("Front desk employee " + george.getId() + " created"); 
        Clerk greg = hotel.new Clerk(2, false); System.out.println("Front desk employee " + greg.getId() + " created"); 
        bellHop bill = hotel.new bellHop(1, false); System.out.println("Bellhop " + bill.getId() + " created"); 
        bellHop bob = hotel.new bellHop(2, false); System.out.println("Bellhop " + bob.getId() + " created"); 
        hotel.start(); // begins the simulation 
        greg.start(); 
        george.start(); 
        bill.start(); 
        bob.start(); 
    } 
    
    public void run() { 
        Hotel h = new Hotel(); 
    
        // This method creates 25 new guests 
        for (int i = 1; i < 10; i++) { 
         Guest aGuest = new Guest(i, h); 
         aGuest.start(); 
         try { 
          sleep(3000); 
         } 
         catch (InterruptedException ex) {   
         } 
        } 
    } 
    } 
    
    +0

    我投票结束这个问题作为题外话,因为它是作业。 – Isaac

    +0

    @Isaac:作业题目不会自动脱离主题。请参阅[我如何问及回答作业问题](http:// meta。在meta上的stackexchange.com/questions/10811/how-do-i-ask-and-answer-homework-questions)。 –

    回答

    0

    睡眠意味着浪费时间(或者更具体地,浪费了线程)。线程在给定的时间过去之前什么都不做(或线程被中断)。

    通常你不想浪费时间 - 你只是想等待别的东西发生。例如,店员不想在没有客人的情况下睡觉,而是店员应该等待客人已经到达的信号,然后立即为该客人提供服务。如果您在Java中使用监视器锁,那么文员将在显示器上显示wait(),直到另一个线程notify()为止。

    Semaphore可用于构建正确类型的并发构造,但它不是理想的适合这种类型的问题。对于“客户(客人)”线程的一般问题,每个线程都想从中央调解员(职员)获取资源(房间)“我实际上会使用Future来表示”排队等待“状态和相应的值。当然,这可能不是你的教授试图教这个作业的技巧。 :-)

    在你表现出理发例子,也是在你的代码,您还使用休眠到模拟的工作,你没有真正做 - 例如,理发师不实际剪掉任何头发,只需几秒钟就可以模拟理发需要一些时间。这听起来像是这个任务所允许的。然而,在现实世界的系统中,你不需要睡眠,只是线程正在进行的实际工作将花费大量的时间。

    +0

    谢谢!所以我需要在try块内部添加一个wait()和notify()系统给每个类? – Ash

    +0

    @Ash:关键是了解*哪些线程需要等待*,以及*他们在等待什么*。如果一个线程正在等待,那么这意味着其他线程现在正在做或将来会做一些让线程停止等待的东西。 *某事*是你需要识别的东西,然后当有什么事情发生时你需要发信号通知等待的线程。有很多方法可以做到这一点,从你的问题来看,最好的解决方案是什么并不清楚(正如我上面所说的,如果我自己编码的话,我实际上会使用'Future's)。 –