Java Program to Set and Print Thread Name

You are currently viewing Java Program to Set and Print Thread Name

Java program to set and print thread name

// Java program to set and print thread name

public class Main {
  public static void main(String[] args) {
    String threadName;
    Thread thrd1 = new Thread("Thread1");
    Thread thrd2 = new Thread("Thread2");

    thrd1.start();
    thrd2.start();

    threadName = thrd1.getName();
    System.out.println("Thread Name: " + threadName);

    threadName = thrd2.getName();
    System.out.println("Thread Name: " + threadName);
  }
}

Output

Thread Name: Thread1
Thread Name: Thread2

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply