• 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

activeCount Gives worng number :argh:

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I have a thread to process files ...


in the main class I defined a ThreadGroup "process_tasks",

then I call the thread for diffrent tasks. different tasks will take different times to finish processing.


in some conditions I need to know, the number of active threads in the system. I have used the activeCount method



the problem is that I'm geting number much larger than the number of threads I called.

for exampl, if I process 7 files and I call activeCount() many times then I get this secqunce of number , 0 2 6 23 22 2 . while the I only used the thread for processing 7 files only.

can any body explain this for me? may be I'm doing somthing wrong?!
please help

regards
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you read the Javadoc for activeCount(), you'll see that it's very careful to state that this number is an estimate; if you read the Javadoc for enumerate(), you'll see that the idea is that the estimate is intended to always be large enough to hold the full list of threads, so it always errs on the large side. I don't believe there's a method that gives you a precise count.

If you need to know how many Threads you've started, then you can use enumerate() on the ThreadGroup and iterate over the array, checking which ones are still active (being sure to check for nulls!)
 
reply
    Bookmark Topic Watch Topic
  • New Topic