Example usage for java.lang Thread enumerate

List of usage examples for java.lang Thread enumerate

Introduction

In this page you can find the example usage for java.lang Thread enumerate.

Prototype

public static int enumerate(Thread tarray[]) 

Source Link

Document

Copies into the specified array every active thread in the current thread's thread group and its subgroups.

Usage

From source file:Main.java

public static void main(String[] args) {
    Thread t = Thread.currentThread();
    t.setName("java2s.com thread");

    System.out.println("Thread = " + t);

    int count = Thread.activeCount();
    System.out.println("currently active threads = " + count);

    Thread th[] = new Thread[count];
    Thread.enumerate(th);

    for (int i = 0; i < count; i++) {
        System.out.println(i + ": " + th[i]);
    }/*from w w  w . java 2s. c o m*/
}

From source file:MainClass.java

public static void main(String args[]) {
    Thread t = Thread.currentThread();
    t.setName("My Thread");
    t.setPriority(1);/*from   ww  w  .j  a  va  2 s.co m*/
    System.out.println("current thread: " + t);
    int active = Thread.activeCount();
    System.out.println("currently active threads: " + active);
    Thread all[] = new Thread[active];
    Thread.enumerate(all);
    for (int i = 0; i < active; i++) {
        System.out.println(i + ": " + all[i]);
    }
    Thread.dumpStack();
}

From source file:Main.java

public static String getAllThreadNames() {
    Thread[] array = new Thread[Thread.activeCount()];

    Thread.enumerate(array);

    String s = "";
    for (Thread t : array) {
        if (t != null) {
            s += t.getName() + ", ";
        }//from   w  w  w  .ja v a2 s. c  o  m
    }
    return s;
}

From source file:Main.java

private static int countThreads(String namePattern) {
    Thread[] list = new Thread[100];
    Thread.enumerate(list);
    int matches = 0;
    for (Thread element : list) {
        if (element != null) {
            if (element.getName().matches(namePattern) || element.getClass().getName().matches(namePattern)) {
                matches++;//w w  w  .  j  a v  a  2s  .c om
            }
        }
    }
    return matches;
}

From source file:Main.java

/**
 * @param name/*  www . ja  v a 2 s  .c  o  m*/
 * @return list of threads
 */
public static List<Thread> listThreadGroup(String name) {
    List<Thread> threads = new LinkedList<Thread>();
    ThreadGroup threadGroup = getThreadGroupByName(name, null);
    if (threadGroup != null) {
        int count = threadGroup.activeCount();
        Thread threadArray[] = new Thread[count];
        int threadCount = Thread.enumerate(threadArray);
        for (int index = 0; index < threadCount; index++) {
            threads.add(threadArray[index]);
        }
    }
    return threads;
}

From source file:com.nubits.nubot.utils.Utils.java

public static void logActiveThreads() {
    int active = Thread.activeCount();
    LOG.trace("currently active threads: " + active);
    Thread allThreads[] = new Thread[active];
    Thread.enumerate(allThreads);

    for (int i = 0; i < active; i++) {
        Thread t = allThreads[i];
        LOG.trace(i + ": " + t + " id: " + t.getId() + " name: " + t.getName() + " " + t.getContextClassLoader()
                + " group: " + t.getThreadGroup() + " alive" + t.isAlive());
        LOG.trace("super: " + t.getClass().getSuperclass());
    }/*w  ww . j  a  v a2  s  .  c  om*/

    if (active > maxThreadsError) {
        LOG.error("too many threads started");
    }
}

From source file:org.commonjava.indy.diag.data.DiagnosticsManager.java

public String getThreadDumpString() {
    Thread[] threads = new Thread[Thread.activeCount()];
    Thread.enumerate(threads);

    Map<Long, Thread> threadMap = new HashMap<>();
    Stream.of(threads).forEach(t -> threadMap.put(t.getId(), t));
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds(), 100);

    StringBuilder sb = new StringBuilder();
    Stream.of(threadInfos).forEachOrdered((ti) -> {
        if (sb.length() > 0) {
            sb.append("\n\n");
        }/*from  w ww.j  a v  a 2s  .c o  m*/

        String threadGroup = "Unknown";
        Thread t = threadMap.get(ti.getThreadId());
        if (t != null) {
            ThreadGroup tg = t.getThreadGroup();
            if (tg != null) {
                threadGroup = tg.getName();
            }
        }

        sb.append(ti.getThreadName()).append("\n  Group: ").append(threadGroup).append("\n  State: ")
                .append(ti.getThreadState()).append("\n  Lock Info: ").append(ti.getLockInfo())
                .append("\n  Monitors:");

        MonitorInfo[] monitors = ti.getLockedMonitors();
        if (monitors == null || monitors.length < 1) {
            sb.append("  -NONE-");
        } else {
            sb.append("\n  - ").append(join(monitors, "\n  - "));
        }

        sb.append("\n  Trace:\n    ").append(join(ti.getStackTrace(), "\n    "));

    });

    return sb.toString();
}

From source file:com.myJava.util.Util.java

public static void logAllThreadInformations() {
    int nb = Thread.activeCount();
    Thread[] th = new Thread[nb + 100];
    Thread.enumerate(th);
    boolean stop = false;
    Logger.defaultLogger().info("Thread information : " + nb + " threads.");
    for (int i = 0; i < th.length && !stop; i++) {
        if (th[i] == null) {
            stop = true;/*from   w  w  w  . ja v a 2  s .c  om*/
        } else {
            String header = "Thread " + i + " (" + th[i].getName() + " / " + th[i].getId() + ")";
            logThreadInformations(header, th[i]);
        }
    }
}

From source file:org.apache.bookkeeper.test.ZooKeeperUtil.java

public void sleepServer(final int seconds, final CountDownLatch l) throws InterruptedException, IOException {
    Thread[] allthreads = new Thread[Thread.activeCount()];
    Thread.enumerate(allthreads);
    for (final Thread t : allthreads) {
        if (t.getName().contains("SyncThread:0")) {
            Thread sleeper = new Thread() {
                public void run() {
                    try {
                        t.suspend();//w  w w.  j a  v  a 2 s  .  c  o  m
                        l.countDown();
                        Thread.sleep(seconds * 1000);
                        t.resume();
                    } catch (Exception e) {
                        LOG.error("Error suspending thread", e);
                    }
                }
            };
            sleeper.start();
            return;
        }
    }
    throw new IOException("ZooKeeper thread not found");
}

From source file:org.dacapo.harness.Tomcat.java

@SuppressWarnings("unused")
private void dumpThreads() {
    ThreadGroup tg = Thread.currentThread().getThreadGroup();
    int nThreads = tg.activeCount();
    Thread[] threads = new Thread[nThreads * 2];
    nThreads = Thread.enumerate(threads);
    System.out.printf("==================== Dumping %d Threads: ====================%n", nThreads);
    System.out.flush();//ww w  .j av a2 s .c o  m
    for (int i = 0; i < nThreads; i++) {
        if (threads[i] != null) {
            System.out.print(threads[i].getName() + ": ");
            StackTraceElement[] stack = threads[i].getStackTrace();
            for (int j = 0; j < stack.length; j++) {
                for (int k = 0; k < j; k++)
                    System.out.print("  ");
                System.out.println(stack[j].getClassName() + "." + stack[j].getMethodName() + ":"
                        + stack[j].getLineNumber() + " <- ");
            }
        } else {
            System.out.print("null ");
        }
        System.out.flush();
    }
    System.out.println();
    System.out.flush();
    System.out.printf("==================== Thread Dump End ====================%n");
}