Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Id Problem

 
sonu raj
Ranch Hand
Posts: 43
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



When I compile and run the above program I always gets the same output:
i.e.

9
10

I want to know why the output is always 9 and 10, why not 2 and 4 or 4 and 5.etc
Please elaborate!!
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it's up to the JVM to decide how it wants to allocate Thread IDs. That's not fixed by the specification. So it just appears that, in this case anyway, the JVM is doing it in a way that repeats every time.

You never need to know how thread IDs are allocated when writing code. The only thing that's defined is that the ID will be constant while the thread is alive, and two alive threads will never have the same ID.
 
Anurag Verma
Ranch Hand
Posts: 168
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Being a programmer we should never be concerned about this, this is allocated by JVM & the allocation is not in our control, its the JVM which is allocating it.
 
sonu raj
Ranch Hand
Posts: 43
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much!!
 
Sachin Bhosale
Greenhorn
Posts: 7
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread Id sequence number starts from 0. And JVM uses initial few ids (e.g. 0-8) for it's own threads. For new threads , this sequence number is incremented.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic