This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes Getting number of active threads and open sockets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Getting number of active threads and open sockets" Watch "Getting number of active threads and open sockets" New topic
Author

Getting number of active threads and open sockets

Victor Ho
Ranch Hand

Joined: Sep 05, 2003
Posts: 74
Hi,

Are their any APIs from the JDK that can retrieve the number of active threads and open sockets used by the VM. (not using any native code).

Thanks
Victor
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

Originally posted by Victor Ho:
Hi,

Are their any APIs from the JDK that can retrieve the number of active threads and open sockets used by the VM. (not using any native code).

Thanks
Victor


The Thread class has an enumerate() method that returns the number of threads in the current thread group and all sub-groups. Just call the method from a thread in the main thread group, (or any thread if your application doesn't create any threadgroups) and you'll get all the threads for you application.

You'll be missing some threads above your application, but for security reasons, you should not be accessing those threads anyway.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting number of active threads and open sockets