Daemon thread is a service provider thread with low priority. Normally, it is used to provide service like background supporting tasks (garbage collection etc.). Its life depends on normal non-daemon threads. If all non-daemon threads are not running, the daemon thread will terminate.
Two methods are related to Daemon thread are:
public void setDaemon(boolean status)
which can only be called before the thread start.
public boolean isDaemon()
Difference between daemon thread and normal thread
- The child of a daemon thread is always daemon thread
- When the JVM shuts down, it does not wait for daemon threads to finish. Daemon thread will be abandoned. If there is a finally block in the daemon thread code, it is not guaranteed to be executed. In addition, stacks are not unwounded and JVM just exits.
The following example code will shows how a daemon thread is created
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package net.tecbar.thread; public class DaemonThreadExample { public static void main(String[] args) { TecThread thread1 = new TecThread(); TecThread thread2 = new TecThread(); TecThread thread3 = new TecThread(); TecThread thread4 = new TecThread(); thread1.setDaemon(true); thread1.start(); thread2.start(); thread3.start(); thread4.start(); } } class TecThread extends Thread { public void run() { StringBuilder sb = new StringBuilder(); sb.append("[thread ID: ").append(Thread.currentThread().getId()) .append( "] [name: " + Thread.currentThread().getName()) .append( "] [priority: " + Thread.currentThread().getPriority()).append("]"); if(Thread.currentThread().isDaemon()){ System.out.println("damon thread: " + sb.toString()); } else{ System.out.println("non-damon thread: " + sb.toString()); } } } |
The output:
1 2 3 4 5 6 |
non-damon thread: [thread ID: 10] [name: Thread-1] [priority: 5] non-damon thread: [thread ID: 12] [name: Thread-3] [priority: 5] non-damon thread: [thread ID: 11] [name: Thread-2] [priority: 5] damon thread: [thread ID: 9] [name: Thread-0] [priority: 5] |
The following example code will shows that once all non-dameon threads executed, the JVM will shut down without waiting for the daemon thread. The daemon thread in the example will be abandoned.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
package net.tecbar.thread; public class DaemonThreadExample2 { public static void main(String[] args) { Tec2Thread thread1 = new Tec2Thread(); Tec2Thread thread2 = new Tec2Thread(); Tec2Thread thread3 = new Tec2Thread(); Tec2Thread thread4 = new Tec2Thread(); thread1.setDaemon(true); thread1.start(); thread2.start(); thread3.start(); thread4.start(); } } class Tec2Thread extends Thread { public void run() { StringBuilder sb = new StringBuilder(); sb.append("[thread ID: ").append(Thread.currentThread().getId()) .append( "] [name: " + Thread.currentThread().getName()) .append( "] [priority: " + Thread.currentThread().getPriority()).append("]"); if(Thread.currentThread().isDaemon()){ try { Thread.sleep(2000); } catch (InterruptedException e) { throw new RuntimeException(e); } // it will not be executed as the daemon thread will abandoned // after all non-dameon threads are gone System.out.println("damon thread: " + sb.toString()); } else{ System.out.println("non-damon thread: " + sb.toString()); } } } |
The output:
1 2 3 4 5 |
non-damon thread: [thread ID: 10] [name: Thread-1] [priority: 5] non-damon thread: [thread ID: 11] [name: Thread-2] [priority: 5] non-damon thread: [thread ID: 12] [name: Thread-3] [priority: 5] |