Why is activeCount = 3?

P

Pep

When I start a program that spawns a thread for a listener socket object, I
get a count of 3 form the activeCount method but when I enumerate the
thread array I find the following

The number of activethreads are 3
tarray[0] Thread[Socket listener,5,main]
tarray[1] Thread[DestroyJavaVM,5,main]
tarray[2] null

So it appears that the activeCount is the length of the array including the
null trailer?

Is this always the case?

TIA,
Pep.
 
T

Thomas Hawtin

Pep said:
When I start a program that spawns a thread for a listener socket object, I
get a count of 3 form the activeCount method but when I enumerate the
thread array I find the following

The number of activethreads are 3
tarray[0] Thread[Socket listener,5,main]
tarray[1] Thread[DestroyJavaVM,5,main]
tarray[2] null

So it appears that the activeCount is the length of the array including the
null trailer?

I take it this is a question about ThreadGroup, which you don't mention.
ThreadGroup is generally ignored because it doesn't do much useful. As
you can see, even the API to find grouped threads and groups is poor.

As the docs for ThreadGroup.activeCount say it only gives an estimate.
Having a quick look at the source it counts threads that are not alive.
Has your program had a recent thread bereavement. Perhaps the main thread?

Tom Hawtin
 
J

jan V

ThreadGroup is generally ignored because it doesn't do much useful.

I take it you've never exploited the fantastic power of uncaughtException()
then?
 
T

Thomas Hawtin

jan said:
I take it you've never exploited the fantastic power of uncaughtException()
then?

When there's Thread.setUncaughtExceptionHandler?

Tom Hawtin
 
J

jan V

ThreadGroup is generally ignored because it doesn't do much useful.
When there's Thread.setUncaughtExceptionHandler?

But that has only been available since 1.5... what did you do before 1.5
came along? ;-)
 
P

Pep

Thomas said:
Pep said:
When I start a program that spawns a thread for a listener socket object,
I get a count of 3 form the activeCount method but when I enumerate the
thread array I find the following

The number of activethreads are 3
tarray[0] Thread[Socket listener,5,main]
tarray[1] Thread[DestroyJavaVM,5,main]
tarray[2] null

So it appears that the activeCount is the length of the array including
the null trailer?

I take it this is a question about ThreadGroup, which you don't mention.
ThreadGroup is generally ignored because it doesn't do much useful. As
you can see, even the API to find grouped threads and groups is poor.

As the docs for ThreadGroup.activeCount say it only gives an estimate.
Having a quick look at the source it counts threads that are not alive.
Has your program had a recent thread bereavement. Perhaps the main thread?

Tom Hawtin

No, that was taken from a running program that is sitting around waiting to
take network connections.

I am actually going to end up using my own method for determining when the
child threads are finshed so this will not really become anything other
than curiosity now. My main question came from wanting to try and do
things according to the book :)

Cheers,
Pep.
 
G

Gordon Beaton

When I start a program that spawns a thread for a listener socket
object, I get a count of 3 form the activeCount method but when I
enumerate the thread array I find the following

The number of activethreads are 3
tarray[0] Thread[Socket listener,5,main]
tarray[1] Thread[DestroyJavaVM,5,main]
tarray[2] null

So it appears that the activeCount is the length of the array
including the null trailer?

I'll bet that Thread.enumerate() returned 2, not 3 (there is no "null
trailer").

I have no idea why activeCount() shouldn't return the same value
however.

/gordon
 
P

Pep

Gordon said:
When I start a program that spawns a thread for a listener socket
object, I get a count of 3 form the activeCount method but when I
enumerate the thread array I find the following

The number of activethreads are 3
tarray[0] Thread[Socket listener,5,main]
tarray[1] Thread[DestroyJavaVM,5,main]
tarray[2] null

So it appears that the activeCount is the length of the array
including the null trailer?

I'll bet that Thread.enumerate() returned 2, not 3 (there is no "null
trailer").

I have no idea why activeCount() shouldn't return the same value
however.

/gordon

"null trailer" is my terminology for the last entry in the array.

I changed the program slightly to show the return value of enumerate and
sure enough it returns 2, so thanks for that.

So I suppose if I want to know the number of active threads I should create
a helper method that returns the value of enumerate but isn't there a
better way that would not have that overhead?

Cheers,
Pep.
 
G

Gordon Beaton

So I suppose if I want to know the number of active threads I should
create a helper method that returns the value of enumerate but isn't
there a better way that would not have that overhead?

Since the threads you want to count seem to belong to your own class,
you could simply add a static counter to the class in order to keep
track of them. Or do you really want to count *all* threads that may
have been created?

/gordon
 
P

Pep

Gordon said:
Since the threads you want to count seem to belong to your own class,
you could simply add a static counter to the class in order to keep
track of them. Or do you really want to count *all* threads that may
have been created?

/gordon

No, I'm only interested in the threads that I create from my class so this
is not a problem.

This is something that I did years ago and now having to do it again, I
thought I would explore the possibility that java has developed a mechanism
to do this for me as it certainly tries to do everything else for me :)

I get spoilt every time I come back to java from c++ by the amount of things
that java attempts to do for me. Oh well, I guess I can't expect everything
to be done for me.

Cheers,
Pep.
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top