aspose file tools*
The moose likes Threads and Synchronization and the fly likes How to get thread id? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How to get thread id?" Watch "How to get thread id?" New topic
Author

How to get thread id?

SivaramaKrishnan Nageswaran
Greenhorn

Joined: Aug 05, 2005
Posts: 7
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

Joined: Sep 27, 2005
Posts: 36
Have u tried using Thread.currentThread().getName()
SivaramaKrishnan Nageswaran
Greenhorn

Joined: Aug 05, 2005
Posts: 7
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

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
SivaramaKrishnan Nageswaran
Greenhorn

Joined: Aug 05, 2005
Posts: 7
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get thread id?