Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright (c) 2009-2013 GreenVulcano ESB Open Source Project. All rights
 * reserved.
 * 
 * This file is part of GreenVulcano ESB.
 * 
 * GreenVulcano ESB 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 3 of the License, or (at your
 * option) any later version.
 * 
 * GreenVulcano ESB 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 GreenVulcano ESB. If not, see <http://www.gnu.org/licenses/>.
 */

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 group with the given name. A null is returned if no such
     * group is found. If more than one group has the same name, the first one
     * found is returned.
     * 
     * @param name
     *        the thread group name to search for
     * @return the thread group, or null if not found
     * @throws NullPointerException
     *         if the name is null
     */
    public static ThreadGroup getThreadGroup(final String name) {
        if (name == null) {
            throw new NullPointerException("Null name");
        }
        final ThreadGroup[] groups = getAllThreadGroups();
        for (ThreadGroup group : groups) {
            if (group.getName().equals(name))
                return group;
        }
        return null;
    }

    /**
     * Get a list of all thread groups. Since there is always at least one
     * thread group (the root), this method never returns a null or empty array.
     * 
     * @return an array of thread groups
     */
    public static ThreadGroup[] getAllThreadGroups() {
        final ThreadGroup root = getRootThreadGroup();
        int nAlloc = root.activeGroupCount();
        int n = 0;
        ThreadGroup[] groups = null;
        do {
            nAlloc *= 2;
            groups = new ThreadGroup[nAlloc];
            n = root.enumerate(groups, true);
        } while (n == nAlloc);
        ThreadGroup[] allGroups = new ThreadGroup[n + 1];
        allGroups[0] = root;
        System.arraycopy(groups, 0, allGroups, 1, n);
        return allGroups;
    }

    /**
     * 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;
    }
}