views:

495

answers:

7

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.

+1  A: 

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.

Kevin
+3  A: 

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.

erickson
The only purpose would be for human readability.
Elijah
A: 

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.

Peter Lawrey
+2  A: 

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

Eddie
+1  A: 

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. :)

Dustin
A: 

This begs the question of best practices around naming conventions for threads. I haven't seen any community guidance in this regard.

jm04469
A: 

I have a question in .Net. Does anyone know how to rename current thread's name in .Net. Name is write-once property of thread class. Any idea would be appreciated! Thanks

Hoa Nguyen
Hoa, I would ask your question in a new question - not in an answer section.
Elijah