title: Why does java thread wait() work only with time limit in here?
body: <p>I am trying to get familiar with Java threads for the SCJP and I had a question.</p>
  
  <p>In the below-written code i simply created:
   two Runnables with 
  a common data storage (an array) and 
  a synchronized write() method to fill it with data successively leaving a letter as a mark for each Runnable (A and B) in sequence. </p>
  
  <p>I know the code is rough and could be better written but I was seeking the moral of the threads.</p>
  
  <p>So now when I run it, it never terminates and the results stop at:</p>
  
  <p>Still good.
  A0.</p>
  
  <p>But when I change wait() to wait(100) it works just fine counting from 0 to 9 and it terminates normally. Could someone explain the reason behind that for me please?</p>
  
  <p>Thank you.</p>
  
  <pre><code>public class ArrayThreads {
  
  Object[] array = new Object[10];
  boolean isA = true;
  
      int position = 0;
  
      int getIndex(){
          return position;
      }
  
  
  
  class ThreadA implements Runnable{
  
              synchronized void write(String value){
                      while(!isA){
              try {
                  wait();
              } catch (InterruptedException ex) {
                  System.out.println("An error in" + value);
                  ex.printStackTrace();
              }
          }
          array[position] = value + position;
          System.out.println(array[position]);
          position++;
          isA = !isA;
          notify();
      }
  
      public void run() {
          while(getIndex()&lt;array.length){
              if (getIndex()==9) return;
              else
          write("A");}
      }
  }
  
      class ThreadB implements Runnable{
  
                  synchronized void write(String value){
                      while(isA){
              try {
                  wait();
              } catch (InterruptedException ex) {
                  System.out.println("An error in" + value);
                  ex.printStackTrace();
              }
          }
          array[position] = value + position;
          System.out.println(array[position]);
          position++;
          isA = !isA;
          notify();
      }
  
      public void run() {
          while(getIndex()&lt;array.length){
              if (getIndex()==9) return;
              else
          write("B");}
      }
  }
  
      public static void main(String[] args){
          ArrayThreads threads = new ArrayThreads();
          Thread threadA = new Thread(threads.new ThreadA());
          Thread threadB = new Thread(threads.new ThreadB());
          System.out.println("Still good");
  
          threadB.start();
          threadA.start();
      }
  </code></pre>
  
  <p>}</p>