• 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

Count all threads

 
Ranch Hand
Posts: 472
Objective C Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
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
No, traversing the tree is the only way that I know. It only gives an estimate of the total thread count. No guarantees.
 
D Rog
Ranch Hand
Posts: 472
Objective C Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, estimate will work. BTW, is there any way to get thread dump when traversing the tree?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
D Rog
Ranch Hand
Posts: 472
Objective C Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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.
 
Die Fledermaus does not fear such a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic