Get number of threads in JVM

C

Crouchez

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

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Crouchez said:
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
 
C

Crouchez

Arne Vajhøj said:
Crouchez said:
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?
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Crouchez said:
Arne Vajhøj said:
Crouchez said:
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
 
K

Knute Johnson

Crouchez said:
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?
 
R

Roedy Green

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

Crouchez

Roedy Green said:
isn't there some command you can issue to the Java console to get a
thread dump?

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

Lew

Crouchez said:
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.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top