Java how to get all threads currently running

htt‮w//:sp‬ww.lautturi.com
Java how to get all threads currently running
/**
 * @author lautturi.com 
 * Java example: how to get a list of all running threads in java
 */

import java.util.*;

public class Lautturi {

	public static void main(String[] args) {

		Set<Thread> threadSet = Thread.getAllStackTraces().keySet();

		Thread[] arr = threadSet.toArray(new Thread[threadSet.size()]);
		
		for (Thread thread : arr) {
			System.out.println(thread.getId()+" "+thread.getName());
		}
	}
}

output:

3 Finalizer
5 Attach Listener
1 main
4 Signal Dispatcher
2 Reference Handler
11 Common-Cleaner
Created Time:2017-09-25 00:35:14  Author:lautturi