Forums Register Login

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

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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

+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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 ?



What a stench! Central nervous system shutting down. Save yourself tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1842 times.
Similar Threads
GC question from dan mock
garbage collection query
THread Selection By JVM Problem
wait and notify
Exercise 13.6 in - A Programmer's Guide to Java SCJP Certification - 3rd Ed
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 10:32:49.