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

/**
 * Get the thread with the given ID and Name.  A null is returned
 * if no such thread is found./* ww  w .  j  a  va  2s.co  m*/
 *
 * @param   id   the thread ID to search for
 * @param   name   the thread name to search for
 * @return      the thread, or null if not found
 */
public static Thread getThread(final long id, final String name) {
    final Thread[] threads = getAllThreads();
    for (Thread thread : threads)
        if ((thread.getId() == id) && (thread.getName().equals(name)))
            return thread;
    return null;
}

From source file:org.apache.bookkeeper.common.testing.util.TimedOutTestsListener.java

static String buildThreadDump() {
    StringBuilder dump = new StringBuilder();
    Map<Thread, StackTraceElement[]> stackTraces = Thread.getAllStackTraces();
    for (Map.Entry<Thread, StackTraceElement[]> e : stackTraces.entrySet()) {
        Thread thread = e.getKey();
        dump.append(String.format("\"%s\" %s prio=%d tid=%d %s\njava.lang.Thread.State: %s", thread.getName(),
                (thread.isDaemon() ? "daemon" : ""), thread.getPriority(), thread.getId(),
                Thread.State.WAITING.equals(thread.getState()) ? "in Object.wait()"
                        : StringUtils.lowerCase(thread.getState().name()),
                Thread.State.WAITING.equals(thread.getState()) ? "WAITING (on object monitor)"
                        : thread.getState()));
        for (StackTraceElement stackTraceElement : e.getValue()) {
            dump.append("\n        at ");
            dump.append(stackTraceElement);
        }//from ww  w  . ja  v a2 s. c o  m
        dump.append("\n");
    }
    return dump.toString();
}

From source file:org.apache.distributedlog.TestDistributedLogBase.java

@BeforeClass
public static void setupCluster() throws Exception {
    File zkTmpDir = IOUtils.createTempDir("zookeeper", "distrlog");
    tmpDirs.add(zkTmpDir);/*from   ww w  .  j  a  va2  s  .  c  o m*/
    Pair<ZooKeeperServerShim, Integer> serverAndPort = LocalDLMEmulator.runZookeeperOnAnyPort(zkTmpDir);
    zks = serverAndPort.getLeft();
    zkPort = serverAndPort.getRight();
    bkutil = LocalDLMEmulator.newBuilder().numBookies(numBookies).zkHost("127.0.0.1").zkPort(zkPort)
            .serverConf(DLMTestUtil.loadTestBkConf()).shouldStartZK(false).build();
    bkutil.start();
    zkServers = "127.0.0.1:" + zkPort;
    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread t, Throwable e) {
            LOG.warn("Uncaught exception at Thread {} : ", t.getName(), e);
        }
    });
}

From source file:Main.java

static Object[] createThreadInfo(Thread thread, Object lock, Thread lockOwner) {

    String lockName = null;//from w  w w . j a  v a 2 s  .c o m
    String lockOwnerName = null;

    if (lock != null) {
        lockName = lock.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(lock));
        if (lockOwner != null)
            lockOwnerName = lockOwner.getName();
    }

    return new Object[] { thread.getName(), thread.getState(), lockName, lockOwnerName };
}

From source file:de.unidue.inf.is.ezdl.gframedl.EzDL.java

private static void initLogging() {
    System.setProperty("appDir", SystemUtils.getPropertyDir().getAbsolutePath());

    URL logConfigUrl = null;/*  w  w w  . j  a  v a 2s . c o m*/
    File logConfigFile = new File(SystemUtils.getPropertyDir(), "/logging.properties");

    if (logConfigFile.exists()) {
        try {
            logConfigUrl = logConfigFile.toURI().toURL();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    } else {
        logConfigUrl = EzDL.class.getResource("/log/logging.properties");
    }

    if (logConfigUrl != null) {
        PropertyConfigurator.configure(logConfigUrl);
    } else {
        System.err.println("no logger config found!");
    }

    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread t, Throwable e) {
            logger.error("Uncaught exception in Thread " + t.getName(), e);
        }
    });
    System.err.close();
}

From source file:org.xtreemfs.foundation.util.OutputUtils.java

