Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

Get number of threads in JVM

Thread view: 
Crouchez - 01 Sep 2007 00:43 GMT
Is there anyway to get the number of threads running in JVM through the java
api?
Knute Johnson - 01 Sep 2007 00:54 GMT
> 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?

Signature

Knute Johnson
email s/nospam/knute/

Crouchez - 01 Sep 2007 01:29 GMT
>> 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?

cos i want to monitor and cull if i need to
Knute Johnson - 01 Sep 2007 05:36 GMT
>>> 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?
>
> 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?

Signature

Knute Johnson
email s/nospam/knute/

Arne Vajhøj - 01 Sep 2007 01:17 GMT
> 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
Crouchez - 01 Sep 2007 01:29 GMT
>> Is there anyway to get the number of threads running in JVM through the
>> java api?
[quoted text clipped - 9 lines]
>
> Arne

have you tried that?
Arne Vajhøj - 01 Sep 2007 01:51 GMT
>>> Is there anyway to get the number of threads running in JVM through the
>>> java api?
[quoted text clipped - 8 lines]
>
> have you tried that?

Yes.

Arne
Roedy Green - 01 Sep 2007 12:44 GMT
On Fri, 31 Aug 2007 23:43:29 GMT, "Crouchez"
<blah@bllllllahblllbllahblahblahhh.com> 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?
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Crouchez - 01 Sep 2007 20:46 GMT
> On Fri, 31 Aug 2007 23:43:29 GMT, "Crouchez"
> <blah@bllllllahblllbllahblahblahhh.com> wrote, quoted or indirectly
[quoted text clipped - 6 lines]
> 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?
Lew - 01 Sep 2007 21:27 GMT
> 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.

Signature

Lew

Christian - 01 Sep 2007 21:21 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.