Example usage for java.lang Thread getName

List of usage examples for java.lang Thread getName

Introduction

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

Prototype

public final String getName() 

Source Link

Document

Returns this thread's name.

Usage

From source file:Main.java

public static Thread findThreadByName(String name) {
    ThreadGroup group = Thread.currentThread().getThreadGroup();

    while (group.getParent() != null)
        group = group.getParent();//  www.j  a va  2s  . co  m
    Thread[] threadList = new Thread[group.activeCount() + 5];
    group.enumerate(threadList, true);
    for (Thread thread : threadList)
        if (thread != null && thread.getName().equals(name))
            return thread;
    return null;
}

From source file:Main.java

/**
 * Gets the name of the particular Thread or null if the Thread object reference is null.
 * <p/>/* ww w. ja  v  a  2  s  .c om*/
 * @param thread the Thread object whose name is returned.
 * @return a String value indicating the name of the Thread or null if the Thread object reference is null.
 * @see java.lang.Thread#getName()
 */
public static String getThreadName(final Thread thread) {
    return (thread == null ? null : thread.getName());
}

From source file:Main.java

/**
 * Throws an {@link IllegalStateException} if the current thread is not an initialization thread.
 *///from www. java2s.c o m
public static void ensureInitThread() {
    Thread currentThread = Thread.currentThread();
    boolean isInitThread = currentThread.getName().equals("LunaInitializationThread");
    checkState(isInitThread, String.format("thread[%s] not an initialization thread", currentThread));
}

From source file:Main.java

public static String getAllThreadNames() {
    Thread[] array = new Thread[Thread.activeCount()];

    Thread.enumerate(array);/*from  w  w w. ja  va 2s  .  c  o m*/

    String s = "";
    for (Thread t : array) {
        if (t != null) {
            s += t.getName() + ", ";
        }
    }
    return s;
}

From source file:Main.java

public static String dumpStack(Thread t) {
    StringBuilder sb = new StringBuilder();
    sb.append(t.getName() + "[" + t.getId() + "] - " + t.getState() + ":\n");
    for (StackTraceElement ste : t.getStackTrace()) {
        sb.append("\tat " + ste.getClassName() + "." + ste.getMethodName() + "(" + ste.getFileName() + ":"
                + ste.getLineNumber() + ")\n");
    }/*  w  ww.ja va  2  s  .  c o  m*/
    return sb.toString();
}

From source file:Main.java

/**
 * Wait for all threads with the specified name to finish, i.e. to not appear in the
 * list of running threads any more./*from  ww  w.j a va 2 s  .  c  o  m*/
 *
 * @param name
 *
 * @throws InterruptedException
 * @author dominik.stadler
 */
public static void waitForThreadToFinish(final String name) throws InterruptedException {
    int count = Thread.currentThread().getThreadGroup().activeCount();

    Thread[] threads = new Thread[count];
    Thread.currentThread().getThreadGroup().enumerate(threads);

    for (Thread t : threads) {
        if (t != null && name.equals(t.getName())) {
            t.join();
        }
    }
}

From source file:Main.java

public static String getThreadSignature() {
    Thread t = Thread.currentThread();
    long l = t.getId();
    String name = t.getName();
    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();
    long pri = t.getPriority();
    String gname = t.getThreadGroup().getName();
    return (name + ":(id)" + id + ":(priority)" + pri + ":(group)" + gname);
}

From source file:Main.java

private static int countThreads(String namePattern) {
    Thread[] list = new Thread[100];
    Thread.enumerate(list);/*w w  w. j a  v a2 s.  co m*/
    int matches = 0;
    for (Thread element : list) {
        if (element != null) {
            if (element.getName().matches(namePattern) || element.getClass().getName().matches(namePattern)) {
                matches++;
            }
        }
    }
    return matches;
}

From source file:Main.java

/**
 * Utility method that sets name, daemon status and starts passed thread.
 * @param t thread to run/* ww  w.j  ava  2  s  .  com*/
 * @return Returns the passed Thread <code>t</code>.
 */
public static Thread setDaemonThreadRunning(final Thread t) {
    return setDaemonThreadRunning(t, t.getName());
}