title: Putting runnable to sleep
body: <p>I have a problem with Java. I would like to write a program where there is Class Main which has ArrayList of Threads of some class (Class Task) which just writes a letter and the number. Object Main just wakes one Thread from ArrayList and let it to do something while the same object(Main) sleeps another one.</p>
  
  <p>It works ok:
  0A, 0B, 0C, 1B, 1C, 1A, 2B, 2A, 2C, 3B, 3C, 3A, 4B, 4C, 4A, 5B, 5A, 5C,</p>
  
  <p>but only if i Comment the: 
     e.printStackTrace() e is Exception
  then
  I get lot of 
      java.lang.IllegalMonitorStateException
      at java.lang.Object.notify(Native Method)
      at Main.run(Main.java:22)</p>
  
  <p>So the notify works wrong, how I should correctly wake it, please tell me, show, correct. PLease</p>
  
  <pre><code>import java.util.ArrayList;
  
  import java.util.ArrayList;
  
  public class Main extends Thread {
  ArrayList&lt;Thread&gt; threads;
  
  public Main() {
      super();
      threads = new ArrayList&lt;Thread&gt;();
  }
  
  public void run() {
      for (int i = 0; i &lt; 3; i++) {
          threads.add(new Thread(new Task(i + 65)));
      }
      long cT = System.currentTimeMillis();
      for (int i = 0; i &lt; threads.size(); i++) {
          threads.get(i).start();
      }
      while (System.currentTimeMillis() - cT &lt; 10000) {
          for (int i = 0; i &lt; threads.size(); i++) {
              try {
                  threads.get(i).notify();
                  // HOW TO WAKE THREAD FROM threads ArrayList
                  Thread.sleep(1000);
                  // how to put to bed the same thread ?
                  threads.get(i).wait();
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      }
  
  }
  
  public static void main(String[] args) {
       new Main().start();
      //new Thread(new Task(65)).start();
  
  }
  
  }
  </code></pre>
  
  <p>H</p>
  
  <pre><code>public class Task implements Runnable {
  int nr;
  char character;
  
  public Task(int literaASCII) {
      this.nr = 0;
      character = (char) (literaASCII);
  }
  
  @Override
  public void run() {
      while (true) {
          try {
              System.out.print(nr + "" + character + ", ");
              nr++;
              int r = (int) ((Math.random() * 500) + 500); // &lt;500ms,1000ms)
              Thread.sleep(r);
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
  
  
  
  public static void main(String[] args) {
      // TODO Auto-generated method stub
  
  }
  }
  </code></pre>