简体   繁体   English

在 Java 1.4 中获取线程的唯一标识符

[英]Get unique identifier of a Thread in Java 1.4

In Java 1.4, is there any better way of getting a Thread's ID than using Thread.getName() ?在 Java 1.4 中,有没有比使用Thread.getName()更好的方法来获取线程的 ID?

I mean, getName() in unit tests returns something like "Thread-1" , but in WebLogic 10 I get "[ACTIVE] ExecuteThread: '1' for queue: 'weblogic.kernel.Default (self-tuning)'.xml" .我的意思是,单元测试中的getName()返回类似"Thread-1"内容,但在 WebLogic 10 中我得到"[ACTIVE] ExecuteThread: '1' for queue: 'weblogic.kernel.Default (self-tuning)'.xml"

Thread.getId (it can theoretically overflow , but it is defined not to and in practice will not). Thread.getId (理论上它可以溢出,但它被定义为不是,实际上也不会)。

1.5 is going through its End of Service Life period now, but if you are using old dusty-decks 1.4, then you can implement your own with ThreadLocal . 1.5 现在正处于使用寿命结束期,但如果您使用的是旧的灰尘甲板 1.4,那么您可以使用ThreadLocal实现您自己的。 (Note, don't follow the Java SE 6 API docs too closely!) (注意,不要太密切地关注 Java SE 6 API 文档!)

Why do you need this?你为什么需要这个? Because depending on your answer, there are several approaches.因为根据您的答案,有几种方法。

First, understand that a thread's name is not guaranteed to be unique.首先,了解线程的名称不能保证是唯一的。 Nor is identity hashcode.身份哈希码也不是。

If you truly want to associate a unique ID to a thread, you're going to need to do it yourself.如果您真的想将唯一 ID 与线程相关联,则需要自己进行。 Probably using an IdentityHashMap.可能使用 IdentityHashMap。 However, that will introduce a strong reference that you don't want to have hanging around in a production app.但是,这将引入一个您不希望在生产应用程序中闲逛的强大参考。

Edit: TofuBeer has solution that's probably better, although the docs note that thread IDs can be reused.编辑:TofuBeer 的解决方案可能更好,尽管文档指出线程 ID 可以重复使用。

As mentioned in " Thread.getId() global uniqueness question " SO question, and confirmed by the source code of Thread.java :如“ Thread.getId() 全局唯一性问题” SO问题中所述,并由Thread.java的源代码确认:

/* For generating thread ID */
private static long threadSeqNumber;

/* Set thread ID */
tid = nextThreadID();

private static synchronized long nextThreadID() {
    return ++threadSeqNumber;
}

The thread id is very simple to implement yourself if your are still in java1.4.如果你还在java1.4中,线程id很容易自己实现。
However this implementation means a given thread will not have the same id when you are running your program several times.然而,这种实现意味着当您多次运行程序时,给定线程将不会具有相同的 id。
So depending on what you need, you may have to implement a naming policy which is both:因此,根据您的需要,您可能必须实施一个命名策略,它是:

  • unique for a given runtime session对于给定的运行时是唯一的 session
  • reused from session to session从 session 重用到 session
  • still linked to the internal original naming policy managed by the 1.4 JVM ("Thread-1", "Thread-2", ...)仍然链接到由 1.4 JVM 管理的内部原始命名策略(“Thread-1”,“Thread-2”,...)

You can use getID if you are on JDK 1.5 or higher.如果您使用的是 JDK 1.5 或更高版本,则可以使用getID

Is it the case that you need a consistent value for each time you run the unit tests, or is just a unique value good enough?是不是每次运行单元测试时都需要一个一致的值,或者只是一个唯一的值就足够了?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM