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

Count all threads

D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

Is there any simple call to get number of active threads? Thread.activeCount() gives only count in the current group. I can traverse entire thread group tree, however probably somebody knows a better solution.


Retire your iPod and start with HD Android music player Kamerton | Minimal J2EE container is here | Light weight full J2EE stack | and build tool | Co-author of "Windows programming in Turbo Pascal"
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
No, traversing the tree is the only way that I know. It only gives an estimate of the total thread count. No guarantees.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

Yes, estimate will work. BTW, is there any way to get thread dump when traversing the tree?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
If you're using JDK 5.0, you can use the getStackTrace() method which has been added to Thread. Actually there's another really cool method for getting you info on all the live threads: getAllStackTraces(). If you're not using 5.0, then I think the best you can do is to use dumpStack() (also in the Thread class). Unfortunately this only writes to standard output. If that's what you want, great; if not, well I suppose you could use System.setOut() to to cause the dumpStack() to write to a different stream. Of course if you're also writing to standard output from other parts of the program (especially from other threads) then you're going to have a hard time separating the output. Hope that helps...
[ August 08, 2005: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

Thanks a lot, I've added recommended method for JDK 1.5 and it works perfectly. My problem is that some customers want to run it on old app servers supporting only JDK 1.4, so I need to have the feature for them too. I looked in the method dumpStack(), but it seems to be static, so how can I make every thread current to call it? Maybe I didn't understand the API right. Another question how to support JDK 1.5 and less in the same codebase? Anything better than just use reflection to discover JDK 1.5 availability?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[DR]: I looked in the method dumpStack(), but it seems to be static, so how can I make every thread current to call it? Maybe I didn't understand the API right.

No, you got it right - I forgot that the method was static. (Now I remember why I've never actually used that method.) I guess this isn't going to work.

Another question how to support JDK 1.5 and less in the same codebase? Anything better than just use reflection to discover JDK 1.5 availability?

Well, one possibility is instead of reflection, use System.getProperty("java.version") to determine what version of Java you're using. What you do with that information is not obvious, though. It might be best to just ship two different versions of the product. I don't really know - might be best to ask this as a separate question in another forum, like Java in General Intermediate or Advanced.

As for other ways to get the thread stack dumps without JDK 5.0: well, you might be able to do something with JPDA. I'm not sure this will work, and in particular if your code is being deployed an a variety of different app servers, it's probably going to be hard to find a way to connect the JVM to a debugger. Plus it wouldn't surprise me if it slowed down performance substantially. Dunno about that; I'm just guessing.

Also here some other ways you may be able to get stack dumps. These are rather platform-dependent; probably no good for what you're trying to do.

I suspect that you'll just have to tell 1.4.2 users that they don't get this functionality, but if they eventually upgrade, they will.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Count all threads