Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Blancanieves implements Runnable {
- Cocina c;
- public Blancanieves(Cocina c){
- this.c=c;
- }
- @Override
- public void run() {
- while (true){
- try {
- c.feed();
- }catch (InterruptedException e) {}
- }
- }
- }
- public class Enano implements Runnable {
- String name;
- Cocina c;
- public Enano(Cocina c,String name){
- this.c=c;
- this.name=name;
- }
- @Override
- public void run() {
- try {
- Thread.sleep((int)Math.random()*500);
- c.addWorker(this);
- } catch (InterruptedException e) {}
- }
- public String getName(){
- return name;
- }
- }
- public class Cocina {
- private static int maxSillas=4;
- private int sillas=maxSillas;
- public synchronized void addWorker(Enano e) throws InterruptedException {
- while (sillas==0) wait();
- sillas --;
- System.out.println(e.getName()+" "+sillas);
- notifyAll();
- }
- public synchronized void feed() throws InterruptedException{
- while (sillas==4) wait();
- sillas++;
- notifyAll();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement