Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know how many threads have been created and running?

Tags:

This is my simple program in Java:

public class Counter extends Thread {      public static void main(String args[]) {             Thread t1 = new Thread();         Thread t2 = new Thread();         t1.start();         t2.start();     } } 

I am using Windows Operating System 32-bit. My question is, how can we know how many Threads are created in the program and how many Threads are running? Is there any such tool?

like image 322
Pawan Avatar asked Apr 01 '12 09:04

Pawan


People also ask

How can I tell how many threads are running?

Open Task Manager (press Ctrl+Shift+Esc) Select Performance tab. Look for Cores and Logical Processors (Threads)

How many threads are running in Java?

The Java thread lifecycle consists of six thread states: New: A new Thread() has been instantiated. Runnable: The Thread 's start() method has been invoked.

How many threads run at once?

Each core can only run 1 thread at a time, i.e. hyperthreading is disabled. So, you can have a total maximum of 20 threads executing in parallel, one thread per CPU/core.


2 Answers

System.out.println("Number of active threads from the given thread: " + Thread.activeCount());

like image 91
dexametason Avatar answered Oct 27 '22 06:10

dexametason


Thread.getAllStackTraces() will give you a map where each Thread is key. You can then examine the state of each Thread and check thread.isAlive().

Map<Thread, StackTraceElement[]> threads = Thread.getAllStackTraces(); 
like image 21
Andrejs Avatar answered Oct 27 '22 08:10

Andrejs