1

How to set the same name for parent and child in this situation?

public class ComponentA {
    public static void main(String[] args){

        Bye bye = new Bye();
        Thread t = new Thread(bye);
        t.start();
    }
}

I cannot add t.setName(Thread.currentThread().getName());, because I need to avoid changing code. I'm looking for solution I can apply from AspectJ class. Is there any way to put an argument to run method? Or maybe in child thread we can get parent name?

EDIT:

I mean:

  • parent - main in ComponentA
  • child - run in Bye
Gray
  • 115,027
  • 24
  • 293
  • 354
alicjasalamon
  • 4,171
  • 15
  • 41
  • 65
  • What do you mean by "parent" and "child"? Are you referring to `Bye` (which must be in the inheritance tree that includes `Runnable`) as the child and the `Thread` object `t` as the parent? – Thomas Owens Aug 07 '12 at 13:56
  • What is parent? What is name? Do you want to get the main thread's name from the started thread (`t`)? – JB Nizet Aug 07 '12 at 13:56
  • Related to: http://stackoverflow.com/questions/11722749/how-to-find-the-name-of-the-parent-thread – Gray Aug 07 '12 at 13:56

2 Answers2

4

Edit:

Is there any way to put an argument to run method? Or maybe in child thread we can get parent name?

Once the thread has started and the run() method has been called, there is no way to find out the name of the parent thread as was discussed in this answer here:

How to find the name of the parent thread?

You can certainly do:

Bye bye = new Bye();
Thread t = new Thread(bye, Thread.currentThread().getName() + "-child");
t.start();

This sets the name of the newly created thread to be related to the current thread which is it's parent. But with the edit you made, I see that you can't change how the thread has been forked.

As was mentioned in my other answer, I think you are going to have to use some other mechanism aside from the name to group your threads.

Inside of the run() method, you can certainly make a call to add the current thread into some collection of threads for reporting purposes.

Community
  • 1
  • 1
Gray
  • 115,027
  • 24
  • 293
  • 354
-1

By using Thread.setName().

Read More here

JDGuide
  • 6,239
  • 12
  • 46
  • 64