Get number of threads in JVM

Discussion in 'Java' started by Crouchez, Sep 1, 2007.

  1. Crouchez

    Crouchez Guest

    Is there anyway to get the number of threads running in JVM through the java
    api?
    Crouchez, Sep 1, 2007
    #1
    1. Advertising

  2. Crouchez wrote:
    > Is there anyway to get the number of threads running in JVM through the java
    > api?
    >


    Not that I know of but why would you care?

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Sep 1, 2007
    #2
    1. Advertising

  3. Crouchez wrote:
    > Is there anyway to get the number of threads running in JVM through the java
    > api?


    Try:

    ThreadGroup tg = Thread.currentThread().getThreadGroup();
    while(tg.getParent() != null) {
    tg = tg.getParent();
    }
    Thread[] t = new Thread[tg.activeCount()];
    tg.enumerate(t);

    Arne
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Sep 1, 2007
    #3
  4. Crouchez

    Crouchez Guest

    "Arne Vajhøj" <> wrote in message
    news:46d8af97$0$90262$...
    > Crouchez wrote:
    >> Is there anyway to get the number of threads running in JVM through the
    >> java api?

    >
    > Try:
    >
    > ThreadGroup tg = Thread.currentThread().getThreadGroup();
    > while(tg.getParent() != null) {
    > tg = tg.getParent();
    > }
    > Thread[] t = new Thread[tg.activeCount()];
    > tg.enumerate(t);
    >
    > Arne


    have you tried that?
    Crouchez, Sep 1, 2007
    #4
  5. Crouchez

    Crouchez Guest

    "Knute Johnson" <> wrote in message
    news:aT1Ci.295812$...
    > Crouchez wrote:
    >> Is there anyway to get the number of threads running in JVM through the
    >> java api?

    >
    > Not that I know of but why would you care?
    >
    > --
    >
    > Knute Johnson
    > email s/nospam/knute/


    cos i want to monitor and cull if i need to
    Crouchez, Sep 1, 2007
    #5
  6. Crouchez wrote:
    > "Arne Vajhøj" <> wrote in message
    > news:46d8af97$0$90262$...
    >> Crouchez wrote:
    >>> Is there anyway to get the number of threads running in JVM through the
    >>> java api?

    >> Try:
    >>
    >> ThreadGroup tg = Thread.currentThread().getThreadGroup();
    >> while(tg.getParent() != null) {
    >> tg = tg.getParent();
    >> }
    >> Thread[] t = new Thread[tg.activeCount()];
    >> tg.enumerate(t);

    >
    > have you tried that?


    Yes.

    Arne
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Sep 1, 2007
    #6
  7. Crouchez wrote:
    > "Knute Johnson" <> wrote in message
    > news:aT1Ci.295812$...
    >> Crouchez wrote:
    >>> Is there anyway to get the number of threads running in JVM through the
    >>> java api?

    >> Not that I know of but why would you care?
    >>
    >> --
    >>
    >> Knute Johnson
    >> email s/nospam/knute/

    >
    > cos i want to monitor and cull if i need to
    >
    >


    Do you want to know the threads all the JVMs or just the one that your
    program is in?

    And how would you cull them?

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Sep 1, 2007
    #7
  8. Crouchez

    Roedy Green Guest

    On Fri, 31 Aug 2007 23:43:29 GMT, "Crouchez"
    <> wrote, quoted or indirectly
    quoted someone who said :

    >Is there anyway to get the number of threads running in JVM through the java
    >api?


    isn't there some command you can issue to the Java console to get a
    thread dump?
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Sep 1, 2007
    #8
  9. Crouchez

    Crouchez Guest

    "Roedy Green" <> wrote in message
    news:p...
    > On Fri, 31 Aug 2007 23:43:29 GMT, "Crouchez"
    > <> wrote, quoted or indirectly
    > quoted someone who said :
    >
    >>Is there anyway to get the number of threads running in JVM through the
    >>java
    >>api?

    >
    > isn't there some command you can issue to the Java console to get a
    > thread dump?
    > --
    > Roedy Green Canadian Mind Products
    > The Java Glossary
    > http://mindprod.com


    i think you could run the debugger but that'll make it seperate to the
    application.

    just out of interest how does swing run gui interaction - through events -
    but are these threads?
    Crouchez, Sep 1, 2007
    #9
  10. Crouchez

    Christian Guest

    Crouchez schrieb:
    > Is there anyway to get the number of threads running in JVM through the java
    > api?
    >
    >

    you can use Thread.getAllStackTraces().size() for that
    Christian, Sep 1, 2007
    #10
  11. Crouchez

    Lew Guest

    Crouchez wrote:
    > just out of interest how does swing run gui interaction - through events -
    > but are these threads?


    There are a variety of mechanisms, of which events and event notification are
    a part. Events are not the same as threads.

    There is one thread that rules in Swing: the Event Dispatch Thread (EDT).
    All, and that means all, GUI actions (event notifications or otherwise) occur
    in this EDT. Or should, anyway. All kinds of bugs happen when this is violated.

    Conversely, long-running non-GUI actions such as database lookups should not
    occur in the EDT, but in the main thread or some subsidiary thread. Their
    visible results get passed into the EDT for (later) viewing by one of the
    invokeLater() or SwingWorker mechanisms. Violation of this paradigm also
    causes bugs.

    Bear in mind that event notifications, method calls and so forth are not
    threads in and of themselves. They are actions that occur in threads. The
    actions and the threads that execute them are not the same thing.

    There are also data items, like instance variables, that are neither actions
    nor threads. They are "resources", things that can be shared between threads.
    These resources need protection both to prevent thread troubles like
    deadlock, and to permit communication of results between threads. The
    communication is controlled by the so-called "Java memory model", and the
    "synchronized" and "volatile" keywords are very important to managing it.

    The important thing for Swing is for GUI actions to occur only on the EDT, and
    for non-GUI actions to occur on any other thread but the EDT.

    --
    Lew
    Lew, Sep 1, 2007
    #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. Kevin Hooke

    Re: Handling both MS JVM and Sun JVM

    Kevin Hooke, Aug 26, 2003, in forum: Java
    Replies:
    2
    Views:
    861
  2. Lasse
    Replies:
    1
    Views:
    680
    Jon A. Cruz
    Jan 5, 2004
  3. Young-Jin Lee

    MS JVM and Sun JVM problem

    Young-Jin Lee, Jan 20, 2004, in forum: Java
    Replies:
    3
    Views:
    560
    Mickey Segal
    Jan 21, 2004
  4. Soeren Gerlach
    Replies:
    0
    Views:
    428
    Soeren Gerlach
    Jul 15, 2004
  5. Pedro Pinto

    Java Threads - Get running threads

    Pedro Pinto, Apr 8, 2008, in forum: Java
    Replies:
    2
    Views:
    1,396
    Arne Vajhøj
    Apr 9, 2008
Loading...

Share This Page