java - Printing values from different threads using wait and notify -



i have 3 threads threada, threadb , threadc printing values a, b , c respectively in loop.
i want output a,b,c , again a, b , c till loops executing in threads.i want write sample program using wait , notify. below code printing desired output seeing "a" in output, not able figure out case.

public class threadorder {      public static void main(string[] args) {         object lockab = new object();         object lockbc = new object();         object lockca = new object();          thread threada = new thread(new threadorder().new threada(lockab, lockca));         thread threadb = new thread(new threadorder().new threadb(lockab, lockbc));         thread threadc = new thread(new threadorder().new threadc(lockbc, lockca));          threada.start();         threadb.start();         threadc.start();     }      class threada implements runnable {         object lockab;         object lockca;          public threada(object lockab, object lockca) {             this.lockab = lockab;             this.lockca = lockca;         }          @override         public void run() {             for(int i=0; i<3; i++) {                 if(i!=0) {                     try {                         synchronized (lockca) {                             lockca.wait();                         }                     } catch (interruptedexception e) {                         e.printstacktrace();                     }                 }                 system.out.println("a");                 synchronized (lockab) {                     lockab.notify();                 }             }         }      }      class threadb implements runnable {         object lockab;         object lockbc;          public threadb(object lockab, object lockbc) {             this.lockab = lockab;             this.lockbc = lockbc;         }          @override         public void run() {             for(int i=0; i<3; i++) {                 try {                     synchronized (lockab) {                         lockab.wait();                     }                 } catch (interruptedexception e) {                     e.printstacktrace();                 }                 system.out.println("b");                 synchronized (lockbc) {                     lockbc.notify();                 }             }         }      }      class threadc implements runnable {         object lockbc;         object lockca;          public threadc(object lockbc, object lockca) {             this.lockbc = lockbc;             this.lockca = lockca;         }          @override         public void run() {             for(int i=0; i<3; i++) {                 try {                     synchronized (lockbc) {                         lockbc.wait();                     }                 } catch (interruptedexception e) {                     e.printstacktrace();                 }                 system.out.println("c");                 synchronized (lockca) {                     lockca.notify();                 }             }         }      } } 

consider creating ring of threads connected 1 blocking queues. can pass token around ring. each thread waits receive token, prints output, passes token on next thread in ring, , goes waiting.


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -