Can a Thread be active even after the run() method has finished execution ?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Mayank_Rastogi
Posted On:   Monday, August 16, 2004 05:41 AM

Scenario - I have written a class, say A which implements the Runnable interface. O/S - Windows 2000 Professional Objective - To keep only a certain number of the threads active of the above class A. My approach is to assign a common thread group, say GrpA to all thread instances of class A that are being created. Method activeCount() from ThreadGroup is used to check the number of active threads. In case, the above method indicates the number of active threads are lesser than how many can be kept alive, fresh ones are instantiated (if required). The problem - It seems that sometimes the activeCount() from ThreadGroup indicates a larger value than that seems to be a   More>>

Scenario - I have written a class, say A which implements the Runnable interface.

O/S - Windows 2000 Professional



Objective - To keep only a certain number of the threads active of the above class A.



My approach is to assign a common thread group, say GrpA to all thread instances of class A that are being created. Method activeCount() from ThreadGroup is used to check the number of active threads.

In case, the above method indicates the number of active threads are lesser than how many can be kept alive, fresh ones are instantiated (if required).



The problem - It seems that sometimes the activeCount() from ThreadGroup indicates a larger value than that seems to be active.



Is it possible that threads may have completed execution, but the object is still residing in memory & so a larger value is indicated? Would invoking garbage collector help?

Any other reasons ?

Ways for me to find out actually working threads?

   <<Less

Re: Can a Thread be active even after the run() method has finished execution ?

Posted By:   Edward_Harned  
Posted On:   Monday, August 16, 2004 07:14 AM

The API for activeCount() says the following:



Returns an estimate of the number of active threads in this thread group.


Returns:

the number of active threads in this thread group and in any other thread group that has this thread group as an ancestor.



You can always keep an accurate count yourself in memory shared by the threads. There are many methods for sharing memory; using a Singleton, synchronizing on a Class object, etc.

About | Sitemap | Contact