Example usage for java.lang Thread isDaemon

List of usage examples for java.lang Thread isDaemon

Introduction

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

Prototype

public final boolean isDaemon() 

Source Link

Document

Tests if this thread is a daemon thread.

Usage

From source file:org.trafodion.rest.ResourceChecker.java

/**
 * Helper function: print the threads/*from  w w w  .ja v a  2 s  .c  o m*/
 */
public static void printThreads() {
    Set<Thread> threads = Thread.getAllStackTraces().keySet();
    System.out.println("name; state; isDameon; isAlive; isInterrupted");
    for (Thread t : threads) {
        System.out.println(t.getName() + ";" + t.getState() + ";" + t.isDaemon() + ";" + t.isAlive() + ";"
                + t.isInterrupted());
    }
}

From source file:Main.java

/** Collect info about a thread  */
private static void collectThreadInfo(final Thread t, final StringBuffer str, final String indent) {
    if (t == null) {
        return;// www  .  jav  a 2  s  . com
    }
    str.append(indent).append("Thread: [").append(t.getName()).append("]  Priority: [").append(t.getPriority())
            .append(t.isDaemon() ? " Daemon" : "").append("] ").append(t.isAlive() ? "" : " Not Alive")
            .append(LINE_SEPARATOR);
}

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);
        }/* w w w. j  a v  a  2  s . co m*/
        dump.append("\n");
    }
    return dump.toString();
}

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

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

    list.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z").format(new Date()));
    list.add("");
    list.add("## Java Platform Information ##");
    list.add("Java Runtime Name: " + System.getProperty("java.runtime.name"));
    list.add("Java Version: " + System.getProperty("java.version"));
    list.add("Java Class Version: " + System.getProperty("java.class.version"));
    list.add("");
    list.add("## Virtual Machine Information ##");
    list.add("VM Name: " + System.getProperty("java.vm.name"));
    list.add("VM Version: " + System.getProperty("java.vm.version"));
    list.add("VM Vendor: " + System.getProperty("java.vm.vendor"));
    list.add("VM Info: " + System.getProperty("java.vm.info"));
    list.add("");
    list.add("## OS Information ##");
    list.add("Name: " + System.getProperty("os.name"));
    list.add("Architeture: " + System.getProperty("os.arch"));
    list.add("Version: " + System.getProperty("os.version"));
    list.add("");
    list.add("## Runtime Information ##");
    list.add("CPU Count: " + Runtime.getRuntime().availableProcessors());
    list.add("");
    for (String line : getMemoryUsageStatistics())
        list.add(line);/*from w w w . ja v  a 2 s.c  o  m*/
    list.add("");
    list.add("## Class Path Information ##\n");
    for (String lib : System.getProperty("java.class.path").split(File.pathSeparator))
        if (!list.contains(lib))
            list.add(lib);
    list.add("");

    Set<Thread> threads = new TreeSet<Thread>(new Comparator<Thread>() {
        @Override
        public int compare(Thread t1, Thread t2) {
            if (t1.isDaemon() != t2.isDaemon())
                return Boolean.valueOf(t1.isDaemon()).compareTo(t2.isDaemon());

            final StackTraceElement[] st1 = t1.getStackTrace();
            final StackTraceElement[] st2 = t2.getStackTrace();

            for (int i = 1;; i++) {
                final int i1 = st1.length - i;
                final int i2 = st2.length - i;

                if (i1 < 0 || i2 < 0)
                    break;

                final int compare = st1[i1].toString().compareToIgnoreCase(st2[i2].toString());

                if (compare != 0)
                    return compare;
            }

            if (st1.length != st2.length)
                return Integer.valueOf(st1.length).compareTo(st2.length);

            return Long.valueOf(t1.getId()).compareTo(t2.getId());
        }
    });
    threads.addAll(Thread.getAllStackTraces().keySet());
    list.add("## " + threads.size() + " thread(s) ##");
    list.add("=================================================");

    int i = 1;
    for (Thread thread : threads) {
        list.add("");
        list.add(i++ + ".");
        list.addAll(getStats(thread));
    }

    return list;
}

From source file:es.darkhogg.hazelnutt.Hazelnutt.java

/**
 * Terminates the application in at most <i>time</i> milliseconds for
 * every alive thread. /* ww  w  .  ja  v  a 2 s .co  m*/
 * 
 * @param time Number of milliseconds to wait for each thread to terminate
 */
