coderbot

Restaurant

Jul 16th, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.28 KB | None | 0 0
  1. /*
  2.     Jatin Thakur
  3.     coderbots.blogspot.com
  4. */
  5.  
  6. import java.util.concurrent.*;
  7. import java.util.concurrent.locks.*;
  8.  
  9. class BusBoy implements Runnable {
  10.  
  11.     Lock lock = new ReentrantLock();
  12.     Condition condition = lock.newCondition();
  13.  
  14.     Restaurant restaurant;
  15.  
  16.     BusBoy(Restaurant r) {
  17.         restaurant = r;
  18.     }
  19.  
  20.     public void run() {
  21.         try {
  22.             while (!Thread.interrupted()) {
  23.  
  24.                 lock.lock();
  25.                 try {
  26.                     condition.await();
  27.                     System.out.println("BusBoy is Cleaning up!\n");
  28.                 } finally {
  29.                     lock.unlock();
  30.                 }
  31.             }
  32.         } catch (InterruptedException e) {
  33.             System.out.println("BusBoy interrupted!");
  34.         }
  35.     }
  36.  
  37. }
  38.  
  39. class Meal {
  40.  
  41.     private final int orderNum;
  42.     volatile int cleanUp = 0;
  43.  
  44.     public Meal(int orderNum) {
  45.         this.orderNum = orderNum;
  46.     }
  47.  
  48.     public String toString() {
  49.         return "Meal " + orderNum;
  50.     }
  51. }
  52.  
  53. class WaitPerson implements Runnable {
  54.  
  55.     Lock lock = new ReentrantLock();
  56.     Condition condition = lock.newCondition();
  57.  
  58.     private Restaurant restaurant;
  59.  
  60.     public WaitPerson(Restaurant r) {
  61.         restaurant = r;
  62.     }
  63.  
  64.     public void run() {
  65.         try {
  66.             while (!Thread.interrupted()) {
  67.                 lock.lock();
  68.                 try {
  69.                     while (restaurant.meal == null) {
  70.                         condition.await(); //... for the chef to produce a meal
  71.                     }
  72.                 } finally {
  73.                     lock.unlock();
  74.                 }
  75.                 System.out.println("Waitperson got " + restaurant.meal);
  76.  
  77.                 restaurant.chef.lock.lock();
  78.                 try {
  79.                     restaurant.meal = null;
  80.                     System.out.println("Meal taken by the waitperson!");
  81.                     restaurant.chef.condition.signalAll(); //Ready for another
  82.                 } finally {
  83.                     restaurant.chef.lock.unlock();
  84.                 }
  85.  
  86.                 try {
  87.                     restaurant.boy.lock.lock();
  88.                     System.out.println("Notifying BusBoy to cleanup...");
  89.                     restaurant.boy.condition.signalAll();
  90.                 } finally {
  91.                     restaurant.boy.lock.unlock();
  92.                 }
  93.             }
  94.         } catch (InterruptedException e) {
  95.             System.out.println("WaitPerson interrupted!");
  96.         }
  97.     }
  98. }
  99.  
  100. class Chef implements Runnable {
  101.  
  102.     Lock lock = new ReentrantLock();
  103.     Condition condition = lock.newCondition();
  104.     private Restaurant restaurant;
  105.     private int count = 0;
  106.  
  107.     public Chef(Restaurant r) {
  108.         restaurant = r;
  109.     }
  110.  
  111.     public void run() {
  112.         try {
  113.             while (!Thread.interrupted()) {
  114.                 lock.lock();
  115.                 try {
  116.  
  117.                     while (restaurant.meal != null) {
  118.                         condition.await();//... for the meal to be taken
  119.                     }
  120.                 } finally {
  121.                     lock.unlock();
  122.                 }
  123.  
  124.                 if (++count == 10) {
  125.                     System.out.println("Out of food, closing");
  126.                     restaurant.exec.shutdownNow();
  127.                     return;
  128.                 }
  129.                 System.out.println("Order up!");
  130.                 restaurant.waitPerson.lock.lock();
  131.                 try {
  132.                     restaurant.meal = new Meal(count);
  133.                     restaurant.waitPerson.condition.signalAll();
  134.                 } finally {
  135.                     restaurant.waitPerson.lock.unlock();
  136.                 }
  137.                 TimeUnit.MILLISECONDS.sleep(100);
  138.             }
  139.         } catch (InterruptedException e) {
  140.             System.out.println("Chef interrupted!");
  141.         }
  142.     }
  143. }
  144.  
  145. public class Restaurant {
  146.  
  147.     Meal meal;
  148.     ExecutorService exec = Executors.newCachedThreadPool();
  149.     WaitPerson waitPerson = new WaitPerson(this);
  150.     Chef chef = new Chef(this);
  151.     BusBoy boy = new BusBoy(this);
  152.  
  153.     public Restaurant() {
  154.         exec.execute(chef);
  155.         exec.execute(waitPerson);
  156.         exec.execute(boy);
  157.     }
  158.  
  159.     public static void main(String[] args) {
  160.         new Restaurant();
  161.     }
  162.  
  163. }
Advertisement
Add Comment
Please, Sign In to add comment