title: Java - does not loop in multithreaded program
body: <p>I am still a java newbie and trying to play around learning threads. My question is that it does not loop 5 times. It runs one time and exits. I am using a.class to lock on the class object, such that both the threads are locking on the same object monitor.</p>
  
  <pre><code>class a implements Runnable {
    Thread thr;
    int count;
    String time;
  
    a(String s) {
      thr = new Thread(this, s);
      thr.start();
    }
  
    public void run() {
      count++;
  
      if (Thread.currentThread().getName().compareTo("one") == 0) {
        synchronized (a.class) {
  
          try {
            for (int i = 0; i &lt; 5; i++) {
              System.out.println("Now running thread " + Thread.currentThread().getName() + " with count " + count);
  
              time = "Tick";
              System.out.println(time);
              notify();
  
              while (time == "Tock") {
                wait();
              }
            }
          } catch (Exception e) {
          }
  
        }
      } else if (Thread.currentThread().getName().compareTo("two") == 0) {
        synchronized (a.class) {
          try {
            for (int j = 0; j &lt; 5; j++) {
              System.out.println("Now running thread " + Thread.currentThread().getName() + " with count " + count);
  
              time = "Tock";
              System.out.println(time);
                notify();
  
              while (time == "Tick") {
                wait();
              }
            }
          } catch (Exception e) {
          }
        }
      }
    }
  }
  
  public class b {
    public static void main(String args[]) {
  
      a obj1 = new a("one");
      a obj2 = new a("two");
    }
  }
  </code></pre>