public static String getThreadDump() {
    StringBuilder sb = new StringBuilder();
    sb.append("<HTML><BODY><H1>THREAD STATES</H1><PRE>");
    final Map<Thread, StackTraceElement[]> traces = Thread.getAllStackTraces();
    for (Thread t : traces.keySet()) {
        sb.append("<B>thread: ");
        sb.append(t.getName());
        sb.append("</B>\n");
        final StackTraceElement[] elems = traces.get(t);
        for (int i = elems.length - 1; i >= 0; i--) {
            sb.append(elems[i].toString());
            sb.append("\n");
        }//from  w  ww  . ja  v  a2s. c om
        sb.append("\n");
    }
    sb.append("</PRE></BODY></HTML>");
    return sb.toString();
}

From source file:org.midonet.midolman.Midolman.java

public static void dumpStacks() {
    Map<Thread, StackTraceElement[]> traces = Thread.getAllStackTraces();
    for (Thread thread : traces.keySet()) {
        System.err.print("\"" + thread.getName() + "\" ");
        if (thread.isDaemon())
            System.err.print("daemon ");
        System.err.print(String.format("prio=%x tid=%x %s [%x]\n", thread.getPriority(), thread.getId(),
                thread.getState(), System.identityHashCode(thread)));

        StackTraceElement[] trace = traces.get(thread);
        for (StackTraceElement e : trace) {
            System.err.println("        at " + e.toString());
        }/*from  w  w w.  j  a v  a2 s. c  o  m*/
    }
}

From source file:com.dattack.dbtools.drules.engine.DrulesEngine.java

private static ThreadFactory createThreadFactory() {
    return new ThreadFactoryBuilder() //
            .withThreadNamePrefix("source") //
            .withUncaughtExceptionHandler(new UncaughtExceptionHandler() {

                @Override//from ww w  .j a va 2  s . com
                public void uncaughtException(final Thread thread, final Throwable throwable) {
                    LOGGER.error("Uncaught exception throwed by thread '{}': {}", thread.getName(),
                            throwable.getMessage());
                }
            }).build();
}

From source file:org.apache.hadoop.hbase.regionserver.ShutdownHook.java

/**
 * Install a shutdown hook that calls stop on the passed Stoppable
 * and then thread joins against the passed <code>threadToJoin</code>.
 * When this thread completes, it then runs the hdfs thread (This install
 * removes the hdfs shutdown hook keeping a handle on it to run it after
 * <code>threadToJoin</code> has stopped).
 *
 * <p>To suppress all shutdown hook  handling -- both the running of the
 * regionserver hook and of the hdfs hook code -- set
 * {@link ShutdownHook#RUN_SHUTDOWN_HOOK} in {@link Configuration} to
 * <code>false</code>./*from www .j  av a2s.  c  o  m*/
 * This configuration value is checked when the hook code runs.
 * @param conf
 * @param fs Instance of Filesystem used by the RegionServer
 * @param stop Installed shutdown hook will call stop against this passed
 * <code>Stoppable</code> instance.
 * @param threadToJoin After calling stop on <code>stop</code> will then
 * join this thread.
 */
public static void install(final Configuration conf, final FileSystem fs, final Stoppable stop,
        final Thread threadToJoin) {
    Runnable fsShutdownHook = suppressHdfsShutdownHook(fs);
    Thread t = new ShutdownHookThread(conf, stop, threadToJoin, fsShutdownHook);
    ShutdownHookManager.affixShutdownHook(t, 0);
    LOG.debug("Installed shutdown hook thread: " + t.getName());
}

From source file:Main.java

public static String getThreadName(Thread thr) {
    if (thr == null)
        return "null";

    // This depends on the formatting in SelectReaderThread and CorbaConnectionImpl.
    // Pattern for SelectReaderThreads:
    // SelectReaderThread CorbaConnectionImpl[ <host> <post> <state>]
    // Any other pattern in the Thread's name is just returned.
    String name = thr.getName();
    StringTokenizer st = new StringTokenizer(name);
    int numTokens = st.countTokens();
    if (numTokens != 5)
        return name;

    String[] tokens = new String[numTokens];
    for (int ctr = 0; ctr < numTokens; ctr++)
        tokens[ctr] = st.nextToken();/*from  w  w w  .j  a  v  a 2 s  . co m*/

    if (!tokens[0].equals("SelectReaderThread"))
        return name;

    return "SelectReaderThread[" + tokens[2] + ":" + tokens[3] + "]";
}