Java tutorial
//package com.java2s; /** * Copyright 2008, David Robert Nadeau, NadeauSoftware.com * * This file is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation, either version 2 of * the License, or (at your option) any later version. * * This file is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this file. If not, see * <a href="http://www.gnu.org/licenses">GNU Licenses</a>. */ import java.lang.management.*; public class Main { /** * The root thread group saved on the first search for it. * The root group doesn't change for the life of the JVM, * so once found there is no need to find it again. */ private static ThreadGroup rootThreadGroup = null; /** * Get the thread who's lock is blocking the given thread. * A null is returned if there is no such thread. * * @param blockedThread the blocked thread * @return the blocking thread, or null if * there is none * @throws NullPointerException * if the blocked thread is null */ public static Thread getBlockingThread(final Thread blockedThread) { final ThreadInfo info = getThreadInfo(blockedThread); if (info == null) return null; final long id = info.getLockOwnerId(); if (id == -1) return null; return getThread(id); } /** * Get the thread info for the thread with the given name. * A null is returned if no such thread info is found. * If more than one thread has the same name, the thread * info for the first one found is returned. * * @param name the thread name to search for * @return the thread info, or null if not found * @throws NullPointerException * if the name is null */ public static ThreadInfo getThreadInfo(final String name) { if (name == null) throw new NullPointerException("Null name"); final Thread[] threads = getAllThreads(); for (Thread thread : threads) if (thread.getName().equals(name)) return getThreadInfo(thread.getId()); return null; } /** * Get the thread info for the thread with the given ID. * A null is returned if no such thread info is found. * * @param id the thread ID to search for * @return the thread info, or null if not found * @throws IllegalArgumentException * if id <= 0 */ public static ThreadInfo getThreadInfo(final long id) { final ThreadMXBean thbean = ManagementFactory.getThreadMXBean(); // Get thread info with lock info, when available. if (!thbean.isObjectMonitorUsageSupported() || !thbean.isSynchronizerUsageSupported()) return thbean.getThreadInfo(id); final ThreadInfo[] infos = thbean.getThreadInfo(new long[] { id }, true, true); if (infos.length == 0) return null; return infos[0]; } /** * Get the thread info for the given thread. A null is * returned if the thread info cannot be found. * * @param thread the thread to search for * @return the thread info, or null if not found * @throws NullPointerException * if thread is null */ public static ThreadInfo getThreadInfo(final Thread thread) { if (thread == null) throw new NullPointerException("Null thread"); return getThreadInfo(thread.getId()); } /** * Get the thread with the given name. A null is returned * if no such thread is found. If more than one thread has * the same name, the first one found is returned. * * @param name the thread name to search for * @return the thread, or null if not found * @throws NullPointerException * if the name is null */ public static Thread getThread(final String name) { if (name == null) throw new NullPointerException("Null name"); final Thread[] threads = getAllThreads(); for (Thread thread : threads) if (thread.getName().equals(name)) return thread; return null; } /** * Get the thread with the given ID. A null is returned * if no such thread is found. * * @param id the thread ID to search for * @return the thread, or null if not found */ public static Thread getThread(final long id) { final Thread[] threads = getAllThreads(); for (Thread thread : threads) if (thread.getId() == id) return thread; return null; } /** * Get the thread with the given ID and Name. A null is returned * if no such thread is found. * * @param id the thread ID to search for * @param name the thread name to search for * @return the thread, or null if not found */ public static Thread getThread(final long id, final String name) { final Thread[] threads = getAllThreads(); for (Thread thread : threads) if ((thread.getId() == id) && (thread.getName().equals(name))) return thread; return null; } /** * Get the thread for the given thread info. A null * is returned if the thread cannot be found. * * @param info the thread info to search for * @return the thread, or null if not found * @throws NullPointerException * if info is null */ public static Thread getThread(final ThreadInfo info) { if (info == null) throw new NullPointerException("Null info"); return getThread(info.getThreadId()); } /** * Get a list of all threads. Since there is always at * least one thread, this method never returns null or * an empty array. * * @return an array of threads */ public static Thread[] getAllThreads() { final ThreadGroup root = getRootThreadGroup(); final ThreadMXBean thbean = ManagementFactory.getThreadMXBean(); int nAlloc = thbean.getThreadCount(); int n = 0; Thread[] threads = null; do { nAlloc *= 2; threads = new Thread[nAlloc]; n = root.enumerate(threads, true); } while (n == nAlloc); return java.util.Arrays.copyOf(threads, n); } /** * Get a list of all threads with a given thread state. * Thread states are defined in the Thread.State enum for * the Thread class. Principal thread states include * RUNNABLE, WAITING, TIMED_WAITING, and BLOCKED. An * empty array is returned if there are no threads in * the chosen state. * * @param state the state to look for * @return an array of threads in that state */ public static Thread[] getAllThreads(final Thread.State state) { final Thread[] allThreads = getAllThreads(); final Thread[] found = new Thread[allThreads.length]; int nFound = 0; for (Thread thread : allThreads) if (thread.getState() == state) found[nFound++] = thread; return java.util.Arrays.copyOf(found, nFound); } /** * Get the root thread group in the thread group tree. * Since there is always a root thread group, this * method never returns null. * * @return the root thread group */ public static ThreadGroup getRootThreadGroup() { if (rootThreadGroup != null) return rootThreadGroup; ThreadGroup tg = Thread.currentThread().getThreadGroup(); ThreadGroup ptg; while ((ptg = tg.getParent()) != null) tg = ptg; rootThreadGroup = tg; return tg; } }