• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread Id Problem

 
Ranch Hand
Posts: 43
Android C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 171
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much!!
 
Greenhorn
Posts: 7
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic