Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how can i get the current thread id ?  RSS feed

 
Meir Yan
Ranch Hand
Posts: 599
  • 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 ?
 
Peter Chase
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 ]
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!