Reputation: 424
I was wondering when does the Id of a thread in Java is created. Is it in the moment the instance is created or is it after is launched with the start method?
Thanks.
Upvotes: 0
Views: 278
Reputation: 31795
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.
Upvotes: 0
Reputation:
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();
...
}
Upvotes: 4
Reputation: 18488
It is initialized on the Thread
constructor.
Code snippet from implementation here:
/* Set thread ID */
tid = nextThreadID();
Upvotes: 0