Get Thread By Name

前端 未结 4 1387
隐瞒了意图╮
隐瞒了意图╮ 2020-12-08 05:12

I have a multithreaded application and I assign a unique name to each thread through setName() property. Now, I want functionality to get access to the threads

4条回答
  •  失恋的感觉
    2020-12-08 05:31

    You can find all active threads using ThreadGroup:

    • Get your current thread's group
    • Work your way up the threadgroup hierarchy by calling ThreadGroup.getParent() until you find a group with a null parent.
    • Call ThreadGroup.enumerate() to find all threads on the system.

    The value of doing this completely escapes me ... what will you possibly do with a named thread? Unless you're subclassing Thread when you should be implementing Runnable (which is sloppy programming to start with).

提交回复
热议问题