Example usage for java.lang Thread setDaemon

List of usage examples for java.lang Thread setDaemon

Introduction

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

Prototype

public final void setDaemon(boolean on) 

Source Link

Document

Marks this thread as either a #isDaemon daemon thread or a user thread.

Usage

From source file:Main.java

/**
 * New thread creation factory/* w w w.  j a  v a 2s . c  om*/
 * @param name name of thread
 * @param daemon if its daemon or not.
 * @return ThreadFactory instance initialized for given values.
 */
public static ThreadFactory threadFactory(final String name, final boolean daemon) {
    return new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            Thread thread = new Thread(r, name);
            thread.setDaemon(daemon);
            return thread;
        }
    };
}

From source file:Main.java

public static Thread getDaemonThread(Runnable runnable) {
    Thread thread = Executors.defaultThreadFactory().newThread(runnable);
    thread.setDaemon(true);
    return thread;
}

From source file:Main.java

public static ThreadFactory daemonThreadFactory(final String name) {
    return new ThreadFactory() {
        @Override//from w  w w . j av  a  2  s .  c  om
        public Thread newThread(Runnable runnable) {
            Thread result = new Thread(runnable, name);
            result.setDaemon(true);
            return result;
        }
    };
}

From source file:Main.java

/**
 * Creates a daemon thread.//from   w  w w  . ja va  2s  .  c  o m
 *
 * @param runnable the runnable
 * @param threadName the thread name
 * @return the thread
 */
public static Thread createDaemonThread(Runnable runnable, String threadName) {
    Thread daemonThread = new Thread(runnable, threadName);
    daemonThread.setDaemon(true);
    return daemonThread;
}

From source file:Main.java

public static void runInThread(final Runnable runnable) throws Throwable {
    final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();
    Runnable exceptionGuard = new Runnable() {
        public void run() {
            try {
                runnable.run();/*from w w  w.j a  v a 2  s . c o  m*/
            } catch (Throwable throwable) {
                exception.set(throwable);
            }
        }
    };
    Thread thread = new Thread(exceptionGuard);
    thread.setDaemon(true);
    thread.start();
    thread.join();
    if (exception.get() != null) {
        throw exception.get();
    }
}

From source file:Main.java

public static ThreadFactory createSimpleThreadFactory(final String name) {
    return r -> {
        Thread t = new Thread(r);
        t.setName(name);/*w  w  w .  j  ava  2s.com*/
        t.setDaemon(true);
        return t;
    };
}

From source file:Main.java

public static void run(Runnable runnable, String threadName, boolean daemon) {

    Thread thread = new Thread(runnable, threadName);

    thread.setName(threadName);//from w w  w.j  ava  2s.  c o m

    thread.setDaemon(daemon);

    thread.start();
}

From source file:Main.java

public static ThreadFactory getThreadFactory(final String name, final UncaughtExceptionHandler handler) {
    return new ThreadFactory() {
        @Override/*from   w w  w  .  j  a v a 2s  .  c  o  m*/
        public Thread newThread(Runnable r) {
            Thread t = new Thread(null, r, name, 1024 * 1024);
            t.setDaemon(true);
            t.setUncaughtExceptionHandler(handler);
            return t;
        }
    };
}

From source file:Main.java

public static Dialog backgroundProcess(Context context, final Runnable run, final boolean showDialog,
        String loadingComment) {//from ww  w. ja  v  a 2 s  .c  om
    final ProgressDialog progressdialog = creativeProgressBar(context, loadingComment);
    if (showDialog)
        progressdialog.show();
    Runnable wrapper = new Runnable() {
        public void run() {
            run.run();
            if (showDialog)
                progressdialog.dismiss();
        }
    };
    Thread thread = new Thread(wrapper);
    thread.setDaemon(true);
    thread.start();
    return progressdialog;
}

From source file:edu.umn.cs.spatialHadoop.util.MemoryReporter.java

public static Thread startReporting() {
    Thread thread = new Thread(new MemoryReporter(), "MemReporter");
    thread.setDaemon(true);
    thread.start();//from w ww.  j  ava2s.  co  m
    return thread;
}