Here you can find the source of daemonThread(Runnable runnable)
Parameter | Description |
---|---|
runnable | The runnable to execute in the background |
public static Thread daemonThread(Runnable runnable)
//package com.java2s; /**//from w w w .ja v a2 s. c o m * Copyright 2016 LinkedIn Corp. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ public class Main { /** * Create a daemon thread * * @param runnable The runnable to execute in the background * @return The unstarted thread */ public static Thread daemonThread(Runnable runnable) { return newThread(runnable, true); } /** * Create a daemon thread * * @param name The name of the thread * @param runnable The runnable to execute in the background * @return The unstarted thread */ public static Thread daemonThread(String name, Runnable runnable) { return newThread(name, runnable, true); } /** * Create a new thread * * @param runnable The work for the thread to do * @param daemon Should the thread block JVM shutdown? * @return The unstarted thread */ public static Thread newThread(Runnable runnable, boolean daemon) { Thread thread = new Thread(runnable); thread.setDaemon(daemon); thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { public void uncaughtException(Thread t, Throwable e) { //error("Uncaught exception in thread '" + t.getName + "':", e) } }); return thread; } /** * Create a new thread * * @param name The name of the thread * @param runnable The work for the thread to do * @param daemon Should the thread block JVM shutdown? * @return The unstarted thread */ public static Thread newThread(String name, Runnable runnable, boolean daemon) { Thread thread = new Thread(runnable, name); thread.setDaemon(daemon); thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { public void uncaughtException(Thread t, Throwable e) { e.printStackTrace(); } }); return thread; } }