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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/701220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:25:56  来源:igfitidea点击:

Get unique identifier of a Thread in Java 1.4

javamultithreadinguniqueidentifier

提问by Tiago Fernandez

In Java 1.4, is there any better way of getting a Thread's ID than using Thread.getName()?

在 Java 1.4 中,有没有比使用更好的方法来获取线程的 ID Thread.getName()

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".

回答by Tom Hawtin - tackline

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. (Note, don't follow the Java SE 6 API docs too closely!)

1.5 现在正在经历其服务生命周期结束,但如果您使用的是旧的尘土飞扬的甲板 1.4,那么您可以使用ThreadLocal. (注意,不要太紧跟 Java SE 6 API 文档!)

回答by kdgregory

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. Probably using an IdentityHashMap. However, that will introduce a strong reference that you don't want to have hanging around in a production app.

如果您真的想将唯一 ID 与线程相关联,您将需要自己完成。可能使用 IdentityHashMap。但是,这将引入一个强引用,您不希望在生产应用程序中闲逛。

Edit: TofuBeer has solution that's probably better, although the docs note that thread IDs can be reused.

编辑:TofuBeer 有可能更好的解决方案,尽管文档指出线程 ID 可以重用。

回答by VonC

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.
However this implementation means a given thread will not have the same id when you are running your program several times.
So depending on what you need, you may have to implement a naming policy which is both:

如果你还在 java1.4 中,线程 id 很容易自己实现。
然而,这种实现意味着当您多次运行程序时,给定的线程将不会具有相同的 id。
因此,根据您的需要,您可能必须实施以下两种命名策略:

  • unique for a given runtime session
  • reused from session to 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”、...)

回答by TofuBeer

You can use getIDif 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?

是每次运行单元测试时都需要一个一致的值,还是一个唯一的值就足够了?