• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

how can i get the current thread id ?

 
Ranch Hand
Posts: 620
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all
i have done java application that invoking some stuff this application is multithread
now i like to print to log every time some application has invoked my application api
the current thread id , so then i could grep in logs the flow by thread id .
how can it be done in java to get the current thread id ?
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thread object for current thread is Thread.currentThread(), a static method.

From Java 5 onwards, there is a Thread instance method getId(). Before that, there is only getName().

The getId() method may, or may not, return the underlying operating system (OS) thread ID. Remember that Java threads are not required to have a one-to-one correspondence with OS threads; they usually do, but it's not guaranteed. If you find that getId() does give the OS thread ID on your favourite JVM and OS, that's fine to an extent, but making that assumption makes your program non-portable.

Finally, note that the value returned by getId() can be the same for two different threads. This is because, while the ID of a thread will not change while it is running, and no other thread will have the same ID while it is running, the ID may be reused when it terminates. Generally, the ID will not be reused for a long time, but making that assumption is again non-portable.
[ June 25, 2007: Message edited by: Peter Chase ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic