Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly get thread name in Java?

I have this class for creating a thread in Java

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();             }         }     } } 

The console displays the following text as an output

execute thread: red rectangle Thread-2 execute thread: yellow ellipse Thread-3 

My code which creates the new thread:

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(); } 

My Question is: Why does Thread.currentThread().getName() return the correct thread name whilst this.getName() returns another?

like image 596
BILL Avatar asked Jan 06 '13 22:01

BILL


People also ask

Which method is used to get current thread name in Java?

The method currentThread() of the Thread class can be used to obtain the current thread. This method requires no parameters.

Which method is used to set name of a thread?

The setName() method of thread class is used to change the name of the thread.


1 Answers

Why Thread.currentThread().getName() return correct thread name whilst this.getName() return other?

Your DrawThread class extends Thread but then you start it by calling:

new Thread(new DrawThread(...)); 

This is not correct. It means that the actual thread that is created is not the same Thread from DrawThread. DrawThread should be implementing Runnable and not extending thread. It should be:

public class DrawThread implements Runnable { 

Your code works because Thread is also a Runnable. Because there are two thread objects, when you call this.getName() on the DrawThread object that is not the thread that is actually running so its name is not properly set. Only the wrapping thread's name is set. Inside of the DrawThread code, you should call Thread.currentThread().getName() to get the true name of the running thread.

Lastly, your class should probably be DrawRunnable if it implements Runnable. :-)

like image 115
Gray Avatar answered Sep 22 '22 03:09

Gray