Thread.getId()全局唯一性问题 [英] Thread.getId() global uniqueness question

查看:150
本文介绍了Thread.getId()全局唯一性问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果系统上运行多个Java应用程序,那么每个线程ID是否都与所有其他Java线程相比是唯一的,无论它们运行的​​是什么应用程序?

If multiple Java applications are running on a system, is each Thread ID unique relative to all other Java threads, regardless of what application they are running in?

Java应用程序应该相对于其他Java应用程序是沙盒子,所以我认为线程ID可能会发生冲突。

Java applications are supposed to be sand-boxed relative to other Java applications so I thought it might be possible for Thread IDs to collide.

如果线程ID在所有应用程序中都是唯一的,这不会泄漏有关系统上其他应用程序的一些(尽管很小)信息吗?比如在其他应用程序中启动了多少线程,或者甚至其他Java应用程序都在运行?

If the Thread IDs are unique across all applications, won't that leak some (although very minor) information about other applications on the system? Such as how many threads have started in other applications, or even if other Java applications are running at all?

推荐答案

好吧,让我们来吧我检查来源。

Well, let me check the source.

线程 init 方法(由每个构造函数调用):

In the Thread's init method (which is called by every constructor):

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

nextThreadID()

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

并且:

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

永远不会设置,因此默认为0.

It is never set, and thus defaults to 0.

所以显然线程ID号始终从0开始并递增1.换句话说,你的问题的答案是它们全局唯一。

So apparently thread ID numbers always start at 0 and increment by 1. In other words, the answer to your question is that they are not globally unique.

这篇关于Thread.getId()全局唯一性问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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