java线程id的生成规则

2,520 阅读1分钟

源码

Thread



//线程名字
/* For autonumbering anonymous threads. */

    private static int threadInitNumber;

    private static synchronized int nextThreadNum() { //同步

        return threadInitNumber++; //自增 从0开始(默认值是0)

    }


//线程id 也是自增 和线程名字-id不是同一个字段 二者没有关系 是独立的
/*

     * Thread ID

     */

    private long tid;



    /* For generating thread ID */

    private static long threadSeqNumber;





    private static synchronized long nextThreadID() {

        return ++threadSeqNumber; //自增 从7开始(默认是7)???

    }


/**

     * Initializes a Thread.

     *

     * @param g the Thread group

     * @param target the object whose run() method gets called

     * @param name the name of the new Thread

     * @param stackSize the desired stack size for the new thread, or

     *        zero to indicate that this parameter is to be ignored.

     * @param acc the AccessControlContext to inherit, or

     *            AccessController.getContext() if null

     */

    private void init(ThreadGroup g, Runnable target, String name,

                      long stackSize, AccessControlContext acc) {

        if (name == null) {

            throw new NullPointerException("name cannot be null");

        }



        this.name = name.toCharArray();



        Thread parent = currentThread();

        SecurityManager security = System.getSecurityManager();

        if (g == null) {

            /* Determine if it's an applet or not */



            /* If there is a security manager, ask the security manager

               what to do. */

            if (security != null) {

                g = security.getThreadGroup();

            }



            /* If the security doesn't have a strong opinion of the matter

               use the parent thread group. */

            if (g == null) {

                g = parent.getThreadGroup();

            }

        }



        /* checkAccess regardless of whether or not threadgroup is

           explicitly passed in. */

        g.checkAccess();



        /*

         * Do we have the required permissions?

         */

        if (security != null) {

            if (isCCLOverridden(getClass())) {

                security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);

            }

        }



        g.addUnstarted();



        this.group = g;

        this.daemon = parent.isDaemon();

        this.priority = parent.getPriority();

        if (security == null || isCCLOverridden(parent.getClass()))

            this.contextClassLoader = parent.getContextClassLoader();

        else

            this.contextClassLoader = parent.contextClassLoader;

        this.inheritedAccessControlContext =

                acc != null ? acc : AccessController.getContext();

        this.target = target;

        setPriority(priority);

        if (parent.inheritableThreadLocals != null)

            this.inheritableThreadLocals =

                ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);

        /* Stash the specified stack size in case the VM cares */

        this.stackSize = stackSize;



        /* Set thread ID */

        tid = nextThreadID(); //自增

    }


测试结果

1个线程 thread name:Thread-0, thread id:8 //一个从0开始 一个从8开始


10个线程

thread name:Thread-0, thread id:8 //同上

thread name:Thread-6, thread id:14

thread name:Thread-7, thread id:15

thread name:Thread-8, thread id:16

thread name:Thread-5, thread id:13

thread name:Thread-1, thread id:9

thread name:Thread-3, thread id:11

thread name:Thread-2, thread id:10

thread name:Thread-9, thread id:17

thread name:Thread-4, thread id:12

总结

不管默认值是几 但是2个id是独立的 互不影响