• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get thread id?

 
SivaramaKrishnan Nageswaran
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All,

I'm using JDK 1.4.2_07 in Solaris UNIX. I've a multi-threaded application and I wanted to measure the CPU usage of every thread from Java. I wrote a Native C application that could return me the CPU usage of the thread given the process id and the thread id. But I'm not sure of how to get the current thread id so that it can be passed on to the native applcation. I'm able to get the current thread name (+ the current process id) and I'm not sure how to get the thread id using this information. It would be of great help, if somebody could help me in getting the thread id.

Thanks & Regards
- Siva
 
Alpana Singh
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have u tried using Thread.currentThread().getName()
 
SivaramaKrishnan Nageswaran
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

currentThread.getName() returns me the name of the current thread. But there is something called as id associated with every thread (this info can be obtained from /proc/<process id>/lwp/ directory, which will contain the list of directory for every lwps or threads). My basic requirement is to find the mapping between the name that is maintained by Java and the id maintained by the OS.

Hope I've made sense!!!

Thanks & Regards
- Siva
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java does not give you a way to get the native thread ID. One reason is that there is no guarantee of a one-to-one correspondence between Java threads and native threads. In the early days of Java, there were "green threads", where one native thread served many Java threads; I don't know if they are still supported or used much.

If you really need the native thread ID, you'll have to write native code and use JNI.
 
SivaramaKrishnan Nageswaran
Greenhorn
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I wrote the Native code using C and invoked the same using JNI. But unfortunately I got the thread id of the JVM (which is always 1) and not the id of the thread from where the JNI call is made :-(

Regards
- Siva
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic