title: Print 1 to 100 using 10 threads in java
body: <p>I'm new to muti-threading and I got a question to print 1 to 100 using 10 threads in Java with below constrain.</p>
  
  <ol>
  <li><p>Thread <code>t1</code> should print:</p>
  
  <blockquote>
    <p>1, 11, 21, 31, ... 91</p>
  </blockquote>
  
  <p><code>t2</code> should print:</p>
  
  <blockquote>
    <p>2, 12, 22, 32, ... 92</p>
  </blockquote>
  
  <p>likewise</p>
  
  <p><code>t10</code> should print:</p>
  
  <blockquote>
    <p>10, 20, 30, ... 100</p>
  </blockquote></li>
  <li><p>The final output should be</p>
  
  <blockquote>
    <p>1 2 3 .. 100</p>
  </blockquote></li>
  </ol>
  
  <p>I have tried it, but it is throwing the following exception in all 10 threads:</p>
  
  <pre><code>java.lang.IllegalMonitorStateException
      at java.lang.Object.wait(Native Method)
      at java.lang.Object.wait(Object.java:485)
      at thread.run(MyThread.java:58)
      at java.lang.Thread.run(Unknown Source) 
  </code></pre>
  
  <p>Please let me know how I can solve this problem.</p>
  
  <pre><code>public class MyThread {
      /**
       * @param args
       */
      public static void main(String[] args) {
          thread.setSequence();
          for(int i = 1; i &lt;= 10; i++) {
              Thread t = new Thread(new thread(i));
              t.setName(i + "");
              t.start();
          }
      }
  }
  
  class thread implements Runnable {
      private static HashMap&lt; String, String&gt; sequence = new HashMap&lt;String, String&gt;();
  
      public static final Object lock = new Object();
      public static String turn = "1"; 
      private int startValue = 0;
      private AtomicInteger counter = new AtomicInteger(1);
  
      public thread(int startValue){
          this.startValue = startValue;
      }
  
      @Override
      public void run() {
          while (!counter.equals(10)){
              synchronized (lock) {
                  if(Thread.currentThread().getName().equals(turn)){  
                      System.out.print(startValue + " ");
                      startValue += 10;
                      counter.incrementAndGet();
                      turn = getNextTurn(turn);
                      try {
                          this.wait();
                      } catch (InterruptedException e) {
                          e.printStackTrace();
                      }
                  }
                  else{                       
                      try {
                          this.wait();
                      } catch (InterruptedException e) {
                          e.printStackTrace();
                      }
                  }
                  this.notifyAll();
              }
          }
      }
  
      public static void setSequence(){
          for (int i = 1; i &lt;= 10; i++)
              if (i == 10)
                  sequence.put(i + "", 1 + "");
              else
                  sequence.put(i + "", (i + 1) + "");
      }
  
      public static String getNextTurn(String currentTurn){
          return sequence.get(currentTurn);
      }
  }
  </code></pre>