java thread id_java线程id的生成规则

源码

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是独立的 互不影响

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值