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.
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 ]
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?
Joined: Jan 30, 2000
[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.