title: Create multiple threads by extending Thread class that share same object in Java
body: <p>I was going through basics of multithreading and was writing a program to understand the difference between using the 2 approaches of creating threads.</p>
  <p>I have read that using Runnable allows multiple threads to share same object and wanted to try similar thing while extending Thread. So after creating new object of Demo2 I passed the reference to Thread constructor (similar to what we do in Runnable).</p>
  <p>I achieved what I was trying to as objT1, tT1, tT2 incremented value of sum to 3. But while printing out the current thread's name, it prints only <code>demo2.1</code>. I thought that the thread names that will be printed would be demo2.1, t1, t2 since I passed these names in constructor.</p>
  <pre><code>    class Main {
      public static void main(String args[]) {
          Demo1 objR1 = new Demo1();
          Demo2 objT1 = new Demo2(&quot;demo2.1&quot;);
  
          Thread tT1 = new Thread(objT1,&quot;t1&quot;);
          Thread tT2 = new Thread(objT1,&quot;t2&quot;);
  
          Thread tR1 = new Thread(objR1,&quot;tR1&quot;);
          Thread tR2 = new Thread(objR1,&quot;tR2&quot;);
      
          objT1.start();
          tT1.start();
          tT2.start();
  
          tR1.start();
          tR2.start();
      }
  }
  
  
  class Demo1 implements Runnable {
  
      int sum = 0;
  
      synchronized void calculate() {
          sum = sum +1;   
      }
  
      public void run()
      {
          calculate();    
          System.out.print(Thread.currentThread().getName()); 
          System.out.println(&quot; &quot;+sum);
      }
  }
  
  class Demo2 extends Thread {
  
      int sum = 0;
  
      Demo2(String n) {
          super(n);   
      }
      
      synchronized void calculate() {
          sum = sum +1;       
      }
  
      public void run()
      {
          calculate();        
          System.out.println(this.getName()+&quot; &quot;+sum);
      }
  }
  </code></pre>
  <p>Output:</p>
  <pre class="lang-none prettyprint-override"><code>demo2.1 1
  demo2.1 2
  demo2.1 3
  tR1 1
  tR2 2
  </code></pre>
  <p>So my question is - Does this snippet create 3 threads? If yes, then why aren't there 3 different names for each thread. If no, then what do these statements do.</p>
  <pre><code>Demo2 objT1 = new Demo2(&quot;demo2.1&quot;);
  Thread tT1 = new Thread(objT1,&quot;t1&quot;);
  Thread tT2 = new Thread(objT1,&quot;t2&quot;); 
  </code></pre>
  <p>I know this must be something trivial but I cannot get answers in tutorials.</p>