title: What is the real time benefits of creating Thread in Java?
body: <pre><code>class ThreadDemo extends Thread
  {  
      public void run()
      {   
          for(int i =0; i&lt;5;i++)
          {
              System.out.println(i);
          }
      }  
  
  }
  class ThreadApp
  {
      public static void main(String args[])
      {  
          ThreadDemo thread1 =  new ThreadDemo();  
          thread1.start();  
          ThreadDemo thread2 =  new ThreadDemo();  
          thread2.start();  
          ThreadDemo thread3 =  new ThreadDemo();  
          thread3.start();  
      }  
  }
  </code></pre>
  <p>Output:</p>
  <pre><code>0
  2
  3
  1
  4
  
  1
  2
  4
  3
  0
  
  0
  1
  2
  3
  4
  </code></pre>
  <p>By default, Java applications are single thread application. We are going for the concept called multithreading to share the work. Means, instead of doing the work with one thread (Main thread) if we create the thread, then it will simplifies the work. I understood this thing theoretically. My doubt arises when I start coding.
  In the above program I have created 3 threads. If 3 threads are working on the same logic (iteration and printing the values by using for loop) why it is giving 3 separate output instead of giving one set of values from 0 to 4?</p>