Get current thread : Thread Properties « Thread « Java Tutorial






public class Main extends Thread {
  public void run() {
    for (int i = 0; i < 5; i++) {
      printMyName();
    }
  }

  public void printMyName() {
    System.out.println("The Thread name is " + Thread.currentThread().getName());
  }

  public static void main(String[] args) {
    Main ttsn = new Main();
    ttsn.setName("Created One");
    ttsn.start();

    Thread t2 = currentThread();
    t2.setName("Main One");

    for (int i = 0; i < 5; i++) {
      ttsn.printMyName();
    }
  }
}








10.2.Thread Properties
10.2.1.Controlling the main Thread.
10.2.2.Get current thread