In fact, you can get a stack trace of all running threads that are dumped to stdout using kill -QUIT <pid>
on * NIX OS, for example, when you launch the application on the Windows console and press Ctrl-Pause
(as another note to the poster.)
However, it looks like you are asking for software ways to do this. So, assuming what you really want is a collection of all topics that the current call stacks include one or more methods from this class ...
The best thing I can find that does not include calls in the JVMTI is to check the stacks of all running threads. I have not tried this, but should work in Java 1.5 and later. Keep in mind that this is by definition NOT ALL WITHOUT thread safe (the list of running threads - and their current stack traces - will constantly change under you ... a lot of paranoid material would be needed to actually use this list.)
public Set<Thread> findThreadsRunningClass(Class classToFindRunning) { Set<Thread> runningThreads = new HashSet<Thread>(); String className = classToFindRunning.getName(); Map<Thread,StackTraceElement[]> stackTraces = Thread.getAllStackTraces(); for(Thread t : stackTraces.keySey()) { StackTraceElement[] steArray = stackTraces.get(t); for(int i = 0;i<steArray.size();i++) { StackTraceElement ste = steArray[i]; if(ste.getClassName().equals(className)) { runningThreads.add(t); continue; } } } return runningThreads; }
Let me know if this approach suits you!
source share