Example usage for java.lang Thread getThreadGroup

List of usage examples for java.lang Thread getThreadGroup

Introduction

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

Prototype

public final ThreadGroup getThreadGroup() 

Source Link

Document

Returns the thread group to which this thread belongs.

Usage

From source file:Main.java

public static void main(String[] args) {
    Thread t1 = Thread.currentThread();

    ThreadGroup tg1 = t1.getThreadGroup();

    System.out.println("Current thread's  name:  " + t1.getName());
    System.out.println("Current thread's  group  name:  " + tg1.getName());

    Thread t2 = new Thread("my  new thread");

    ThreadGroup tg2 = t2.getThreadGroup();
    System.out.println("New  thread's name:  " + t2.getName());
    System.out.println("New  thread's  group  name:  " + tg2.getName());
}

From source file:Main.java

private static String getThreadGroupName(Thread thread) {
    ThreadGroup threadGroup = thread.getThreadGroup();
    return (threadGroup == null) ? "stopped" : threadGroup.getName();
}

From source file:Main.java

public static boolean isThreadInGroup(Thread thread, ThreadGroup group) {
    return thread.getThreadGroup() == group;
}

From source file:Main.java

public static Thread[] getThreads() {
    Thread currentThread = Thread.currentThread();

    ThreadGroup threadGroup = currentThread.getThreadGroup();

    while (threadGroup.getParent() != null) {
        threadGroup = threadGroup.getParent();
    }/*from   w ww.j ava  2  s. c  o  m*/

    int threadCountGuess = threadGroup.activeCount();

    Thread[] threads = new Thread[threadCountGuess];

    int threadCountActual = threadGroup.enumerate(threads);

    while (threadCountActual == threadCountGuess) {
        threadCountGuess *= 2;

        threads = new Thread[threadCountGuess];

        threadCountActual = threadGroup.enumerate(threads);
    }

    return threads;
}

From source file:Main.java

public static String getThreadSignature() {
    Thread t = Thread.currentThread();
    long l = t.getId();
    String name = t.getName();/* w  ww  .j a va2  s  .c o m*/
    long p = t.getPriority();
    String gname = t.getThreadGroup().getName();
    return (name + ":(id)" + l + ":(priority)" + p + ":(group)" + gname);
}

From source file:Main.java

public static String getThreadSignature() {
    Thread t = Thread.currentThread();
    long id = t.getId();
    String name = t.getName();/*from   ww w .j a  v  a2  s.c  o m*/
    long pri = t.getPriority();
    String gname = t.getThreadGroup().getName();
    return (name + ":(id)" + id + ":(priority)" + pri + ":(group)" + gname);
}

From source file:Main.java

/**
 * Print a stack trace of the current thread.
 */// ww w .  ja v a 2 s . co m
public static void printFullStackTrace() {
    Thread thread = Thread.currentThread();
    System.out.printf("  Thread id: %d, name: %s, state: %s, daemon: %s, EDT: %s\n", thread.getId(),
            thread.getName(), thread.getState(), thread.isDaemon(), EventQueue.isDispatchThread());
    ThreadGroup group = thread.getThreadGroup();
    System.out.printf("    priority: %d, group: %s, group count: %d\n", thread.getPriority(), group.getName(),
            group.activeCount());
    StackTraceElement[] backtrace = thread.getStackTrace();

    for (StackTraceElement e : backtrace) {
        System.out.printf("    Stack Trace: %s\n", e);
    }
}

From source file:Main.java

/**
 * Print diagnostic info about the current thread.
 *//* ww w .ja va2s .  c  om*/
public static void printThreadInfo() {
    Thread thread = Thread.currentThread();
    System.out.printf("  Thread id: %d, name: %s, state: %s, daemon: %s, EDT: %s\n", thread.getId(),
            thread.getName(), thread.getState(), thread.isDaemon(), EventQueue.isDispatchThread());
    ThreadGroup group = thread.getThreadGroup();
    System.out.printf("    priority: %d, group: %s, group count: %d\n", thread.getPriority(), group.getName(),
            group.activeCount());
    StackTraceElement[] backtrace = thread.getStackTrace();
    if (backtrace.length > 2) {
        System.out.printf("    trace[2]: %s\n", backtrace[2]);
    }
}

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  a 2s .  c o  m*/

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

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");
                    }//from   w  w w  .  ja v  a  2 s .c  o m
                } else {
                    thread.setContextClassLoader(replacementClassLoader);
                    if (debug) {
                        log.debug(
                                "Replaced leaked cl in thread [" + thread + "] with " + replacementClassLoader);
                    }
                }
            }
        }
    }
}