Reputation: 13604
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.
Upvotes: 16
Views: 13416
Reputation: 1667
Change Java Thread Name
Thread.currentThread().setName("MyThread");
Upvotes: 10
Reputation: 30419
You can use Thread.enumerate()
or Thread.getAllStackTraces()
to get a list of the running Threads. You can then call Thread.setName()
on each.
I can't tell you whether its a good idea or not but I'd probably lean towards "no". The more code you write, the more code you have to maintain.
Upvotes: 3
Reputation: 533492
setName()
can be used to change the name of a thread but you have to have some way of determining what the thread should be called.
getAllStackTraces()
would allow the program to example the stack trace and use the class/methods/file name names as a hint as to what to call the thread.
Upvotes: 1
Reputation: 6664
This begs the question of best practices around naming conventions for threads. I haven't seen any community guidance in this regard.
Upvotes: 1
Reputation: 90980
I name all threads I create. I've had too many apps leaking threads, or having threads otherwise spin out of control.
The more you treat java as an operating system, the easier it gets to manage. :)
Upvotes: 5
Reputation: 54421
If the problem is in open source libraries, then the best solution is to supply patches to those products for better thread naming. Barring a security manager running, you can rename any thread by enumerating threads (as others have mentioned) and applying an arbitrary name to each, but this is a fragile way of naming threads that are not your own. If the 3rd party library changes their naming, you'll have to change your code.
I agree that 3rd party libraries with poorly-named threads make more difficult understanding what is going on in your application. But renaming threads from a 3rd party library is risky. What happens if their next release changes the way they name threads? And how do you handle a 3rd party library that makes no attempt at all to name threads?
EDIT: Add back text that somehow disappeared from the end
Upvotes: 6
Reputation: 269657
Yes, you can change the name of a thread with the setName() method.
As for whether it's a good idea, there's no telling for sure, but relying on the thread name for anything other than a human-readable description would be a very poor decision, and most libraries will not do this. Thus, it's unlikely to break anything if you rename them.
Upvotes: 11