How to set and print thread name in Java

1 Answer

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

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

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

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



/*
run:

Thread Name: Thread1
Thread Name: Thread2

*/

 



answered Jan 29, 2024 by avibootz

Related questions

1 answer 224 views
1 answer 160 views
160 views asked Oct 14, 2016 by avibootz
1 answer 189 views
1 answer 209 views
...