Renaming Threads in Java

ⅰ亾dé卋堺 提交于 2019-12-08 15:26:54

问题


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:


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




回答2:


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.




回答3:


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




回答4:


Change Java Thread Name

Thread.currentThread().setName("MyThread");



回答5:


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.




回答6:


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




回答7:


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.



来源:https://stackoverflow.com/questions/467224/renaming-threads-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!