• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

how can i get the current thread id ?  RSS feed

 
Ranch Hand
Posts: 600
  • Mark post as helpful
  • send pies
  • 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
  • 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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!