12

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, but I have found answer only for .Net


Edit: I tried setting names:

public class Main {

    public static void main(String[] args) {
        Thread r = new ThreadA();
        r.start();
    }

}



public class ThreadA extends Thread {
    public void run() {
        Thread.currentThread().setName("Thread A");
        System.out.println("Here  " + Thread.currentThread().getName());
        Thread r = new ThreadB();
        r.setName(Thread.currentThread().getName());
        r.start();
    }
}

public class ThreadB extends Thread {
    public void run() {
        Thread.currentThread().setName("Thread B");
        System.out.println("Here " + Thread.currentThread().getName());
        Thread r = new ThreadC();
        r.setName(Thread.currentThread().getName());
        r.start();
    }
}

public class ThreadC extends Thread {
    public void run() {
        Thread.currentThread().setName("Thread C");
        System.out.println("Here " + Thread.currentThread().getName());
    }
}
Community
  • 1
  • 1
alicjasalamon
  • 4,171
  • 15
  • 41
  • 65

4 Answers4

14

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

A thread does not have a reference to the parent thread so there is no way for you to get the name of the parent from a particular thread. In looking at the code, the parent thread is used to get daemon status, priority, and other information but the name is not stored in the new Thread object.

You mentioned that you need to have the name of the threads so you can group those that "go together in a control flow". I would look into ThreadGroups. They aren't used too often but you might want to in this case:

ThreadGroup threadGroup = new ThreadGroup("mythreadgroup");
Thread thread = new Thread(threadGroup, new Runnable() {...});
...
// then you can do such methods as
threadGroup.enumerate(...);

With thread-groups you can tie multiple threads together. You can, of course, do this with a collection as well yourself.


Edit:

You mentioned that the real issue is how can you measure the "time spent" in each component of a distributed system – in this case the RMI handlers.

I'm afraid there is no easy answer here. For wall clock, you are going to have to compare the System.currentTimeMillis() at the start of each RMI method call with the time from the end. You can also use the following code to test the CPU time used by the thread.

ThreadInfo threadInfo =
    ManagementFactory.getThreadMXBean().getThreadCpuTime(thread.getId()); 

To get the "user" time you use getThreadUserTime(...). I'm not sure thread-ids are reused so maybe all you need to do is record all of the thread-ids in your RMI calls in a collection and then note their CPU and user times in a monitoring thread.

I suspect the RMI threads have a particular name so your monitoring thread could find the threads in the thread list to do this but you are not going to be able to determine which thread is handling which RMI request.

Lastly, one thing to consider is to take time stamps at a number of points in the process and to pass this long[] around between calls. This would add some small percentage of data overhead but then you would be able to get a good feeling about the performance of the various different parts of your distributed system.

