title: Why the following Java code has different outputs each time?
body: <p>I don't know about threads in Java. I like to know what is happening in this code because each time it runs, it produces a different output:</p>
  
  <pre><code>public class TwoThreadsDemo{
      public static void main(String[] args)
      {
          new SimpleThread("Java Programmer").start();
          new SimpleThread("Java Programmer").start();
      }
  }
  
  class SimpleThread extends Thread{
      public SimpleThread(String str)
      {
        super(str);
      }
  
      public void run()
      {
          for (int i=0;i&lt;10;i++)
          {
  
             System.out.println(i + " " + getName());  
  
              try
              {
                  sleep((long)(Math.random()*1000));
              }
              catch(InterruptedException e)
              {
  
              }
  
          }
          System.out.println("Done!" + getName());
      } 
  
  }
  </code></pre>