How to get the family tree of all the threads?

David Holmes David.Holmes at Sun.COM
Thu Oct 29 18:54:29 PDT 2009


Tony,

Just a note that there will be two cases where there is no currentThread:

a) when the main thread is created
b) when the main thread detaches (after executing main) and reattaches as 
the DestroyJavaVM thread.

but this shouldn't affect your parent check in run()

David

Tony Guan wrote:
> Hi David &  Alan & Christian,
> 
> Thank you all for the prompt reply! I am currently using such an
> approach as the following, could you please take a look to see if
> something might be wrong?
> 
> 1) added a Thread* field for the JavaThread:
>   Thread*  _parent; //to memorize the thread which creates this thread.
> 2) get the current thread running when a new JavaThread is created,
> this current thread should be the parent of the to-be created thread,
> in JavaThread::JavaThread(ThreadFunction entry_point, size_t stack_sz)
> :
>     _parent=Thread::current(); //tony: we added it for convenience.
> 3)verify the _parent field in the run() method of the newly created
> thread, by this time, the current thread should be the child thread
> created.
>    int ostid_parent=_parent->osthread()->thread_id();
> 
> Then if the child thread is ever running, I can get its parent thread.
> 
> Thanks a lot!
> 
> Tony
> 
> On Thu, Oct 29, 2009 at 6:39 AM, David Holmes <David.Holmes at sun.com> wrote:
>> Hi Tony,
>>
>>> I am working on a project where I need to identify the relations
>>> between parent threads and children threads. Especially, I want to do
>>> it inside the JVM, not in a java program.
>> As others have said you need to do this yourself by monitoring thread
>> construction and/or starting.
>>
>> In Java there is no parent/child relationship between threads - all threads
>> are considered equal siblings. This is in contrast to other systems where
>> "child" threads are an explicit notion where exceptions in a child can be
>> caught in a parent; where joining a parent joins all children etc. But
>> that's not Java.
>>
>> Cheers,
>> David Holmes
>>
>>> Looking at the code of hotspot, I can only find the thread list inside
>>> the Thread class, but it's not enough for my purpose.
>>>
>>> Is there some way that I can draw out a family tree of the threads
>>> within the hotspot? I know that from within a java program, we can
>>> draw a tree of all the ThreadGroups, but it again is not what I want.
>>>
>>> Or could you tell me how I could identify when a thread is created,
>>> and who is the parental thread that creates it?
>>>
>>> Thanks a lot!
>>>
>>> Tony Guan


More information about the hotspot-dev mailing list