Why is activeCount = 3?

Discussion in 'Java' started by Pep, Aug 10, 2005.

  1. Pep

    Pep Guest

    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.
     
    Pep, Aug 10, 2005
    #1
    1. Advertising

  2. Pep wrote:
    > 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
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Aug 10, 2005
    #2
    1. Advertising

  3. Pep

    jan V Guest

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

    I take it you've never exploited the fantastic power of uncaughtException()
    then?
     
    jan V, Aug 10, 2005
    #3
  4. jan V wrote:
    >>ThreadGroup is generally ignored because it doesn't do much useful.

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


    When there's Thread.setUncaughtExceptionHandler?

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Aug 10, 2005
    #4
  5. Pep

    jan V Guest

    > >>ThreadGroup is generally ignored because it doesn't do much useful.
    > >
    > > I take it you've never exploited the fantastic power of

    uncaughtException()
    > > then?

    >
    > When there's Thread.setUncaughtExceptionHandler?


    But that has only been available since 1.5... what did you do before 1.5
    came along? ;-)
     
    jan V, Aug 10, 2005
    #5
  6. Pep

    Pep Guest

    Thomas Hawtin wrote:

    > Pep wrote:
    >> 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.
     
    Pep, Aug 10, 2005
    #6
  7. On Wed, 10 Aug 2005 14:53:26 +0100, Pep wrote:
    > 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

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Aug 11, 2005
    #7
  8. Pep

    Pep Guest

    Gordon Beaton wrote:

    > On Wed, 10 Aug 2005 14:53:26 +0100, Pep wrote:
    >> 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.
     
    Pep, Aug 11, 2005
    #8
  9. On Thu, 11 Aug 2005 08:37:37 +0100, Pep wrote:
    > 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

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Aug 11, 2005
    #9
  10. Pep

    Pep Guest

    Gordon Beaton wrote:

    > On Thu, 11 Aug 2005 08:37:37 +0100, Pep wrote:
    >> 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
    >


    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.
     
    Pep, Aug 11, 2005
    #10
    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. =?Utf-8?B?VGltOjouLg==?=

    Why, why, why???

    =?Utf-8?B?VGltOjouLg==?=, Jan 27, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    598
    Juan T. Llibre
    Jan 27, 2005
  2. avinashrk
    Replies:
    10
    Views:
    4,786
    Andrew Thompson
    Dec 14, 2004
  3. Horace Nunley

    why why why does function not work

    Horace Nunley, Sep 27, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    498
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Sep 27, 2006
  4. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    977
    Mark Rae
    Dec 21, 2006
  5. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,216
    Smokey Grindel
    Dec 2, 2006
Loading...

Share This Page