Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * ThreadUtil.java
 *
 * Copyright 2013 the original author or authors.
 *
 * We licenses this file to you under the Apache License, version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License.  You may obtain a copy of the License at:
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Get all the threads in this JVM.
     *
     * @return all the threads
     */
    public static final List<Thread> getAllThreads() {
        ThreadGroup topGroup = topGroup();
        return getAllThreadsInGroup(topGroup, topGroup.activeCount());
    }

    /**
     * Get the top thread group of this JVM.
     *
     * @return the top thread group
     */
    public static final ThreadGroup topGroup() {
        ThreadGroup topGroup = Thread.currentThread().getThreadGroup();
        ThreadGroup pareGroup = topGroup.getParent();
        // Iterate through the group link.
        while (pareGroup != null) {
            topGroup = pareGroup;
            pareGroup = pareGroup.getParent();
        }
        return topGroup;
    }

    /**
     * Get all the threads in this thread group.
     *
     * @param group the specified thread group
     * @param size the group size
     * @return all the threads in the specified thread group
     */
    public static final List<Thread> getAllThreadsInGroup(ThreadGroup group, int size) {
        Thread[] array = null;
        do {
            array = new Thread[size + 100];
            size = group.enumerate(array, true);
        } while (size >= array.length);
        List<Thread> ret = new ArrayList<Thread>(size);
        for (int i = 0; i < size; ++i)
            ret.add(array[i]);
        return ret;
    }
}