Example usage for java.lang Thread toString

List of usage examples for java.lang Thread toString

Introduction

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

Prototype

public String toString() 

Source Link

Document

Returns a string representation of this thread, including the thread's name, priority, and thread group.

Usage

From source file:es.emergya.Main.java

/**
 * Starts the app./*  w  w  w  .j a  va  2  s.  c o m*/
 * 
 * @param args
 */
public static void main(final String[] args) {
    try {
        Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {

            @Override
            public void uncaughtException(final Thread t, final Throwable e) {
                if (LOG.isTraceEnabled()) {
                    LOG.trace("Excepcion descontrolada en " + t.toString(), e);
                } else {
                    LOG.error("Excepcion descontrolada en " + t.toString() + " :: " + e.toString(), e);
                }
            }
        });
    } catch (Throwable t) {
        LOG.error(t, t);
    }

    try {
        TimeZone.setDefault(TimeZone.getTimeZone("Europe/Madrid")); //$NON-NLS-1$
        new Initializer().run();
    } catch (Throwable t) {
        LOG.error("Fallo el SwingUtilities.invokeLater", t);
    }
}

From source file:Main.java

/**
 * This method recursively visits (LOG.info()) all thread groups under
 * `group'./* w ww .  j  ava2  s . c  om*/
 * 
 * @param log the {@link Logger}to be used for logging
 */
public static void logThreadGroup(final Logger log, ThreadGroup group, int level) {
    // Get threads in `group'
    int numThreads = group.activeCount();
    final 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/
        final Thread thread = threads[i];
        log.info(thread.toString());
    }

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

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

From source file:Main.java

/**
 * This method recursively visits (log.info()) all thread groups under `group'.
 * //from  w w w .  j  a v  a 2  s  .  c om
 * @param log
 * @param logLevel
 */
public static void logThreadGroup(Logger log, Level logLevel, 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];
        log.log(logLevel, thread.toString());
    }

    // 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++) {
        logThreadGroup(log, logLevel, groups[i], level + 1);
    }
}

From source file:Main.java

private static void printStackTrace(PrintWriter writer, Thread thread, StackTraceElement[] trace) {
    try {/*  w ww . ja  v  a  2 s  .  co  m*/
        writer.println(thread.toString() + ":");
        for (int i = 0; i < trace.length; i++)
            writer.println("\tat " + trace[i]);
    } catch (Exception e) {
        writer.println("\t*** Exception printing stack trace: " + e);
    }
    writer.flush();
}

From source file:Main.java

public static void visit(ThreadGroup group, int level, StringBuffer buffer) {
    int numThreads = group.activeCount();
    Thread[] threads = new Thread[numThreads * 2];
    numThreads = group.enumerate(threads, false);
    for (int i = 0; i < numThreads; i++) {
        Thread t = threads[i];
        for (int j = 0; j < level; j++) {
            buffer.append("  ");
        }//  ww w. j  a v  a 2 s  .  c  o  m
        buffer.append(t.toString()).append("\r");
    }

    int numGroups = group.activeGroupCount();
    ThreadGroup[] groups = new ThreadGroup[numGroups * 2];
    numGroups = group.enumerate(groups, false);
    for (int i = 0; i < numGroups; i++) {
        for (int j = 0; j < level; j++) {
            buffer.append("  ");
        }
        buffer.append(groups[i].toString()).append("\r");
        visit(groups[i], level + 1, buffer);
    }
}

From source file:com.l2jfree.lang.L2Thread.java

public static List<String> getStats(Thread t) {
    List<String> list = new FastList<String>();

    list.add(t.toString() + " - ID: " + t.getId());
    list.add(" * State: " + t.getState());
    list.add(" * Alive: " + t.isAlive());
    list.add(" * Daemon: " + t.isDaemon());
    list.add(" * Interrupted: " + t.isInterrupted());
    for (ThreadInfo info : ManagementFactory.getThreadMXBean().getThreadInfo(new long[] { t.getId() }, true,
            true)) {// w  w w  .  j  av  a2 s .c o m
        for (MonitorInfo monitorInfo : info.getLockedMonitors()) {
            list.add("==========");
            list.add(" * Locked monitor: " + monitorInfo);
            list.add("\t[" + monitorInfo.getLockedStackDepth() + ".]: at " + monitorInfo.getLockedStackFrame());
        }

        for (LockInfo lockInfo : info.getLockedSynchronizers()) {
            list.add("==========");
            list.add(" * Locked synchronizer: " + lockInfo);
        }

        list.add("==========");
        for (StackTraceElement trace : info.getStackTrace())
            list.add("\tat " + trace);
    }

    return list;
}

From source file:com.microsoft.tfs.util.thread.ThreadInterruptTimer.java

@Override
public void run() {
    reset();//from www . j  a  va2 s.c o m

    while (true) {
        long lastResetCopy;
        long interruptTimeoutSecondsCopy;

        synchronized (this) {
            lastResetCopy = lastReset;
            interruptTimeoutSecondsCopy = interruptTimeoutSeconds;
        }

        final long idleMilliseconds = System.currentTimeMillis() - lastResetCopy;

        if (idleMilliseconds > interruptTimeoutSecondsCopy * 1000) {
            break;
        }

        final String messageFormat = "Idle for an acceptable {0} milliseconds out of {1}"; //$NON-NLS-1$
        final String message = MessageFormat.format(messageFormat, idleMilliseconds,
                interruptTimeoutSecondsCopy * 1000);
        log.trace(message);

        try {
            Thread.sleep(TIMEOUT_CHECK_PERIOD_SECONDS * 1000);
        } catch (final InterruptedException e) {
            break;
        }
    }

    /*
     * To avoid deadlock, we don't hold a lock on this object while
     * interrupting. The implementation of interrupt on the other thread may
     * call back into this class (though there's probably no reason to).
     */
    Thread interruptThisThread;
    synchronized (this) {
        interruptThisThread = threadToInterrupt;
    }

    final String messageFormat = "Timeout period of {0} seconds exceeded, interrupting {1}"; //$NON-NLS-1$
    final String message = MessageFormat.format(messageFormat, interruptTimeoutSeconds,
            interruptThisThread.toString());
    log.debug(message);

    interruptThisThread.interrupt();
}

From source file:com.workplacesystems.utilsj.collections.TransactionalBidiTreeMap.java

/**
 * Retrieve the current thread id for use by the
 * transaction code./*from www  .j  a  v  a  2  s  .  c o  m*/
 *
 * @return the thread id of the current thread
 */
protected String getCurrentThreadId() {

    String attach_id = (String) ThreadSession.getValue(getThreadSessionKey());
    if (attach_id != null)
        return attach_id;

    Thread thread = Thread.currentThread();
    return thread.toString() + "(" + thread.hashCode() + ")";
}

From source file:org.apache.hadoop.dfs.ClusterTestDFS.java

public static String summarizeThreadGroup() {
    int n = 10;//from www.  j  a  v a 2s.  c  o m
    int k = 0;
    Thread[] tarray = null;
    StringBuffer sb = new StringBuffer(500);
    do {
        n = n * 10;
        tarray = new Thread[n];
        k = Thread.enumerate(tarray);
    } while (k == n); // while array is too small...
    for (int i = 0; i < k; i++) {
        Thread thread = tarray[i];
        sb.append(thread.toString());
        sb.append("\n");
    }
    return sb.toString();
}

From source file:org.apache.hadoop.hdfs.TestDFSShell.java

@Test
public void testPut() throws IOException {
    Configuration conf = new HdfsConfiguration();
    MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).numDataNodes(2).build();
    FileSystem fs = cluster.getFileSystem();
    assertTrue("Not a HDFS: " + fs.getUri(), fs instanceof DistributedFileSystem);
    final DistributedFileSystem dfs = (DistributedFileSystem) fs;

    try {/*from   w  w w  .ja v  a2s  .c  o  m*/
        // remove left over crc files:
        new File(TEST_ROOT_DIR, ".f1.crc").delete();
        new File(TEST_ROOT_DIR, ".f2.crc").delete();
        final File f1 = createLocalFile(new File(TEST_ROOT_DIR, "f1"));
        final File f2 = createLocalFile(new File(TEST_ROOT_DIR, "f2"));

        final Path root = mkdir(dfs, new Path("/test/put"));
        final Path dst = new Path(root, "dst");

        show("begin");

        final Thread copy2ndFileThread = new Thread() {
            @Override
            public void run() {
                try {
                    show("copy local " + f2 + " to remote " + dst);
                    dfs.copyFromLocalFile(false, false, new Path(f2.getPath()), dst);
                } catch (IOException ioe) {
                    show("good " + StringUtils.stringifyException(ioe));
                    return;
                }
                //should not be here, must got IOException
                assertTrue(false);
            }
        };

        //use SecurityManager to pause the copying of f1 and begin copying f2
        SecurityManager sm = System.getSecurityManager();
        System.out.println("SecurityManager = " + sm);
        System.setSecurityManager(new SecurityManager() {
            private boolean firstTime = true;

            @Override
            public void checkPermission(Permission perm) {
                if (firstTime) {
                    Thread t = Thread.currentThread();
                    if (!t.toString().contains("DataNode")) {
                        String s = "" + Arrays.asList(t.getStackTrace());
                        if (s.contains("FileUtil.copyContent")) {
                            //pause at FileUtil.copyContent

                            firstTime = false;
                            copy2ndFileThread.start();
                            try {
                                Thread.sleep(5000);
                            } catch (InterruptedException e) {
                            }
                        }
                    }
                }
            }
        });
        show("copy local " + f1 + " to remote " + dst);
        dfs.copyFromLocalFile(false, false, new Path(f1.getPath()), dst);
        show("done");

        try {
            copy2ndFileThread.join();
        } catch (InterruptedException e) {
        }
        System.setSecurityManager(sm);

        // copy multiple files to destination directory
        final Path destmultiple = mkdir(dfs, new Path("/test/putmultiple"));
        Path[] srcs = new Path[2];
        srcs[0] = new Path(f1.getPath());
        srcs[1] = new Path(f2.getPath());
        dfs.copyFromLocalFile(false, false, srcs, destmultiple);
        srcs[0] = new Path(destmultiple, "f1");
        srcs[1] = new Path(destmultiple, "f2");
        assertTrue(dfs.exists(srcs[0]));
        assertTrue(dfs.exists(srcs[1]));

        // move multiple files to destination directory
        final Path destmultiple2 = mkdir(dfs, new Path("/test/movemultiple"));
        srcs[0] = new Path(f1.getPath());
        srcs[1] = new Path(f2.getPath());
        dfs.moveFromLocalFile(srcs, destmultiple2);
        assertFalse(f1.exists());
        assertFalse(f2.exists());
        srcs[0] = new Path(destmultiple2, "f1");
        srcs[1] = new Path(destmultiple2, "f2");
        assertTrue(dfs.exists(srcs[0]));
        assertTrue(dfs.exists(srcs[1]));

        f1.delete();
        f2.delete();
    } finally {
        try {
            dfs.close();
        } catch (Exception e) {
        }
        cluster.shutdown();
    }
}