Thread.getId() global uniqueness question

前端 未结 2 1259
日久生厌
日久生厌 2021-02-13 18:43

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?

J

2条回答
  •  有刺的猬
    2021-02-13 19:23

    According to the JDK source, a thread ID is unique in a given JVM - in fact, it's simply implemented as a running sequence.

    Here's the nextThreadID() method from 1.6.0_10:

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

    (there's probably actually a long overflow bug in there, presumably it's never actually happened)

提交回复
热议问题