title: How to correctly get thread name in Java?
body: <p>I have this class for creating a thread in Java</p>
  
  <pre><code>package org.vdzundza.forms;
  
  import java.awt.Graphics;
  import java.awt.Graphics2D;
  
  public class DrawThread extends Thread {
      private static final int THREAD_SLEEP = 500;
      public CustomShape shape;
      private Graphics g;
  
      public DrawThread(CustomShape shape, Graphics g) {
          this.shape = shape;
          this.g = g;
      }
  
      @Override
      public void run() {
          while (true) {
              try {
                  Thread.sleep(THREAD_SLEEP);
                  Graphics2D g2d = (Graphics2D) g;
                  g2d.setColor(this.shape.getColor());
                  g2d.fill(this.shape.getShape());
                  System.out.println(String.format("execute thread: %s %s",
                          Thread.currentThread().getName(), this.getName()));
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
      }
  }
  </code></pre>
  
  <p>The console displays the following text as an output</p>
  
  <pre><code>execute thread: red rectangle Thread-2
  execute thread: yellow ellipse Thread-3
  </code></pre>
  
  <p>My code which creates the new thread:</p>
  
  <pre><code>customShapes[0] = new CustomShape(
  new Rectangle2D.Float(10, 10, 50, 50), Color.RED,
      "red rectangle");
  customShapes[1] = new CustomShape(new Ellipse2D.Float(70, 70, 50, 50),
  Color.YELLOW, "yellow ellipse");
  for (CustomShape cshape: customShapes) {
      Thread t = new Thread(new DrawThread(cshape, this.getGraphics()),
      cshape.getName());
      threads.add(t);
      t.start();
  }
  </code></pre>
  
  <p>My Question is: Why does <code>Thread.currentThread().getName()</code> return the correct thread name whilst <code>this.getName()</code> returns another?</p>