java - Thread Synchronization. Printing the values in right way -


i not able find solution of problem. messing @ point, not sure. kindly give suggestions overcome this.

problem:

have 3 arrays, each array assigned thread, output should in sequence...

t1 ={1,4,7} t2 ={2,5,8} t3 ={3,6,9}

expected output

out = {1,2,3,4,5,6,7,8,9}

wht tried:

public class worker extends thread {     worker next;     int[] val;     object lock = new object();      worker(int[] val) {          this.val = val;     }      public void setnext(worker next) {         this.next = next;     }      @override     public void run() {         (int = 0; < val.length; i++) {             synchronized (this) {                 synchronized (next) {                      system.out.println(val[i]);                     next.notify();                  }                  synchronized (this) {                     try {                          this.wait();                     } catch (interruptedexception e) {                         // todo auto-generated catch block                         e.printstacktrace();                     }                 }             }             synchronized(next){                 next.notify();             }         }     } } 

and test class

public class testworker {     public static void main(string[] args) throws exception{         worker worker1 = new worker(new int[]{1,4,7});         worker worker2 = new worker(new int[]{2,5,8});         worker worker3 = new worker(new int[]{3,6,9});          worker1.setnext(worker3);         worker2.setnext(worker1);         worker3.setnext(worker2);          worker1.start();         worker2.start();         worker3.start();      } } 

try version. note wait should used in loop.

public class worker extends thread {     int[] val;     worker next;     boolean ready;     boolean go;      worker(int[] val) {         this.val = val;     }      public void setnext(worker next) {         this.next = next;     }      @override     public void run() {         synchronized (worker.class) {             ready = true;             worker.class.notifyall();             (int = 0; < val.length; i++) {                 try {                     while (!go) {                         worker.class.wait();                     }                     system.out.println(val[i]);                     go = false;                     next.go = true;                     worker.class.notifyall();                 } catch (interruptedexception e) {                 }             }         }     }      public static void main(string[] args) throws exception {         worker worker1 = new worker(new int[] { 1, 4, 7 });         worker worker2 = new worker(new int[] { 2, 5, 8 });         worker worker3 = new worker(new int[] { 3, 6, 9 });          worker1.setnext(worker2);         worker2.setnext(worker3);         worker3.setnext(worker1);          worker1.start();         worker2.start();         worker3.start();          synchronized (worker.class) {             while(!worker1.ready || !worker2.ready ||!worker3.ready ) {                 worker.class.wait();             }             worker1.go = true;             worker.class.notifyall();         }     } } 

output

1 2 3 4 5 6 7 8 9 

Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -