Problem wtih Java ThreadGroup.activeCount method

A

avinashrk

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
 
N

nimph

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?
 
A

avinashrk

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
 
N

nimph

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?

????
 
A

avinashrk

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
 
E

Esmond Pitt

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());
}

}
 
A

avinashrk

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
 
E

Esmond Pitt

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.
 
A

avinashrk

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
 
E

Esmond Pitt

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
 
A

Andrew Thompson

..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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top