How to find the name of the parent thread?

前端 未结 4 2027
天涯浪人
天涯浪人 2020-12-04 01:55

I know we can have \'parents\' and \'children\' when we are talking about processes. But is it possible to get parent Thread name?

I did my research, b

4条回答
  •  無奈伤痛
    2020-12-04 02:10

    In the accepted answer Gray mentions that the thread locals are possibly inherited from a thread that starts another thread (i.e. parent to child; note that the terms "parent" and "child" don't have any more special technical meaning here).

    Based on that idea, it seems that there is a way to figure out the parent thread using InheritableThreadLocal: whatever value will be set in the parent (like name) will be available in the child automatically.


    Moreover, if we don't control the child threads (e.g. we run a third-party component in our thread, and it spawns a few threads, which we wish to keep track of), it might be possible to use this mechanism also. Reflection can let us see other threads' thread locals.

    This might allow us to e.g. take a snapshot of all running threads, and figure out which ones were started by our thread, and children of those threads, etc. — all their descendants. Should work well for monitoring purposes. Not sure if it will be any good for anything else.

提交回复
热议问题