Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to set a thread name using the setName() method in Java

1 Answer

0 votes
class MyThread implements Runnable {
    public void run() {
        try {
            System.out.println("Thread ID: " + Thread.currentThread().getId());
        } catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}

public class Program {
    public static void main(String[] args) {
        Thread trd1 = new Thread(new MyThread());
        Thread trd2 = new Thread(new MyThread());
        Thread trd3 = new Thread(new MyThread());

        trd1.setName("Thread_1");
        trd2.setName("Thread_2");
        trd3.setName("Thread_3");

        System.out.println("Thread Name: " + trd1.getName());
        System.out.println("Thread Name: " + trd2.getName());
        System.out.println("Thread Name: " + trd3.getName());

        trd1.start();
        trd2.start();
        trd3.start();
    }
}
 
 
 
 
  
/*
run:
  
Thread Name: Thread_1
Thread Name: Thread_2
Thread Name: Thread_3
Thread ID: 16
Thread ID: 15
Thread ID: 17
  
*/

 



answered Jan 29, 2024 by avibootz

Related questions

1 answer 122 views
122 views asked Jan 29, 2024 by avibootz
1 answer 145 views
145 views asked Oct 14, 2016 by avibootz
1 answer 191 views
1 answer 174 views
1 answer 112 views
...