• Post Reply Bookmark Topic Watch Topic
  • New Topic

activeCount Gives worng number :argh:  RSS feed

 
Sultan Al-Yahyai
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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!)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!