Example usage for java.lang ThreadGroup getName

List of usage examples for java.lang ThreadGroup getName

Introduction

In this page you can find the example usage for java.lang ThreadGroup getName.

Prototype

public final String getName() 

Source Link

Document

Returns the name of this thread group.

Usage

From source file:ThreadLister.java

/** Display info about a thread group */
private static void printGroupInfo(ThreadGroup g, String indent) {
    if (g == null)
        return;/*w  w w.j  a v  a2s. co  m*/
    int numThreads = g.activeCount();
    int numGroups = g.activeGroupCount();
    Thread[] threads = new Thread[numThreads];
    ThreadGroup[] groups = new ThreadGroup[numGroups];

    g.enumerate(threads, false);
    g.enumerate(groups, false);

    System.out.println(indent + "Thread Group: " + g.getName() + "  Max Priority: " + g.getMaxPriority()
            + (g.isDaemon() ? " Daemon" : ""));

    for (int i = 0; i < numThreads; i++)
        printThreadInfo(threads[i], indent + "    ");
    for (int i = 0; i < numGroups; i++)
        printGroupInfo(groups[i], indent + "    ");
}

From source file:ThreadLister.java

/** Display info about a thread group and its threads and groups */
private static void printGroupInfo(PrintWriter out, ThreadGroup g, String indent) {
    if (g == null)
        return;/*from w w  w.j a v  a2s .  c  o m*/
    int num_threads = g.activeCount();
    int num_groups = g.activeGroupCount();
    Thread[] threads = new Thread[num_threads];
    ThreadGroup[] groups = new ThreadGroup[num_groups];

    g.enumerate(threads, false);
    g.enumerate(groups, false);

    out.println(indent + "Thread Group: " + g.getName() + "  Max Priority: " + g.getMaxPriority()
            + (g.isDaemon() ? " Daemon" : ""));

    for (int i = 0; i < num_threads; i++)
        printThreadInfo(out, threads[i], indent + "    ");
    for (int i = 0; i < num_groups; i++)
        printGroupInfo(out, groups[i], indent + "    ");
}

From source file:Main.java

/**
 * Display info about a thread group and its threads and groups
 *///  w  w  w  .  jav  a 2 s .  c  o  m
private static void printGroupInfo(PrintWriter out, ThreadGroup g, String indent) {
    if (g == null) {
        return;
    }
    int num_threads = g.activeCount();
    int num_groups = g.activeGroupCount();
    Thread[] threads = new Thread[num_threads];
    ThreadGroup[] groups = new ThreadGroup[num_groups];

    g.enumerate(threads, false);
    g.enumerate(groups, false);

    out.println(indent + "Thread Group: " + g.getName() + "  Max Priority: " + g.getMaxPriority()
            + (g.isDaemon() ? " Daemon" : ""));

    for (int i = 0; i < num_threads; i++) {
        printThreadInfo(out, threads[i], indent + "    ");
    }
    for (int i = 0; i < num_groups; i++) {
        printGroupInfo(out, groups[i], indent + "    ");
    }
}

From source file:Main.java

/** Collect info about a thread group */
private static void collectThreadGroupInfo(final ThreadGroup g, final StringBuffer str, final String indent) {
    if (g == null) {
        return;//from   w  ww.j  ava 2  s.c o  m
    }
    int numThreads = g.activeCount();
    int numGroups = g.activeGroupCount();
    final Thread[] threads = new Thread[numThreads * 2];
    final ThreadGroup[] groups = new ThreadGroup[numGroups * 2];

    numThreads = g.enumerate(threads, false);
    numGroups = g.enumerate(groups, false);

    str.append(indent).append("Thread Group: [").append(g.getName()).append("]  Max Priority: [")
            .append(g.getMaxPriority()).append(g.isDaemon() ? " Daemon" : "").append("]")
            .append(LINE_SEPARATOR);

    for (int i = 0; i < numThreads; i++) {
        if (threads[i] != null) {
            collectThreadInfo(threads[i], str, indent + "    ");
        }
    }
    for (int i = 0; i < numGroups; i++) {
        collectThreadGroupInfo(groups[i], str, indent + "    ");
    }
}

From source file:com.gigaspaces.internal.utils.ClassLoaderCleaner.java

