title: Java-Thread Vs Runnable
body: <p>While reading through the significant difference between Thread and Runnable from <a href="http://manikandanmv.wordpress.com/2010/12/10/thread-vs-runnable/">here</a>, I encountered a difference that is:</p>
  
  <p><strong>When you extends Thread class, each of your thread creates unique object and associate with it.</strong> where as </p>
  
  <p><strong>When you implement Runnable, it shares the same object to multiple threads.</strong>. </p>
  
  <p>There is code give : </p>
  
  <pre><code>class ImplementsRunnable implements Runnable {
  
    private int counter = 0;
  
    public void run() {
      counter++;
      System.out.println("ImplementsRunnable : Counter : " + counter);
    }
  }
  
   class ExtendsThread extends Thread {
  
     private int counter = 0;
  
     public void run() {
       counter++;
       System.out.println("ExtendsThread : Counter : " + counter);
     }
   }
  
   public class ThreadVsRunnable {
  
     public static void main(String args[]) throws Exception {
       //Multiple threads share the same object.
       ImplementsRunnable rc = new ImplementsRunnable();
       Thread t1 = new Thread(rc);
       t1.start();
       Thread.sleep(1000); // Waiting for 1 second before starting next thread
       Thread t2 = new Thread(rc);
       t2.start();
       Thread.sleep(1000); // Waiting for 1 second before starting next thread
       Thread t3 = new Thread(rc);
       t3.start();
  
       //Creating new instance for every thread access.
       ExtendsThread tc1 = new ExtendsThread();
       tc1.start();
       Thread.sleep(1000); // Waiting for 1 second before starting next thread
       ExtendsThread tc2 = new ExtendsThread();
       tc2.start();
       Thread.sleep(1000); // Waiting for 1 second before starting next thread
       ExtendsThread tc3 = new ExtendsThread();
       tc3.start();
     }
   }
  </code></pre>
  
  <p>The output is something like this:</p>
  
  <pre><code>ImplementsRunnable : Counter : 1
  ImplementsRunnable : Counter : 2
  ImplementsRunnable : Counter : 3
  ExtendsThread : Counter : 1
  ExtendsThread : Counter : 1
  ExtendsThread : Counter : 1
  </code></pre>
  
  <p>It proves differences given above. I make a slight modification in code given below:</p>
  
  <pre><code>class ImplementsRunnable implements Runnable {
  
    private int counter = 0;
  
    public void run() {
      counter++;
      System.out.println("ImplementsRunnable : Counter : " + counter);
    }
  }
  
  class ExtendsThread extends Thread {
  
    private int counter = 0;
  
    public void run() {
      counter++;
      System.out.println("ExtendsThread : Counter : " + counter);
    }
  }
  
  public class ThreadVsRunnable {
  
    public static void main(String args[]) throws Exception {
      //Multiple threads share the same object.
      ImplementsRunnable rc = new ImplementsRunnable();
      Thread t1 = new Thread(rc);
      t1.start();
      Thread.sleep(1000); // Waiting for 1 second before starting next thread
      Thread t2 = new Thread(rc);
      t2.start();
      Thread.sleep(1000); // Waiting for 1 second before starting next thread
      Thread t3 = new Thread(rc);
      t3.start();
  
      //Modification done here. Only one object is shered by multiple threads here also.
      ExtendsThread extendsThread = new ExtendsThread();
      Thread thread11 = new Thread(extendsThread);
      thread11.start();
      Thread.sleep(1000);
      Thread thread12 = new Thread(extendsThread);
      thread12.start();
      Thread.sleep(1000);
      Thread thread13 = new Thread(extendsThread);
      thread13.start();
      Thread.sleep(1000);
    }
  }
  </code></pre>
  
  <p>Now the output is :</p>
  
  <pre><code>ImplementsRunnable : Counter : 1
  ImplementsRunnable : Counter : 2
  ImplementsRunnable : Counter : 3
  ExtendsThread : Counter : 1
  ExtendsThread : Counter : 2
  ExtendsThread : Counter : 3
  </code></pre>
  
  <p>I understand the fact that, here same object (extendsThread) is shared by three threads. But I'm confused here that how it is different from implementing Runnable. Here, even if *<em>ExtendsThread *</em> extends Thread, we are still able to share the object of this class to other threads. In my thinking the above difference does not make any sense.</p>
  
  <p>Thanks.</p>