• August 5th, 2011, 12:06 PM
shashank90
could someone help me regarding this following code. It's large but in essence, i'm just running 4 threads
1. to count
2. to check if count is even
3. to check if count is odd
4. to check if count is prime (this one is supposed to run only if 2 & 3 are done with thread 1 for one iteration).

Errors: (it prints the following sequence, and then gets stuck forever at bb.wait)

count
Reaching A
aa.wait
Reaching B
Reaching C
outside a in C
ss in A wait
bb.wait

Code :

```/* Write a thread program that can count from 1 till 100 with the following conditions :- 1. Have three threads running simultaneously: Thread A ,Thread B,Thread C. Thread A will count only the even numbers, Thread B will count only the odd numbers, Thread C will count only prime numbers. 2. When ever the multiples of 10 is reached for Thread A , let it sleep for 2 secs, When ever the multiples of 9 is reached for Thread B , let it sleep for 2 secs. Thread C count should always be less than A or B. Only after thread A / thread B have counted a number, may C check whether it's prime number or not. */   class store { int i;   synchronized void set(int k) { i = k; }   synchronized int get() { return i; }   }   class counts extends Thread { int i; store ss;   counts(store S) { ss = S; }   public void run() { synchronized (ss) { ss.notify(); while( i < 100) { i = ss.get(); i++; ss.set(i); try { System.out.println("count"); ss.wait(); } catch (InterruptedException e){}   } } System.exit(1);   } }   class A extends Thread { int i = 0; boolean Aboo = false; store ss; A aa;   A(){}   A(store S) { ss = S; }   public void run() { System.out.println("Reaching A"); synchronized (ss) { synchronized (this) { notify(); while (i< 100) { ss.notifyAll(); i = ss.get();   if (i%2 == 0) { System.out.println("Counted Even in A \t" + i); Aboo = true; if (i%10 == 0) { try { this.sleep(2000); } catch(InterruptedException e) { System.out.println("A InterruptedException caught"); } } }   else Aboo = false;   try {System.out.println("aa.wait"); wait(); } catch (InterruptedException e) { System.out.println("class B error \n"); }   try {System.out.println("ss in A wait"); ss.wait(); } catch (InterruptedException e) { System.out.println("class A error"); }   } } } System.exit(1); }   boolean bool() { return Aboo; }   int Ai() { return i; }   }   class B extends Thread { int i = 0; boolean Bboo = false; store ss; B bb;   B(store S) { ss = S; }   public void run() {System.out.println("Reaching B"); synchronized (ss) { synchronized (this) { notify(); while (i< 100) { ss.notifyAll(); i = ss.get();   if (i%2 == 0) { System.out.println("Counted Even in B \t" + i); Bboo = true;   if (i%9 == 0) { try { this.sleep(2000); } catch(InterruptedException e) { System.out.println("B InterruptedException caught"); } } }   else Bboo = false;   try {System.out.println("bb.wait"); wait(); } catch (InterruptedException e) { System.out.println("class B error \n"); }   try {System.out.println("ss in B wait"); ss.wait(); } catch (InterruptedException e) { System.out.println("class B error"); }   } } } System.exit(1); }   boolean bool() { return Bboo; }   int Bi() { return i; } }   class C extends Thread { A a; B b; int i = 0; store ss;   C(A aa, B bb) { a = aa; b = bb; }   public void Check(int k) { int j = 1;   while (j < k) { if (k%j == 0) { break; } j++; }   if (j == k) { System.out.println("counted a Prime Number\t" +i); } }   public synchronized void run() { System.out.println("Reaching C");   synchronized (a) { a.notify(); if (a.bool()) { i = a.Ai(); while (i < 100) { Check(i); try { System.out.println("a and s in C"); a.wait(); } catch (InterruptedException e) { System.out.println("class C error \n"); } } } else try{System.out.println(" outside a in C"); a.wait();} catch(Exception ee){} }   synchronized (b) { b.notify(); if (b.bool()) { i = b.Bi(); while (i < 100) { Check(i); try { System.out.println("a and s in C"); b.wait(); } catch (InterruptedException e) { System.out.println("class C error \n"); } } } else try{System.out.println(" outside b in C"); b.wait();} catch(Exception ee){} } System.exit(1); } }   public class OddEvenPrime { public static void main(String[] arg) { store ss = new store(); ss.set(0);   counts countin = new counts(ss); Thread thcount = new Thread(countin); thcount.setName("thcount");   A aa = new A(ss); Thread Aeven = new Thread(aa); Aeven.setName("Aeven");   B bb = new B(ss); Thread Bodd = new Thread(bb); Bodd.setName("Bodd");   C cc = new C(aa, bb); Thread Cprime = new Thread(cc); Cprime.setName("Cprime");   thcount.start(); Aeven.start(); Bodd.start(); Cprime.start();   } }```
• August 5th, 2011, 12:15 PM
Norm
Please edit your code and wrap it with code tags. See: BB Code List - Java Programming Forums
Or use the Go Advanced button and use the #icon

Please post the full text of the error message here.
• August 8th, 2011, 12:42 PM
shashank90