@SuppressWarnings("deprecation")
private static void clearReferencesThreads(ClassLoader classLoader) {
    Thread[] threads = getThreads();

    // Iterate over the set of threads
    for (Thread thread : threads) {
        if (thread != null) {
            ClassLoader ccl = thread.getContextClassLoader();
            if (ccl != null && ccl == classLoader) {
                // Don't warn about this thread
                if (thread == Thread.currentThread()) {
                    continue;
                }//from w  w  w .  jav a2s.  c om

                // Skip threads that have already died
                if (!thread.isAlive()) {
                    continue;
                }

                // Don't warn about JVM controlled threads
                ThreadGroup tg = thread.getThreadGroup();
                if (tg != null && JVM_THREAD_GROUP_NAMES.contains(tg.getName())) {
                    continue;
                }

                // TimerThread is not normally visible
                if (thread.getClass().getName().equals("java.util.TimerThread")) {
                    clearReferencesStopTimerThread(thread);
                    continue;
                }

                if (logger.isLoggable(Level.FINE))
                    logger.fine("A thread named [" + thread.getName()
                            + "] started but has failed to stop it. This is very likely to create a memory leak.");

                // Don't try an stop the threads unless explicitly
                // configured to do so
                if (!clearReferencesStopThreads) {
                    continue;
                }

                // If the thread has been started via an executor, try
                // shutting down the executor
                try {
                    Field targetField = thread.getClass().getDeclaredField("target");
                    targetField.setAccessible(true);
                    Object target = targetField.get(thread);

                    if (target != null && target.getClass().getCanonicalName()
                            .equals("java.util.concurrent.ThreadPoolExecutor.Worker")) {
                        Field executorField = target.getClass().getDeclaredField("this$0");
                        executorField.setAccessible(true);
                        Object executor = executorField.get(target);
                        if (executor instanceof ThreadPoolExecutor) {
                            ((ThreadPoolExecutor) executor).shutdownNow();
                        }
                    }
                } catch (Exception e) {
                    logger.log(Level.WARNING, "Failed to terminate thread named [" + thread.getName() + "]", e);
                }

                // This method is deprecated and for good reason. This is
                // very risky code but is the only option at this point.
                // A *very* good reason for apps to do this clean-up
                // themselves.
                thread.stop();
            }
        }
    }
}

From source file:org.springframework.data.hadoop.mapreduce.ExecutionUtils.java

private static void replaceTccl(ClassLoader leakedClassLoader, ClassLoader replacementClassLoader) {
    for (Thread thread : threads()) {
        if (thread != null) {
            ClassLoader cl = thread.getContextClassLoader();
            // do identity check to prevent expensive (and potentially dangerous) equals()
            if (leakedClassLoader == cl) {
                log.warn("Trying to patch leaked cl [" + leakedClassLoader + "] in thread [" + thread + "]");
                ThreadGroup tg = thread.getThreadGroup();
                // it's a JVM thread so use the System ClassLoader always
                boolean debug = log.isDebugEnabled();
                if (tg != null && JVM_THREAD_NAMES.contains(tg.getName())) {
                    thread.setContextClassLoader(ClassLoader.getSystemClassLoader());
                    if (debug) {
                        log.debug("Replaced leaked cl in thread [" + thread + "] with system classloader");
                    }/*w w  w. ja  v a 2 s  . com*/
                } else {
                    thread.setContextClassLoader(replacementClassLoader);
                    if (debug) {
                        log.debug(
                                "Replaced leaked cl in thread [" + thread + "] with " + replacementClassLoader);
                    }
                }
            }
        }
    }
}

From source file:ThreadGroupDemo1.java

public void run() {
    ThreadGroup tg = getThreadGroup();
    System.out.println(tg.getName());
}

From source file:org.openmrs.util.OpenmrsClassLoader.java

private static List<Thread> listThreads(ThreadGroup group, String indent) {
    List<Thread> threadToReturn = new ArrayList<Thread>();

    log.error(indent + "Group[" + group.getName() + ":" + group.getClass() + "]");
    int nt = group.activeCount();
    Thread[] threads = new Thread[nt * 2 + 10]; //nt is not accurate
    nt = group.enumerate(threads, false);

    // List every thread in the group
    for (int i = 0; i < nt; i++) {
        Thread t = threads[i];//from w  ww .  j  ava  2  s. c  o m
        log.error(indent + "  Thread[" + t.getName() + ":" + t.getClass() + ":"
                + (t.getContextClassLoader() == null ? "null cl"
                        : t.getContextClassLoader().getClass().getName() + " "
                                + t.getContextClassLoader().hashCode())
                + "]");
        threadToReturn.add(t);
    }

    // Recursively list all subgroups
    int ng = group.activeGroupCount();
    ThreadGroup[] groups = new ThreadGroup[ng * 2 + 10];
    ng = group.enumerate(groups, false);

    for (int i = 0; i < ng; i++) {
        threadToReturn.addAll(listThreads(groups[i], indent + "  "));
    }

    return threadToReturn;
}

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

public String getThreadDumpString() {
    Thread[] threads = new Thread[Thread.activeCount()];
    Thread.enumerate(threads);/*from  ww  w.j av a2 s .  c  om*/

    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");
        }

        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.brienwheeler.lib.concurrent.NamedThreadFactory.java

public String getName() {
    ThreadGroup threadGroup = this.threadGroup.get();
    if (threadGroup == null)
        throw new IllegalStateException(
                "not allowed to call getName() before calling setName() on " + getClass().getSimpleName());
    return threadGroup.getName();
}