Java 线程 ID 是否会在整个堆栈中保持不变?

Will a Java thread ID stay the same for the full stack?

如果我调用 Thread.currentThread().getId(),我会得到一个代表当前线程 ID 的长整数。

我在 SessionContext class 中使用线程 ID 进行用户-threadId 映射。当用户对后端进行 API 调用时,我将当前的 threadId 存储在他们的 userId 中,这样我就可以随时调用 SessionContext.getUserId(),它将使用 threadId 将我映射回来给用户。但是,如果 Thread.currentThread().getId() 的结果在堆栈中的任何一点发生变化,这种方法将不起作用。

我的问题是,对于整个堆栈,这个 ID 会保持不变吗?

The documentation 说得很清楚了:

Returns the identifier of this Thread. The thread ID is a positive long number generated when this thread was created. The thread ID is unique and remains unchanged during its lifetime. When a thread is terminated, this thread ID may be reused.

我认为这就是 ID 的全部意义所在 — 它在其生命周期内保持不变。