java - Thread synchronizing, using notifyAll() -


this exercise bruce eckel's thinking in java 3rd edition, revision 4.0, chapter 13 (exercise 10). supposed have class acts chef gives out orders , notifies waiters, , 2 waiters wait order ready, , take it. here's have sofar:

class order {     private static int = 0;     private int count = i++;     public order() {         if(count == 10) {             system.out.println("out of food, closing.");             system.exit(0);         }     }     public string tostring() {             return "order " + count;     } } class waitperson extends thread {     public  restaurant restaurant;     private int waitpersonid;     public waitperson(restaurant r, int waitpersonid) {         restaurant = r;         this.waitpersonid = waitpersonid;         start();     }     public void run() {         order currentorder;         while(true) {              while(restaurant.order == null) {                  synchronized(restaurant.waitpersonslist) {                     try {                         system.out.println("waitperson" + this.waitpersonid + " waiting...");                         wait();                         system.out.println("waitperson" + this.waitpersonid + " attempting " + restaurant.order);                     }                     catch(interruptedexception e) {                         throw new runtimeexception(e);                     }                 }             }              currentorder = restaurant.order;             system.out.println("waitperson" + this.waitpersonid + " got " + currentorder);             restaurant.order = null;              try {                 sleep(3000); //waitperson busy 3 seconds             } catch (interruptedexception e) {                 e.printstacktrace();             }              system.out.println("waitperson" + this.waitpersonid + " delivered " + currentorder);          }     } } class chef extends thread {     private restaurant restaurant;     private int chefid;     public chef(restaurant r, int chefid) {         restaurant = r;         this.chefid = chefid;         start();     }     public void run() {         while(true) {             if(restaurant.order == null) {                 restaurant.order = new order();                 system.out.println("chef" + this.chefid + ": order up! ");                 synchronized(restaurant.waitpersonslist) {                     restaurant.waitpersonslist.notifyall();                 }             }             try {                 sleep(100);             }             catch(interruptedexception e) {                 throw new runtimeexception(e);             }         }     } }  class restaurant {     public order order;     public static list<waitperson> waitpersonslist = collections.synchronizedlist(new arraylist<waitperson>()); } public class main {     public static void main(string[] args) {         restaurant r = new restaurant();         waitperson wp1 = new waitperson(r, 1);         waitperson wp2 = new waitperson(r, 1);         restaurant.waitpersonslist.add(wp1);         restaurant.waitpersonslist.add(wp2);         chef chef = new chef(r, 1);     } } 

everything ok until waitperson gets part supposed wait() 'till gets notified. illegalmonitorstateexception means thread waiting on object's monitor without owning specified monitor. still, can't figure out.

your problem you've synchronized on restaurant.waitpersonslist, called wait on waitperson. must synchronized on object call wait, notify or notifyall on

             synchronized(restaurant.waitpersonslist) {                 try {                     system.out.println("waitperson" + this.waitpersonid + " waiting...");                     wait();                     system.out.println("waitperson" + this.waitpersonid + " attempting " + restaurant.order);                 }                 catch(interruptedexception e) {                     throw new runtimeexception(e);                 }             } 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -