import javax.imageio.*; import java.awt.image.*; import java.awt.event.*; import java.awt.*; import java.security.NoSuchAlgorithmException; import java.security.MessageDigest; import java.lang.management.*; import java.lang.reflect.*; import java.net.*; import java.io.*; import javax.swing.text.*; import javax.swing.event.*; import javax.swing.*; import java.util.concurrent.atomic.*; import java.util.concurrent.*; import java.util.regex.*; import java.util.List; import java.util.zip.*; import java.util.*; public class main { static Map idToThread; static WeakHashMap infoMap = new WeakHashMap(); static class Info { long cpuTime, measuredWhen; // all in milliseconds double percentage; // percentage of CPU used } static ThreadMXBean mx; static boolean cpuTimeSupported; static double totalPercentage; // all threads combined public static void main(String[] args) throws Exception { mx = ManagementFactory.getThreadMXBean(); cpuTimeSupported = mx.isThreadCpuTimeSupported(); if (!cpuTimeSupported) fail("No CPU time info available on platform."); while (true) { try { monitor(); printMostActiveThread(); } catch (Throwable __e) { __e.printStackTrace(); } sleepSeconds(1); } } static void monitor() { List threads = getAllThreads(); WeakHashMap newInfoMap = new WeakHashMap(); totalPercentage = 0; for (Thread t : threads) { Info info = infoMap.get(t); if (info == null) info = new Info(); newInfoMap.put(t, info); long nano = 0; try { nano = mx.getThreadCpuTime(t.getId()); } catch (Throwable __e) { __e.printStackTrace(); } long measured = now(); long ms = nano/1000000; if (info.measuredWhen != 0) { long elapsed = measured - info.measuredWhen; long used = ms-info.cpuTime; info.percentage = Math.max(0, Math.min(100, used*100.0/elapsed)); totalPercentage += info.percentage; } info.measuredWhen = measured; info.cpuTime = ms; } infoMap = newInfoMap; } static void printMostActiveThread() { Thread mostActive = null; double highest = 0.0; for (Map.Entry e : infoMap.entrySet()) { Thread t = e.getKey(); Info info = e.getValue(); if (mostActive == null || info.percentage > highest) { mostActive = t; highest = info.percentage; } } if (mostActive == null) print("No info"); else print("Total CPU used by this VM: " + formatDouble(totalPercentage, 1) + "%. Most active thread: " + mostActive.getName() + " (" + formatDouble(highest, 1) + "% CPU)"); } static List getAllThreads() { ThreadGroup rootGroup = Thread.currentThread().getThreadGroup(); ThreadGroup parentGroup; while ( ( parentGroup = rootGroup.getParent() ) != null ) rootGroup = parentGroup; Thread[] threads = new Thread[rootGroup.activeCount()]; int n; while ((n = rootGroup.enumerate(threads, true)) == threads.length) threads = new Thread[threads.length*2]; List l = new ArrayList(n); for (int i = 0; i < n; i++) l.add(threads[i]); return l; } static Throwable pcall(Runnable r) { try { r.run(); return null; } catch (Throwable e) { return e; } } static void sleepSeconds(long s) { if (s > 0) sleep(s*1000); } static long now_virtualTime; static long now() { return now_virtualTime != 0 ? now_virtualTime : System.currentTimeMillis(); } static void print() { System.out.println(); } static void print(Object o) { System.out.println(o); } static void print(long i) { System.out.println(i); } public static String formatDouble(double d, int digits) { String format = "0."; for (int i = 0; i < digits; i++) format += "#"; return new java.text.DecimalFormat(format, new java.text.DecimalFormatSymbols(Locale.ENGLISH)).format(d); } static RuntimeException fail() { throw new RuntimeException("fail"); } static RuntimeException fail(Object msg) { throw new RuntimeException(String.valueOf(msg)); } static void sleep(long ms) { try { Thread.sleep(ms); } catch (Exception e) { throw new RuntimeException(e); } } static void sleep() { try { synchronized(main.class) { main.class.wait(); } } catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }} }