Java Data Type Tutorial - Java Thread.getName()








Syntax

Thread.getName() has the following syntax.

public final String getName()

Example

In the following code shows how to use Thread.getName() method.

//from w  w w  .  j  a va 2  s .c om
class ThreadDemo extends Thread {
  public void run() {
    // returns the name of this Thread.
    System.out.println("Name = " + getName());
  }
}

public class Main {
  public static void main(String args[]) {
    Thread t = new Thread(new ThreadDemo(), "java2s.com thread");
    // set thread priority
    t.setPriority(1);
    // print thread created
    System.out.println("thread  = " + t);
    // this will call run() function
    t.start();
  }

}

The code above generates the following result.