Problem wtih Java ThreadGroup.activeCount method

Discussion in 'Java' started by avinashrk, Dec 9, 2004.

  1. avinashrk

    avinashrk Guest

    Hi

    We hava base class server which creates worker threads and everytime it
    creates these threads it pushes them into a ThreadGroup thereby
    automatically incrementing the activeCount on this ThreadGroup.As I
    understand it when the child thread exits its run method, it should die
    and hence the ThreadGroup count should automatically go down.However
    this was not happening in my code. Initially i thought I might have
    written something wrong and so the run method hangs and some threads
    dont die. So I put some print statements to see if the worker thread
    was coming out of the run method and to my surprise it was and still
    the activeCount wont go down...

    Since there was some important decision logic based on this active
    count I just got rid of the ThreadGroup and kept a count of
    activethreads myself by using a varaiblw which was incremented for
    every thread created and decremented for every thread exiting out of
    its run method...So now the program works fine...mind you there was no
    other change i did..so the only thing I can point this to is some kind
    of problem with activeCount method in threadGroups....Has anyone else
    seen this kind of behavior? Any advice or tips would help
    Thanks
    Avinash
     
    avinashrk, Dec 9, 2004
    #1
    1. Advertising

  2. avinashrk

    nimph Guest

    I had to do the same thing in a project I recently worked on....
    Exactly!

    I have no idea why though... Maybe the Thread objects' destroy()
    method was not called yet, making the contained thread remain active.
    Or, maybe it's something to do with garbage collection?????
    Just guessing...

    Anyone else?
     
    nimph, Dec 9, 2004
    #2
    1. Advertising

  3. avinashrk

    avinashrk Guest

    Yes that is what my initial hunch was... But then I read somewhere that
    activeCount returns the count of the threads which are active .i.e NOT
    DEAD..and a thread is dead when it exits its run method...A thread
    object might be lying in memory to be garbage collected but the active
    status of the thread is only dependent on its exiting the run method
    because a thread can never be resurrected...

    so what I mean is if a thread is not garbage collected as soon as it
    exits its run method its ok but it surely does mean by definition that
    the thread is dead and shouldn't the activeCount go down then?
    Thanks
    Avinash
     
    avinashrk, Dec 9, 2004
    #3
  4. avinashrk

    nimph Guest

    The javadoc says that the return is an estimate of the number of active
    threads in this thread group and in any other thread group that has
    this thread group as an ancestor...

    Did any of your code create any threads that would have that
    threadGroup as it's ancestor?

    ????
     
    nimph, Dec 10, 2004
    #4
  5. avinashrk

    avinashrk Guest

    Ok here is what my code (incomplete..only parts of logic using
    threadGroup) looked like

    Class Server{

    protected ThreadGroup threadGroup;

    /** Creates a new instance of Server */
    public Server(String serviceName) {
    this.serviceName = serviceName;
    this.threadGroup = new ThreadGroup(serviceName);

    }

    public void run(){

    newSocket.runner = new Thread( threadGroup, newSocket);
    newSocket.runner.start();

    }


    }


    class PhredServer extends Server{
    //This class used the ThreadGroup.activeCount

    }


    So with this framework I was having 3 PhredServers running but each
    PhredServer was running in a separate JVM..i mean I started 3
    PhredServers by calling
    java PhredServer 3 times...So I can see that the serviceName is same in
    each of them but still since they are in different JVMs I think the
    ThreadGroups are distinct objects and therefore should have their own
    independent counts..right? i mean if there are two threads in each of
    these Servers then each of them should have count 2 and not 2*3=6?

    Other than that there were no other threads which had this group as an
    ancestor ...So what do you think?

    Avinash
     
    avinashrk, Dec 10, 2004
    #5
  6. avinashrk

    Esmond Pitt Guest

    ThreadGroup.activeCount() works for me, see following. JDK 1.3. As soon
    as a thread exits its run() method the activeCount goes down. Joining
    and GC have no effect on it.

    public class ThreadGroupTest extends Thread
    {
    ThreadGroup group;
    int timeout;

    /** Creates new ThreadGroupTest */
    ThreadGroupTest(ThreadGroup group,int timeout)
    {
    super(group,"timeout="+timeout);
    this.group = group;
    this.timeout = timeout;
    }

    public void run()
    {
    System.out.println(this+": running");
    try
    {
    sleep(timeout);
    }
    catch (InterruptedException exc)
    {
    }
    System.out.println(this+": exiting - activeCount="+group.activeCount());
    }

    /**
    * @param args the command line arguments
    */
    public static void main (String args[]) throws Exception
    {

    ThreadGroup group = new ThreadGroup("Telekinesis");
    Thread[] threads = new Thread[5];
    for (int i = 0; i < threads.length; i++)
    {
    threads = new ThreadGroupTest(group, (i+1)*1000);
    threads.start();
    }
    System.out.println("main after all starts:
    activeCount="+group.activeCount());
    for (int i = 0; i < threads.length; i++)
    {
    threads.join();
    System.out.println("main after joining "+i+":
    activeCount="+group.activeCount());
    }
    System.out.println("main after all joins:
    activeCount="+group.activeCount());
    threads = null;
    System.gc();
    System.out.println("main after GC: activeCount="+group.activeCount());
    }

    }
     
    Esmond Pitt, Dec 10, 2004
    #6
  7. avinashrk

    avinashrk Guest

    Hi Esmond

    What you have here surely works and i am not contending that...but my
    code is not as simple as that..it has cloning involved and also
    implements thr unnavle interfae (these are the 2 main
    differences)...Now it might be bcos of these differences that I see the
    behavior of active count

    In theory the behavior should be as your code shows but we see
    something different as did Nimph..so i want to know why that happened?

    also keep in mind I am not a novice programmer and I have shown it to
    people with about 5 years of Java programming experience and they dont
    seem to see anything weird I might be doing..So all in all I think
    there is some problem with threadGroup which I dont know of

    Avinash
     
    avinashrk, Dec 10, 2004
    #7
  8. avinashrk

    Esmond Pitt Guest

    As my example works as expected, the rational presumption at the moment
    is surely that there is a bug in your code rather than in ThreadGroup.

    I don't know why Nimph mentions Thread.destroy(), this method is
    deprecated, doesn't do anything, and shouldn't be called.

    Good luck.

    avinashrk wrote:
    > Hi Esmond
    >
    > What you have here surely works and i am not contending that...but my
    > code is not as simple as that..it has cloning involved and also
    > implements thr unnavle interfae (these are the 2 main
    > differences)...Now it might be bcos of these differences that I see the
    > behavior of active count
    >
    > In theory the behavior should be as your code shows but we see
    > something different as did Nimph..so i want to know why that happened?
    >
    > also keep in mind I am not a novice programmer and I have shown it to
    > people with about 5 years of Java programming experience and they dont
    > seem to see anything weird I might be doing..So all in all I think
    > there is some problem with threadGroup which I dont know of
     
    Esmond Pitt, Dec 11, 2004
    #8
  9. avinashrk

    avinashrk Guest

    Hi

    Alrite Esmond I dont think there is a bug, but I wouldn't mind someone
    looking at it again..If you would have time to look at it I can send it
    across..would you like to see it?
    In any case I appreciate your comments...

    Thanks
    Avinash
     
    avinashrk, Dec 11, 2004
    #9
  10. avinashrk

    Esmond Pitt Guest

    No, I'm not getting paid for this, you are, either in dollars or grades.

    As you have asserted a bug in threadGroup.activeCount I think it would
    be instructive for you to try to construct a test case for a Sun bug
    report. This should consist of the simplest possible test which exhibits
    the problem. You could either start with my code, elaborating it
    successively so it more resembles your code until the point at which the
    problem occurs, or you could start with your own code, progressively
    simplifying it until the problem disappears. Then review the last change
    you made - this will either demonstrate the bug's existence or show you
    what's wrong with your code.

    EJP

    > Alrite Esmond I dont think there is a bug, but I wouldn't mind someone
    > looking at it again..If you would have time to look at it I can send it
     
    Esmond Pitt, Dec 12, 2004
    #10
  11. On Sun, 12 Dec 2004 07:35:43 GMT, Esmond Pitt wrote:

    > ..This should consist of the simplest possible test which exhibits
    > the problem. You could either start with my code, elaborating it
    > successively so it more resembles your code until the point at which the
    > problem occurs, or you could start with your own code, progressively
    > simplifying it until the problem disappears. Then review the last change
    > you made - this will either demonstrate the bug's existence or show you
    > what's wrong with your code.


    Or, to put that another way..
    <http://www.physci.org/codes/sscce.jsp>

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.LensEscapes.com/ Images that escape the mundane
     
    Andrew Thompson, Dec 14, 2004
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Timo Nentwig

    Stopping a ThreadGroup

    Timo Nentwig, Oct 25, 2003, in forum: Java
    Replies:
    4
    Views:
    2,815
    Gambit
    Oct 25, 2003
  2. Pep

    Why is activeCount = 3?

    Pep, Aug 10, 2005, in forum: Java
    Replies:
    9
    Views:
    1,651
  3. Replies:
    1
    Views:
    380
  4. chris patton

    the problem wtih cgi

    chris patton, May 28, 2005, in forum: Python
    Replies:
    3
    Views:
    295
    Ivan Van Laningham
    May 29, 2005
  5. Replies:
    8
    Views:
    376
    John Stephens
    Sep 8, 2013
Loading...

Share This Page