Thread.getId() global uniqueness question

前端 未结 2 1281
日久生厌
日久生厌 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:08

    Well, let me check the source.

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

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

    In nextThreadID():

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

    And:

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

    It is never set, and thus defaults to 0.

    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.

提交回复
热议问题