Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Wiki
Search Coderanch
Advance search
Google search
Register / Login
Bookmark Topic
Watch Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forums:
Wiki
Threads and Synchronization
Thread Lister
posted 8 years ago
1
Number of slices to send:
Optional 'thank-you' note:
Send
This class lists all the currently active threads in a JVM. Note how the number of JVM-internal threads increases once GUI objects are used. If the code is part of a server application (like a web app), there will be even more threads.
public class ThreadLister { public static void main (String[] args) { listThreads(); new javax.swing.JFrame(); listThreads(); System.exit(0); } private static void listThreads() { // Get the top level thread group ThreadGroup adam = getAdam(Thread.currentThread().getThreadGroup()); // Get all of the active threads in the system Thread list[] = new Thread[adam.activeCount()]; int numThreads = adam.enumerate(list); // Display all of the active threads in the system for (Thread th : list) { // a thread object can be null because the thread may // have terminated since we enumerated all threads if (th == null) continue; System.out.println("group:" + th.getThreadGroup().getName() + ", thread:" + th.getName() + ", priority:" + th.getPriority() + ", daemon:" + (th.isDaemon() ? "Yes" : "No") + ", alive:" + (th.isAlive() ? "Yes" : "No")); } System.out.println(); } // Recursively calls itself to find the top level thread group private static ThreadGroup getAdam (ThreadGroup tg) { ThreadGroup parent = tg.getParent(); if (parent == null) return tg; return getAdam(parent); } }
CodeBarn
CategoryCodeSamples
Look! I laid an egg! Why does it smell like that? Tiny ad, does this smell weird to you?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
status of deamon threads
what's running on my VM?
java threads
ThreadGroup .activeCount()
Thread Null pointer problem
More...