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 String getThreadInfo(Thread thread) {
    String info = String.format("id:%s, Name:%s, State:%s , Status:%d", thread.getId(), thread.getName(),
            thread.getState(), thread.getPriority());
    return info;/*w ww. ja  v  a2 s  .c o m*/
}

From source file:Main.java

public static Thread getThreadByName(String name) {
    if (name != null) {
        final Thread[] threads = getAllThreads();

        for (Thread thread : threads) {
            if (thread.getName().equals(name)) {
                return thread;
            }//from w  w w  .ja va  2 s  .co m
        }
    }

    return null;
}

From source file:ThreadLister.java

private static void printThreadInfo(Thread t, String indent) {
    if (t == null)
        return;/*  w w  w .j a v a  2 s. c  o  m*/
    System.out.println(indent + "Thread: " + t.getName() + "  Priority: " + t.getPriority()
            + (t.isDaemon() ? " Daemon" : "") + (t.isAlive() ? "" : " Not Alive"));
}

From source file:GetPriority.java

private static Runnable makeRunnable() {
    Runnable r = new Runnable() {
        public void run() {
            for (int i = 0; i < 5; i++) {
                Thread t = Thread.currentThread();
                System.out.println("in run() - priority=" + t.getPriority() + ", name=" + t.getName());

                try {
                    Thread.sleep(2000);
                } catch (InterruptedException x) {
                    // ignore
                }/*from  ww w  .  j  av a2s  .co  m*/
            }
        }
    };

    return r;
}

From source file:Main.java

/**
 * Gets all threads if its name matches a regular expression.  For example,
 * using a regex of "main" will execute a case-sensitive match for threads
 * with the exact name of "main".  A regex of ".*main.*" will execute a
 * case sensitive match for threads with "main" anywhere in their name. A
 * regex of "(?i).*main.*" will execute a case insensitive match of any
 * thread that has "main" in its name.//from w w w  .j  a va 2 s.  c o  m
 * @param regex The regular expression to use when matching a threads name.
 *      Same rules apply as String.matches() method.
 * @return An array (will not be null) of all matching threads.  An empty
 *      array will be returned if no threads match.
 */
static public Thread[] getAllThreadsMatching(final String regex) {
    if (regex == null)
        throw new NullPointerException("Null thread name regex");
    final Thread[] threads = getAllThreads();
    ArrayList<Thread> matchingThreads = new ArrayList<Thread>();
    for (Thread thread : threads) {
        if (thread.getName().matches(regex)) {
            matchingThreads.add(thread);
        }
    }
    return matchingThreads.toArray(new Thread[0]);
}

From source file:Main.java

/**
 * Based on the default thread factory/*w ww . j  a  v a 2  s .co m*/
 *
 * @param name
 *            the name prefix of the new thread
 * @return the factory to use when creating new threads
 */
public static final ThreadFactory getThreadFactory(String name) {
    return r -> {
        Thread t = Executors.defaultThreadFactory().newThread(r);
        t.setName(name + "-" + t.getName()); //$NON-NLS-1$
        return t;
    };
}

From source file:Main.java

public static void dumpThreads() {
    final Map<Thread, StackTraceElement[]> threads = Thread.getAllStackTraces();
    for (Thread thread : threads.keySet())
        System.out.println((thread.isDaemon() ? "DAEMON  " : "        ") + thread.getName());

    // final ThreadInfo[] threads = ManagementFactory.getThreadMXBean().dumpAllThreads(false, false);
    //        for(ThreadInfo thread : threads) {
    //            System.out.println(thread.getThreadName() + ": ");
    //        }//from w  ww  .ja  va 2 s  . c o m
}

From source file:Main.java

/**
 * This method recursively visits all thread groups under group.
 * //from   w w  w  . j  a  v  a2 s .  co  m
 * @param group
 *            {@link ThreadGroup}
 * @param level
 *            a thread level
 */
public static void viewRunningThreads(ThreadGroup group, int level) {
    /* Get threads in group */
    int numThreads = group.activeCount();
    Thread[] threads = new Thread[numThreads * 2];
    numThreads = group.enumerate(threads, false);

    /* Enumerate each thread in group */
    for (int i = 0; i < numThreads; i++) {
        // Get thread
        Thread thread = threads[i];
        System.out.println(thread.getState() + ", " + thread.getName());
    }

    /* Get thread subgroups of group */
    int numGroups = group.activeGroupCount();
    ThreadGroup[] groups = new ThreadGroup[numGroups * 2];
    numGroups = group.enumerate(groups, false);

    /* Recursively visit each subgroup */
    for (int i = 0; i < numGroups; i++) {
        viewRunningThreads(groups[i], level + 1);
    }
}

From source file:Main.java

/**
 * Returns all threads that have a name matching the specified pattern name.
 * /* w  w  w  .  j a  v  a 2  s.c  o m*/
 * @param threadNamePattern
 *            regex to match.
 * 
 * @return all threads that have a name matching the specified pattern name.
 */
public static Thread[] getAllThreads(String threadNamePattern) {
    Thread[] threads = getAllThreads();

    Thread[] filteredThreads = new Thread[threads.length];

    int i = 0;
    for (Thread t : threads) {
        if (t.getName().matches(threadNamePattern)) {
            filteredThreads[i] = t;
            i++;
        }
    }

    return Arrays.copyOfRange(filteredThreads, 0, i);
}

From source file:Main.java

/**
 * @param thread a thread/*from w  w w. j a va 2 s .co  m*/
 * @return a human-readable representation of the thread's stack trace
 */
public static String formatStackTrace(Thread thread) {
    Throwable t = new Throwable(
            String.format("Stack trace for thread %s (State: %s):", thread.getName(), thread.getState()));
    t.setStackTrace(thread.getStackTrace());
    StringWriter sw = new StringWriter();
    t.printStackTrace(new PrintWriter(sw));
    return sw.toString();
}