Gray
  • 115,027
  • 24
  • 293
  • 354
  • Sound like a solution, thank you! Is it possible to use `ThreadGroup` for threads created by RMI or on a different JVMs? – alicjasalamon Jul 30 '12 at 13:56
  • Certainly not available on a different JVM. I don't think you can control the RMI threads either. They are handled automagically by the JVM. – Gray Jul 30 '12 at 14:06
  • 2
    I still fail to see the reason why this is necessary @trebuchet? It sounds like you are trying the square peg in a round hole. – Gray Jul 30 '12 at 14:14
  • 1
    I have no idea what does it mean to "square peg in a round hole"(non-native speaker), but sounds terrifying :) What I need is to gather together threads from every request. My task is to measure (for every request) time spent in each component in distributed system – alicjasalamon Jul 30 '12 at 14:17
  • That's the question you should be asking. "How do I measure the time spent in a RMI call". That's the correct question @trebuchet. I suspect that you are going to have to record the wall clock at the start and end of the thread. You can also use `ThreadInfo threadInfo = ManagementFactory.getThreadMXBean().getThreadCpuTime(thread.getId());` (or `getThreadUserTime`) at the start and the end. – Gray Jul 30 '12 at 14:25
  • Oh, it is hard to be a girl on CS studies. I'll try to make it more precise. I've though I can write a method which get parent name and set is as its name, even if it is RMI thread. My idea is to log from every component: time, localization and some kind of request-ID. I thought this thread-name can be used as this ID. Do you recommend to write another question with more details? – alicjasalamon Jul 30 '12 at 14:37
  • I've added to my answer with some recommendations @trebuchet. I would consider, however, writing a new question. When you are asking for help (in general) be careful of asking a too specific question. Best of luck. – Gray Jul 30 '12 at 14:39
  • (For people who don't understand the analogy that Gray makes / idiom that he uses, see https://en.wikipedia.org/wiki/Square_peg_in_a_round_hole) – Stephen C May 06 '18 at 05:04
  • "if every child had a reference to the thread that forked them then this would mean a lot of unnecessary thread structures held in memory" - the name is not a reference... – benji Apr 23 '19 at 22:12
  • Agreed @Ben. What I meant was that there is no reference to the parent thread from while you can get the name. I'll see if I can make that more plain. – Gray Apr 24 '19 at 21:35
11

No - there's no particular concept of a "parent" thread in either Java or .NET. As per the .NET answer you referenced, however, if you're creating the thread yourself you can always give a name which indicates the "creator" thread name within the new thread's name.

EDIT: Your sample code sets the name before it starts... but then overwrites it after it starts, ignoring the previous name.

I'd expect something like:

String currentName = Thread.currentThread.name();
Thread thread = new Thread(new RunnableC());
thread.setName("C (started by" + currentName + ")");
thread.start();

That would be the only place the name of the thread would be set.

Note that this also uses the idea of implementing Runnable rather than extending Thread. That's a separate matter, but is the preferred approach in most cases.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
7

Using InheritableThreadLocal<T> with a carefully crafted

@Override protected T childValue(T parentValue) {
    // Use Thread.currentThread() -- the parent -- to make a return value.
}

makes it possible for threads you have no control over to pass a reference to themselves to any child threads they create -- this will be the closest thing their children have to a parent.

As mentioned by Gray, keeping such references could hinder GC, so wrapping them in a WeakReference<Thread> could be necessary.

Here is an example where each thread knows its full ancestry, unless ancestors are dead and buried by GC.

import java.lang.ref.WeakReference;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;

import static java.lang.Thread.currentThread;

public class ThreadAncestry {

    /** Linked list holding the thread which created the current one, and its ancestry */
    static class Chain {

        final Chain ancestors;
        final WeakReference<Thread> parent;

        Chain(Chain ancestors, Thread parent) {
            this.ancestors = ancestors;
            this.parent = new WeakReference<>(parent);
        }

        @Override
        public String toString() {
            Thread parent = this.parent.get();
            return   (parent == null ? "[dead and buried]" : parent.getName())
                   + (ancestors == null ? "" : " -> " + ancestors);
        }

    }

    /** Prints the current thread's ancestry, then spawns a new thread which does the same. */
    static void spawnRecursively(InheritableThreadLocal<Chain> ancestors, int remainingSpawns) {
        System.out.println(  "The ancestors of " + currentThread().getName() + " are " + ancestors.get());
        if (remainingSpawns > 0)
            new Thread(() -> spawnRecursively(ancestors, remainingSpawns - 1)).start();
    }

    /** Uses an InheritableThreadLocal to record the ancestry of each thread as they are created. */
    public static void main(String[] args) {
        InheritableThreadLocal<Chain> ancestors = new InheritableThreadLocal<Chain>() {
            @Override
            protected Chain childValue(Chain parentValue) {
                return new Chain(parentValue, currentThread()); // This is called by the parent thread.
            }
        };

        spawnRecursively(ancestors, 3);

        IntStream.range(0, 6).parallel().forEach(
                i -> System.out.println(  i + " ran on " + currentThread().getName()
                                        + " with ancestors " + ancestors.get()));

        ExecutorService service = Executors.newSingleThreadExecutor();
        service.submit(() -> {
            System.out.println(  currentThread().getName() + " has ancestors "
                               + ancestors.get() + "; it will now attempt to kill these.");
            System.gc(); // May not work on all systems.
            System.out.println(  currentThread().getName() + " now has ancestors "
                               + ancestors.get() + " after attempting to force GC.");
            service.shutdown();
        });
    }

}

This example results in the following output on my machine:

The ancestors of main are null
The ancestors of Thread-0 are main
The ancestors of Thread-1 are Thread-0 -> main
The ancestors of Thread-2 are Thread-1 -> Thread-0 -> main
3 ran on main with ancestors null
4 ran on main with ancestors null
5 ran on ForkJoinPool.commonPool-worker-2 with ancestors main
0 ran on ForkJoinPool.commonPool-worker-3 with ancestors ForkJoinPool.commonPool-worker-1 -> main
1 ran on ForkJoinPool.commonPool-worker-1 with ancestors main
2 ran on ForkJoinPool.commonPool-worker-2 with ancestors main
pool-1-thread-1 has ancestors main; it will now attempt to kill these.
pool-1-thread-1 now has ancestors [dead and buried] after attempting to force GC.

I'm not sure how generally useful this is, but it can be used to, e.g., hierarchically display what each of a number of threads (over which you have no control) have printed to System.out or logged with java.util.Logger; this is something you would want to implement as part of a test framework with parallel test runs, for instance.

2

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.

Community
  • 1
  • 1
Evgeni Sergeev
  • 22,495
  • 17
  • 107
  • 124