java - WaterTank ProducerConsumer Pattern -


this exercise have 1 producer , n consumer ( fill , remove water watertank ) implemented in shown code.

now text of exercise says: filling additional tank: this way u can sure water won't put in , taken away @ same time. or water can taken away if there putoperation.

i don't understand it. have synchronized on private final object first part ( won't put , taken away @ same time ) doesn't happen anyway. there no need additional watertank way.

the second part taken @ same time put quite don't understand. need semaphores or reeantrantlock or anything?

i know have put watertank object existing watertank far understand.

thy :)

public class watertank {    private final int capacity;   private int water;   private final object synchobj = new object();    public watertank(int capacit, int wate) {     this.capacity = capacit;     this.water = wate;   }    public void fillwater(int wata) {      synchronized (synchobj) {       if (water + wata > capacity) {         system.out.println("cannot fill water!");        } else {         water = water + wata;         system.out.println("fill::::capacity: " + capacity + " , water: " + water);       }     }   }    public void removewater(int wata) {     synchronized (synchobj) {       if (water - wata < 0) {         system.out.println("cannot take water!");       } else {         water = water - wata;         system.out.println("remove:::capacity: " + capacity + " , water: " + water);       }     }   } }  public class producer implements runnable {   private final watertank t;    public producer(watertank t) {     this.t = t;   }    @override   public void run() {     while (true) {       t.fillwater(10);     }   } }  public class consumer implements runnable {    private final watertank t;    public consumer(watertank t) {     this.t = t;   }    @override   public void run() {     while (true) {       t.removewater(10);     }   } }  import java.util.concurrent.executorservice; import java.util.concurrent.executors;  public class test {    public static void main(string[] args) {      watertank f = new watertank(100, 0);      thread pro = new thread(new producer(f));     pro.start();     executorservice exec = executors.newcachedthreadpool();      (int = 0; < 5; i++) {       exec.execute(new consumer(f));     }   } } 

i tried again using wait/notify wont work , don't understand why. next try locks.

somebody see why code doesnt run? compiler throws illegalmonotirstateexception.

public class watertank {  private final int capacity; private int water; private final object synchobj = new object();   public watertank ( int capacit, int wate ){     this.capacity = capacit;     this.water = wate; }   public void fillwater(int wata){     synchronized(synchobj){         while(water+wata>capacity){             try {                 wait();             } catch (interruptedexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }         }           water = water + wata;         system.out.println("fill:::::capacity: " + capacity + " water: " + water);         notifyall();     }  }  public void removewater(int wata){     synchronized(synchobj){         while(water-wata<0){             try{                 wait();             }             catch (interruptedexception e){                 e.printstacktrace();             }          }         water = water - wata;         system.out.println("remo:::::capacity: " + capacity + " water: " + water);         notifyall();     } }  } 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -