• 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
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to get thread id?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have u tried using Thread.currentThread().getName()
 
SivaramaKrishnan Nageswaran
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sasparilla and fresh horses for all my men! You will see to it, won't you tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic