atzu
atzu

Reputation: 424

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 created or is it after is launched with the start method?

Thanks.

Upvotes: 0

Views: 278

Answers (4)

Eugene Kuleshov
Eugene Kuleshov

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

user425367
user425367

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

Prince John Wesley
Prince John Wesley

Reputation: 63708

At the time of instance creation.

Upvotes: 0

Oscar Gomez
Oscar Gomez

Reputation: 18488

It is initialized on the Thread constructor.

Code snippet from implementation here:

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

Upvotes: 0

Related Questions