public static void terminate(long time) {
    Logger logger = getLogger();
    logger.info("Terminating application...");

    try {
        getFrame().dispose();

        // Get the root thread group
        ThreadGroup rootThreadGroup = Thread.currentThread().getThreadGroup();
        while (rootThreadGroup.getParent() != null) {
            rootThreadGroup = rootThreadGroup.getParent();
        }

        // Declare some collections
        Queue<ThreadGroup> threadGroups = new LinkedList<ThreadGroup>();
        Queue<Thread> threads = new LinkedList<Thread>();

        // Get ALL groups
        threadGroups.add(rootThreadGroup);
        while (!threadGroups.isEmpty()) {
            ThreadGroup group = threadGroups.remove();

            Thread[] subThreads = new Thread[group.activeCount() * 2];
            //group.enumerate( subThreads );
            for (Thread subThread : subThreads) {
                if (subThread != null) {
                    threads.add(subThread);
                }
            }

            ThreadGroup[] subThreadGroups = new ThreadGroup[group.activeGroupCount() * 2];
            for (ThreadGroup subThreadGroup : subThreadGroups) {
                if (subThreadGroup != null) {
                    threadGroups.add(subThreadGroup);
                }
            }
        }

        // Join a maximum of time milliseconds for all non-daemon threads
        while (!threads.isEmpty()) {
            Thread thread = threads.remove();
            LOGGER.trace(thread);

            if (!thread.isDaemon() && thread != Thread.currentThread()) {
                logger.trace("Waiting for thread '" + thread.getName() + "'");
                thread.join(time);
                if (thread.isAlive()) {
                    logger.trace("Interrupting thread '" + thread.getName() + "'");
                    thread.interrupt();
                }
            }
        }

    } catch (Throwable e) {
        LOGGER.warn("Interrupted while terminating application", e);

    } finally {
        // Exit the program
        System.exit(0);
    }
}

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());
        }/*  www  .  j  a  v a2s . c om*/
    }
}

From source file:com.ery.estorm.util.Threads.java

/**
 * Get a named {@link ThreadFactory} that just builds daemon threads.
 * // w  w  w.  j a v a  2 s .c o m
 * @param prefix
 *            name prefix for all threads created from the factory
 * @param handler
 *            unhandles exception handler to set for all threads
 * @return a thread factory that creates named, daemon threads with the supplied exception handler and normal priority
 */
public static ThreadFactory newDaemonThreadFactory(final String prefix,
        final UncaughtExceptionHandler handler) {
    final ThreadFactory namedFactory = getNamedThreadFactory(prefix);
    return new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            Thread t = namedFactory.newThread(r);
            if (handler != null) {
                t.setUncaughtExceptionHandler(handler);
            }
            if (!t.isDaemon()) {
                t.setDaemon(true);
            }
            if (t.getPriority() != Thread.NORM_PRIORITY) {
                t.setPriority(Thread.NORM_PRIORITY);
            }
            return t;
        }

    };
}

From source file:org.tranche.logs.LogUtil.java

/**
 * /*from   w  ww  .j  a  v a  2 s  . co m*/
 * @return
 */
public static final String getThreadDump() {
    Map<Thread, StackTraceElement[]> threadInfo = Thread.getAllStackTraces();
    StringBuffer buf = new StringBuffer();
    buf.append("Thread dump: " + threadInfo.size() + " threads");
    buf.append("\n" + "\n");
    for (Thread t : threadInfo.keySet()) {
        StackTraceElement[] ste = threadInfo.get(t);
        String daemonMsg = t.isDaemon() ? "daemon" : "non-daemon";
        String aliveMsg = t.isAlive() ? "alive" : "non-alive";
        buf.append("    * " + t.getName() + " (priority: " + t.getPriority() + ", " + daemonMsg + ", "
                + aliveMsg + ", state: " + t.getState() + ") ");
        buf.append("\n");

        for (int i = 0; i < ste.length; i++) {
            buf.append("        " + ste[i].toString());
            buf.append("\n");
        }

        buf.append("\n");
    }
    buf.append("\n" + "\n");
    return buf.toString();
}

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)) {/*from ww w .j a  va 2s.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:org.opennms.core.test.db.TemporaryDatabase.java

public static void dumpThreads() {
    Map<Thread, StackTraceElement[]> threads = Thread.getAllStackTraces();
    int daemons = 0;
    for (Thread t : threads.keySet()) {
        if (t.isDaemon()) {
            daemons++;//from  w ww.j a va 2s.c o  m
        }
    }
    System.err.println("Thread dump of " + threads.size() + " threads (" + daemons + " daemons):");
    Map<Thread, StackTraceElement[]> sortedThreads = new TreeMap<Thread, StackTraceElement[]>(
            new Comparator<Thread>() {
                public int compare(final Thread t1, final Thread t2) {
                    return Long.valueOf(t1.getId()).compareTo(Long.valueOf(t2.getId()));
                }
            });
    sortedThreads.putAll(threads);

    for (Entry<Thread, StackTraceElement[]> entry : sortedThreads.entrySet()) {
        Thread thread = entry.getKey();
        System.err.println("Thread " + thread.getId() + (thread.isDaemon() ? " (daemon)" : "") + ": " + thread
                + " (state: " + thread.getState() + ")");
        for (StackTraceElement e : entry.getValue()) {
            System.err.println("\t" + e);
        }
    }
    System.err.println("Thread dump completed.");
}