What are daemons?

Java threads are divided into user threads and guard threads.

  • Daemons are threads that provide a common service in the background when a program is running. When all user threads stop, the process will stop all daemons and exit the program.
  • The way to set threads to guard threads in Java is to call the thread's setDaemon(true) method before the start thread.

 

Be careful:

  • setDaemon(true) must be set before start(), otherwise an IllegalThreadStateException exception will be thrown. The thread is still the user thread by default, and execution will continue
  • The thread created by the daemons is also the Daemons
  • The daemons should not access and write to persistent resources, such as files and databases, because they will be stopped at any time, causing problems such as resource not released and data write interruption
package constxiong.concurrency.a008;

/**
 * Test Daemons
 * @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(() -> {
			//Create a thread and verify whether the thread created in the daemons is a Daemons
			Thread t2 = new Thread(() -> {
				System.out.println("t2 : " + (Thread.currentThread().isDaemon() ? "Daemon thread" : "Non daemonic thread"));
			});
			t2.start();
			
			//When all user threads are executed, the daemons will be killed directly and the program will stop running.
			int i = 1;
			while(true) {
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println("t : " + (Thread.currentThread().isDaemon() ? "Daemon thread" : "Non daemonic thread") + " , Execution times : " + i);
				if (i++ >= 10) {
					break;
				}
			}
		});
		//setDaemon(true) must be set before start(), otherwise an IllegalThreadStateException exception will be thrown. The thread is still the user thread by default, and execution will continue
		t.setDaemon(true);
		t.start();
		
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		//End of main thread
		System.out.println("End of main thread");
	}
	
}

results of enforcement

t2: Daemons
t: daemons, execution times: 1
End of main thread
t: daemons, execution times: 2

 

Conclusion:

  • The above code thread t is not printed to t: day thread, time: 10, indicating that all user threads are stopped, the process will stop all daemons and exit the program
  • When t.start(); is placed before t.setDaemon(true), the program throws IllegalThreadStateException, t is still the user thread, and the print is as follows
Exception in thread "main" t2 : Non daemonic thread
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 : Non daemonic thread , Execution times : 1
t : Non daemonic thread , Execution times : 2
t : Non daemonic thread , Execution times : 3
t : Non daemonic thread , Execution times : 4
t : Non daemonic thread , Execution times : 5
t : Non daemonic thread , Execution times : 6
t : Non daemonic thread , Execution times : 7
t : Non daemonic thread , Execution times : 8
t : Non daemonic thread , Execution times : 9
t : Non daemonic thread , Execution times : 10


 

 

All resources are summarized in the public address.



 

Keywords: Programming Java

Added by Lijoyx on Wed, 27 Nov 2019 23:43:42 +0200