Java Thread Id Creation Time
I was wondering when does the Id of a thread in Java is created. Is it in the moment the instance is create开发者_如何转开发d or is it after is launched with the start method?
Thanks.
When instantiated.
public Thread() {
init(null, null, "Thread-" + nextThreadNum(), 0);
}
private void init(ThreadGroup g, Runnable target, String name, long stackSize) {
...
/* Set thread ID */
tid = nextThreadID();
...
}
It is initialized on the Thread
constructor.
Code snippet from implementation here:
/* Set thread ID */
tid = nextThreadID();
At the time of instance creation.
From the source code id returned from Thread.getId() is initialized when Thread instance were created (i.e. in its constructor), regardless when this Thread instance is actually started.
精彩评论