Advertisement
Guest User

Untitled

a guest
Oct 20th, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. public class Blancanieves implements Runnable {
  2.    
  3.     Cocina c;
  4.    
  5.     public Blancanieves(Cocina c){
  6.         this.c=c;
  7.     }
  8.  
  9.     @Override
  10.     public void run() {
  11.         while (true){
  12.             try {
  13.                 c.feed();
  14.             }catch (InterruptedException e) {}     
  15.         }
  16.     }
  17.  
  18. }
  19.  
  20. public class Enano implements Runnable {
  21.    
  22.     String name;
  23.     Cocina c;
  24.    
  25.     public Enano(Cocina c,String name){
  26.         this.c=c;
  27.         this.name=name;
  28.     }
  29.  
  30.     @Override
  31.     public void run() {
  32.         try {
  33.             Thread.sleep((int)Math.random()*500);
  34.             c.addWorker(this);
  35.         } catch (InterruptedException e) {}    
  36.     }
  37.     public String getName(){
  38.         return name;
  39.     }
  40. }
  41.  
  42. public class Cocina {
  43.    
  44.     private static int maxSillas=4;
  45.    
  46.     private int sillas=maxSillas;
  47.    
  48.     public synchronized void addWorker(Enano e) throws InterruptedException {
  49.         while (sillas==0) wait();
  50.         sillas --;
  51.         System.out.println(e.getName()+" "+sillas);
  52.         notifyAll();
  53.     }
  54.    
  55.     public synchronized void feed() throws InterruptedException{
  56.         while (sillas==4) wait();
  57.         sillas++;
  58.         notifyAll();
  59.     }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement