Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renaming Threads in Java

I am working on a project that is slowly getting larger and larger and the number of active threads used by many different processes is increasing. Lately, I have been taking a closer look at the running threads in the debugger and I have noticed that a lot of my third party libraries have given very poor names to their threads - Timer-0, qtp0, etc. I want other devs unfamiliar with the poorly named threads to know instantly what is running.

Rather than write patches for the libs we are using, does anyone know how to rename the running threads? Or if this is even a good idea? Any suggestions would be appreciated.

like image 539
Elijah Avatar asked Jan 21 '09 22:01

Elijah


People also ask

Can we change name of thread?

Yes, you can change the name of a thread with the setName() method.

What is thread currentThread () getName ()?

currentThread(). getName() gets you the name of the currently-running thread, but getName() resolves to st. getName() , and st is always your first thread instance.

Why naming a thread is useful?

It's helpful to name threads so one can sort out which threads are doing what for diagnostic and debugging purposes.

Can two threads have same name?

Every thread has a name for identification purposes. More than one thread may have the same name. If a name is not specified when a thread is created, a new name is generated for it.


1 Answers

Change Java Thread Name

Thread.currentThread().setName("MyThread");
like image 70
KunMing Xie Avatar answered Sep 18 '22 15:09

KunMing Xie