Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * Copyright (c) 2011-2014 INRIA.
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 * 
 * This program 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 General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>
 **/

import java.lang.Thread.State;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
import java.util.Arrays;

public class Main {
    /**
     * Returns the number of threads that are in state {@link State#RUNNABLE} in
     * the current JVM.
     * 
     * @return the number of threads that are in state {@link State#RUNNABLE} in
     *         the current JVM.
     */
    public static int countActive() {
        return countActive(getAllThreads());
    }

    /**
     * Returns the number of threads that are in state {@link State#RUNNABLE} in
     * in the specified array of threads..
     * 
     * @return the number of threads that are in state {@link State#RUNNABLE} in
     *         the specified array of threads.
     */
    public static int countActive(Thread[] threads) {
        int result = 0;

        for (Thread t : threads) {
            if (t.getState() == State.RUNNABLE) {
                result++;
            }
        }

        return result;
    }

    /**
     * Returns all threads that have a name matching the specified pattern name.
     * 
     * @param threadNamePattern
     *            regex to match.
     * 
     * @return all threads that have a name matching the specified pattern name.
     */
    public static Thread[] getAllThreads(String threadNamePattern) {
        Thread[] threads = getAllThreads();

        Thread[] filteredThreads = new Thread[threads.length];

        int i = 0;
        for (Thread t : threads) {
            if (t.getName().matches(threadNamePattern)) {
                filteredThreads[i] = t;
                i++;
            }
        }

        return Arrays.copyOfRange(filteredThreads, 0, i);
    }

    public static Thread[] getAllThreads() {
        final ThreadGroup root = getRootThreadGroup();
        final ThreadMXBean thbean = ManagementFactory.getThreadMXBean();

        int nAlloc = thbean.getThreadCount();
        int n = 0;

        Thread[] threads;
        do {
            nAlloc *= 2;
            threads = new Thread[nAlloc];
            n = root.enumerate(threads, true);
        } while (n == nAlloc);

        return java.util.Arrays.copyOf(threads, n);
    }

    private static ThreadGroup getRootThreadGroup() {
        ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
        ThreadGroup parentThreadGroup;

        while ((parentThreadGroup = threadGroup.getParent()) != null) {
            threadGroup = parentThreadGroup;
        }

        return threadGroup;
    }
}