This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Thread.getId() error and ambiguity in ORACLE docs ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Thread.getId() error and ambiguity in ORACLE docs ???" Watch "Thread.getId() error and ambiguity in ORACLE docs ???" New topic
Author

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

Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

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.


SCJP 6. Learning more now.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24081
    
  15

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.


[Jess in Action][AskingGoodQuestions]
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

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
Marshal

Joined: Jul 08, 2003
Posts: 24081
    
  15

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

Joined: Jan 21, 2011
Posts: 637

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 ?



 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Thread.getId() error and ambiguity in ORACLE docs ???
 
Similar Threads
Exercise 13.6 in - A Programmer's Guide to Java SCJP Certification - 3rd Ed
THread Selection By JVM Problem
GC question from dan mock
garbage collection query
wait and notify