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.opennms.core.test.db.TemporaryDatabasePostgreSQL.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 om*/
        }
    }
    System.err.println("Thread dump of " + threads.size() + " threads (" + daemons + " daemons):");
    Map<Thread, StackTraceElement[]> sortedThreads = new TreeMap<Thread, StackTraceElement[]>(
            new Comparator<Thread>() {
                @Override
                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.");
}

From source file:DaemonThreadFactory.java

/**
 * Create a new thread for the thread pool.  The create
 * thread will be a daemon thread.//  w ww . ja  va2 s. c  o  m
 * 
 * @param r      The runnable used by the thread pool.
 * 
 * @return The newly created thread. 
 */
public Thread newThread(Runnable r) {
    Thread t = factory.newThread(r);
    if (!t.isDaemon()) {
        t.setDaemon(true);
    }
    return t;
}

From source file:voltkvqa.AsyncBenchmark.java

/**
 * Fake an internal jstack to the log// ww  w  .  j  a  v a  2  s.  c om
 */
static public void printJStack() {
    prt(new Date().toString() + " Full thread dump");

    Map<String, List<String>> deduped = new HashMap<String, List<String>>();

    // collect all the output, but dedup the identical stack traces
    for (Entry<Thread, StackTraceElement[]> e : Thread.getAllStackTraces().entrySet()) {
        Thread t = e.getKey();
        String header = String.format("\"%s\" %sprio=%d tid=%d %s", t.getName(), t.isDaemon() ? "daemon " : "",
                t.getPriority(), t.getId(), t.getState().toString());

        String stack = "";
        for (StackTraceElement ste : e.getValue()) {
            stack += "    at " + ste.toString() + "\n";
        }

        if (deduped.containsKey(stack)) {
            deduped.get(stack).add(header);
        } else {
            ArrayList<String> headers = new ArrayList<String>();
            headers.add(header);
            deduped.put(stack, headers);
        }
    }

    for (Entry<String, List<String>> e : deduped.entrySet()) {
        String logline = "";
        for (String header : e.getValue()) {
            logline += header + "\n";
        }
        logline += e.getKey();
        prt(logline);
    }
}

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

@Test(timeout = 180000)
public void testShutdown() throws Exception {
    oldThreads = Thread.getAllStackTraces().keySet();
    conf = new Configuration();
    conf.setInt(HttpServer.HTTP_THREADPOOL_MAX_STOP_TIME, 3000);
    cluster = new MiniDFSCluster(conf, 0, true, null);

    InjectionHandler.set(new TestHandler());
    Thread fsck = new FsckThread();
    fsck.setDaemon(true);//from  ww  w.j  a v a2  s  . c o  m
    fsck.start();

    while (!fsckCalled) {
        Thread.sleep(1000);
        LOG.info("Waiting for fsck to hit NN");
    }
    cluster.shutdown();

    LOG.info("Alive Non Daemon threads : ");
    for (Map.Entry<Thread, StackTraceElement[]> entry : Thread.getAllStackTraces().entrySet()) {
        Thread t = entry.getKey();
        if (!t.isDaemon() && !oldThreads.contains(t)) {
            LOG.info("Thread : " + t.getName());
            for (StackTraceElement e : entry.getValue()) {
                LOG.info(e);
            }
            fail("Thread : " + t.getName() + " is not a daemon thread");
        }
    }
}

From source file:com.kotcrab.vis.editor.util.CrashReporter.java

private void printThreadInfo() {
    println("--- Threads ---");

    Set<Thread> threadSet = Thread.getAllStackTraces().keySet();

    for (Thread t : threadSet) {
        if (t.isDaemon()) {
            println("Skipping daemon thread: " + t.getName());
        } else {/*from  w w w .  j  a  va2 s  .c  o  m*/
            println("Thread: " + t.getName());
            for (StackTraceElement e : t.getStackTrace()) {
                crashReport.append("\t");
                println(e.toString());
            }
        }

        println();
    }

    println("---------------");
    println();
}

From source file:com.kotcrab.vis.editor.util.vis.CrashReporter.java

private void printThreadInfo() {
    println("--- Threads ---");

    Set<Thread> threadSet = Thread.getAllStackTraces().keySet();

    for (Thread t : threadSet) {
        println("Thread: " + t.getName() + ", daemon: " + t.isDaemon() + ", state: " + t.getState());
        for (StackTraceElement e : t.getStackTrace()) {
            crashReport.append("\t");
            println(e.toString());/*w w w .  j  a  va  2  s  .c  om*/
        }

        println();
    }

    println("---------------");
    println();
}

From source file:gridool.util.net.InterruptableInputStream.java

private int internalRead(final byte[] b, final int off, final int len) throws IOException {
    boolean ntraced = true;
    while (true) {
        try {/*ww w  .j a  v  a2s.  c  om*/
            final int n = is.read(b, off, len);
            return n;
        } catch (SocketTimeoutException e) {
            if (Thread.interrupted()) {
                throw e;
            } else {
                Thread thread = Thread.currentThread();
                if (!thread.isDaemon()) {
                    State state = thread.getState();
                    switch (state) {
                    case BLOCKED:
                    case WAITING:
                        if (LOG.isWarnEnabled()) {
                            LOG.warn("thread (" + thread.getName() + "timeout: " + thread.getState(), e);
                        }
                        throw e;
                    default:
                        if (LOG.isTraceEnabled() && ntraced) {
                            ntraced = false;
                            LOG.trace("thread (" + thread.getName() + ") timeout: " + thread.getState());
                        }
                        break;
                    }
                }
            }
        }
    }
}

From source file:org.apache.hadoop.fs.s3r.S3RFileSystem.java

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

    };
}

From source file:net.pms.util.BasicThreadFactory.java

@Override
public Thread newThread(Runnable runnable) {
    String threadName;//from   ww  w  .j av  a  2s  .co m
    switch (numVariables) {
    case 0:
        threadName = namePattern;
        break;
    case 1:
        threadName = String.format(Locale.ROOT, namePattern, threadNumber.getAndIncrement());
        break;
    default:
        threadName = String.format(Locale.ROOT, namePattern, instancePoolNumber,
                threadNumber.getAndIncrement());
    }
    Thread thread = new Thread(group, runnable, threadName, 0);
    if (thread.isDaemon()) {
        thread.setDaemon(false);
    }
    if (thread.getPriority() != threadPriority) {
        thread.setPriority(threadPriority);
    }
    return thread;
}

From source file:ThreadViewer.java

private void createPendingCellData() {
    Thread[] thread = findAllThreads();
    Object[][] cell = new Object[thread.length][columnCount];

    for (int i = 0; i < thread.length; i++) {
        Thread t = thread[i];
        Object[] rowCell = cell[i];

        rowCell[0] = new Integer(t.getPriority());
        rowCell[1] = new Boolean(t.isAlive());
        rowCell[2] = new Boolean(t.isDaemon());
        rowCell[3] = new Boolean(t.isInterrupted());
        rowCell[4] = t.getThreadGroup().getName();
        rowCell[5] = t.getName();/*w ww.j  a  va  2 s .  com*/
    }

    synchronized (dataLock) {
        pendingCellData = cell;
    }
}