Пример #1
0
  static void AdultItinerary() {
    System.out.println("--- called adult itinerary");

    /* This is where you should put your solutions. Make calls
     to the BoatGrader to show that it is synchronized. For
     example:
         bg.AdultRowToMolokai();
     indicates that an adult has rowed the boat across to Molokai
    */

    pilotLock.acquire();

    // While the boat is not at Oahu or there are more than 2 children on Oahu,
    // put the adult on Oahu thread to sleep (because we wouldn't send and adult in
    // these scenarios).
    while (boatLocation != 0 || numChildrenOnOahu >= 2) {
      System.out.println("--- can't send an adult to Molokai right now");
      adultReadyAtOahu.sleep();
    }

    System.out.println("--- sending an adult to Molokai");
    numAdultsOnOahu--; // an adult is rowing Oahu --> Molokai, so decrement
    bg.AdultRowToMolokai();
    numAdultsOnMolokai++; // adult has rowed over, so increment
    printState();
    boatLocation = 1; // boat is now at Molokai
    childReadyAtMolokai.wake(); // wake up child at Molokai since it's now their turn			

    pilotLock.release();
  }
Пример #2
0
 static void SampleItinerary() {
   // Please note that this isn't a valid solution (you can't fit
   // all of them on the boat). Please also note that you may not
   // have a single thread calculate a solution and then just play
   // it back at the autograder -- you will be caught.
   System.out.println("\n ***Everyone piles on the boat and goes to Molokai***");
   bg.AdultRowToMolokai();
   bg.ChildRideToMolokai();
   bg.AdultRideToMolokai();
   bg.ChildRideToMolokai();
 }