如何找到父线程的名称? [英] How to find the name of the parent thread?

查看:114
本文介绍了如何找到父线程的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在谈论流程时,我们可以拥有父母和孩子。但是可以获得父 Thread 名称吗?

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

我做了我的研究,但我找到的答案只是 .net

I did my research, but I have found answer only for .Net

编辑:我尝试设置名称:

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());
    }
}


推荐答案


我知道在谈论流程时,我们可以拥有父母和孩子。但是有可能获得父线程名称吗?

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

正如Jon所提到的,线程无法知道其父线程。这很重要,因为如果每个子节点都引用了分叉它们的线程,那么这将意味着内存中存在许多不必要的线程结构。如果孩子持有对它的引用,则无法回收父线程结构或重用它。

As Jon mentioned, there is no way for a thread to know its parent thread. This is important because 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 parent thread structure could not be reclaimed by the GC or reused if the child held a reference to it.

在查看代码时,父级用于获取守护进程状态,优先级和其他信息,但不存储在 Thread 对象中。

In looking at the code, the parent is used to get daemon status, priority, and other information but is not stored in the Thread object.

您提到需要拥有线程的名称,以便您可以将那些在控制流中一起的组合在一起。我会查看 ThreadGroup s。它们不经常使用,但在这种情况下你可能想要:

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.

编辑

您提到真正的问题是如何衡量分布式系统的每个组件中的花费的时间 - 在这种情况下RMI处理程序。

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.

我担心这里没有简单的答案。对于挂钟,您必须在每次RMI方法调用开始时将 System.currentTimeMillis()与结束时间进行比较。您还可以使用以下代码来测试线程使用的CPU时间。

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()); 

要获得用户时间,请使用 getThreadUserTime(...) 。我不确定thread-id是否可以重用,所以你需要做的就是在集合中记录RMI调用中的所有thread-id,然后在监视线程中记下它们的CPU和用户时间。

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.

我怀疑RMI线程有一个特定的名称,所以你的监控线程可以找到线程列表中的线程来执行此操作,但是你无法确定哪个线程正在处理哪个RMI请求。

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.

最后,要考虑的一件事是在流程中的多个点上加上时间戳并传递 long [] 在两次通话之间。这会增加一小部分数据开销,但您可以很好地了解分布式系统各个不同部分的性能。

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.

这篇关于如何找到父线程的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