What is a daemon thread?

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/meism5/article/details/100515809

Java threads are divided into user threads and daemon threads.

Daemon thread is running when the program provide a universal service in a background thread. All user threads to stop, the process will be stopped all the daemon threads, exit the program.

The method in Java threads to daemon threads: Call setDaemon thread (true) method prior to start threads.

 

note:

  • setDaemon (true) must () before setting the start, otherwise it will throw an exception IllegalThreadStateException, the thread is still defaults to the user thread, continue
  • Daemon thread created thread is a daemon thread
  • Daemon threads should not access, write persistent resources, such as files, databases, because it can be stopped at any time, did not lead to the release of resources, data is written to the interrupt issues
package constxiong.concurrency.a008;

/**
 * 测试守护线程
 * @author ConstXiong
 * @date 2019-09-03 12:15:59
 */
public class TestDaemonThread {

	public static void main(String[] args) {
		testDaemonThread();
	}
	
	//
	public static void testDaemonThread() {
		Thread t = new Thread(() -> {
			//创建线程,校验守护线程内创建线程是否为守护线程
			Thread t2 = new Thread(() -> {
				System.out.println("t2 : " + (Thread.currentThread().isDaemon() ? "守护线程" : "非守护线程"));
			});
			t2.start();
			
			//当所有用户线程执行完,守护线程会被直接杀掉,程序停止运行
			int i = 1;
			while(true) {
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println("t : " + (Thread.currentThread().isDaemon() ? "守护线程" : "非守护线程") + " , 执行次数 : " + i);
				if (i++ >= 10) {
					break;
				}
			}
		});
		//setDaemon(true) 必须在 start() 之前设置,否则会抛出IllegalThreadStateException异常,该线程仍默认为用户线程,继续执行
		t.setDaemon(true);
		t.start();
		
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		//主线程结束
		System.out.println("主线程结束");
	}
	
}

Results of the

t2 : 守护线程
t : 守护线程 , 执行次数 : 1
主线程结束
t : 守护线程 , 执行次数 : 2

 

in conclusion:

  • The above code thread t, unprinted to t: daemon thread, time: 10, descriptions of all user threads to stop, the process will be stopped all the daemon threads, exit the program
  • When t.start (); put t.setDaemon (true); before, the program throws IllegalThreadStateException, t is still the user thread, print as follows
Exception in thread "main" t2 : 非守护线程
java.lang.IllegalThreadStateException
	at java.lang.Thread.setDaemon(Thread.java:1359)
	at constxiong.concurrency.a008.TestDaemonThread.testDaemonThread(TestDaemonThread.java:39)
	at constxiong.concurrency.a008.TestDaemonThread.main(TestDaemonThread.java:11)
t : 非守护线程 , 执行次数 : 1
t : 非守护线程 , 执行次数 : 2
t : 非守护线程 , 执行次数 : 3
t : 非守护线程 , 执行次数 : 4
t : 非守护线程 , 执行次数 : 5
t : 非守护线程 , 执行次数 : 6
t : 非守护线程 , 执行次数 : 7
t : 非守护线程 , 执行次数 : 8
t : 非守护线程 , 执行次数 : 9
t : 非守护线程 , 执行次数 : 10

Source: GitHub     gitee

 


Collect and share the most valuable programming information 

Personal blog   |   Public Number   |   GitHub   |   code cloud 

 

Guess you like

Origin blog.csdn.net/meism5/article/details/100515809