• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread.getId() error and ambiguity in ORACLE docs ???

 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what oracle says :
"The thread ID is a positive long number generated when this thread was created".

So, when does a Thread actually get an ID ? After new Thread() OR after start() OR after after it enters running state. To check this out i made the following code and saw some strange behaviour.



CODE "A" TO PUT : This causes a compiler error :


CODE "B" TO PUT : This works as expected :


It seems from the above code that a thread is considered to be "created" when it is in running state , right ???
What is the basis for assigning ID's to threads ? I got 9,10,11...but there are only 4 threads in my code ? So, 1,2,3, (a fourth for main) etc is what i expected. Could it give 4,5,6 or 30 , 700 ,951 ???


PS : I would be very grateful if you could attract attention to this post on my behalf.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler error is presumably due to that stray semicolon after r2.getId(): it should be a comma, of course.

First, I don't understand what this program tells you about when the IDs are assigned; wouldn't you want to check the id of a single thread both before and after starting it to explore that question?

But second: when your Java code starts, there are many other threads already running: the main one, of course, and then several having to do with garbage collection, finalization, deadlock detection, etc.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:The compiler error is presumably due to that stray semicolon after r2.getId(): it should be a comma, of course.


Yes, i corrected that...it was the problem. (This is so embarrassing)

Ernest Friedman-Hill wrote:
First, I don't understand what this program tells you about when the IDs are assigned; wouldn't you want to check the id of a single thread both before and after starting it to explore that question?


I did that just now by getting ID's immediately after the new Thread() code. And the program runs as per expectation. So a thread is considered "created" when we instantiate a thread.
Perhaps oracle could add that.

Ernest Friedman-Hill wrote:
But second: when your Java code starts, there are many other threads already running: the main one, of course, and then several having to do with garbage collection, finalization, deadlock detection, etc.

Nice ! i was not aware of that. Suppose there are more programs running, each having their own threads, could that affect the numbers in my program ?
I hope i am not asking a foolish question ? Please tell me if thinking on these lines need higher concepts.

thanks

 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul Sudip Bose wrote:

I did that just now by getting ID's immediately after the new Thread() code. And the program runs as per expectation. So a thread is considered "created" when we instantiate a thread.
Perhaps oracle could add that.


"Create" is a synonym for "instantiate"; I think this might just be an English language issue. When I read that, it doesn't seem ambiguous to me.

Rahul Sudip Bose wrote: Suppose there are more programs running, each having their own threads, could that affect the numbers in my program ?
I hope i am not asking a foolish question ? Please tell me if thinking on these lines need higher concepts.


That's a fine question. Each Java application you run will be in its own JVM, and will have its own independent, unrelated thread IDs. If you put code like this in a servlet, though, you would observe different numbers for different runs, depending on the history of the JVM, because of course the JVM keeps running in between servlet requests and is shared between web apps.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:
Each Java application you run will be in its own JVM, and will have its own independent, unrelated thread IDs. If you put code like this in a servlet, though, you would observe different numbers for different runs, depending on the history of the JVM, because of course the JVM keeps running in between servlet requests and is shared between web apps.


Thanks for adding to my knowledge. I am a beginner and I am not able to "appreciate" the servlets part. But just out of curiosity, should a servlet developer be concerned about it ? That is, is it of importance in practical work or is it just of academic importance ?



 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!