3
You can make any java thread as daemon thread. Daemon threads acts like service providers for other threads running in the same process. Daemon threads will be terminated by the JVM when there are none of the other threads running, it includs main thread of execution as well. To specify that a thread is a daemon thread, call the setDaemon method with the argument true. To determine if a thread is a daemon thread, use the accessor method isDaemon. When we create a Thread in java, by default it’s a user thread and if it’s running JVM will not terminate the program. When a thread is marked as daemon thread, JVM doesn’t wait it to finish and as soon as all the user threads are finished, it terminates the program as well as all the associated daemon threads. Thread.setDaemon(true) can be used to create a daemon thread in java. Let’s see a small example of java daemon thread. JavaDaemonThread 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 package com.journaldev.threads; public class JavaDaemonThread { public static void main(String[] args) throws InterruptedException { Thread dt = new Thread(new DaemonThread(), "dt"); dt.setDaemon(true); dt.start(); //continue program Thread.sleep(30000); System.out.println("Finishing program"); } } class DaemonThread implements Runnable{ @Override public void run() { while(true){ processSomething(); } } private void processSomething() { try { System.out.println("Processing daemon thread"); Thread.sleep(5000);

Daemon Thread

Embed Size (px)

DESCRIPTION

daemon threads

Citation preview

Page 1: Daemon Thread

You can make any java thread as daemon thread. Daemon threads acts like service providers for other threads running in the same process.

Daemon threads will be terminated by the JVM when there are none of the other threads running, it includs main thread of execution as well.

To specify that a thread is a daemon thread, call the setDaemon method with the argument true.

To determine if a thread is a daemon thread, use the accessor method isDaemon. When we create a Thread in java, by default it’s a user thread and if it’s running JVM will not

terminate the program. When a thread is marked as daemon thread, JVM doesn’t wait it to finish and as soon as all the user threads are finished, it terminates the program as well as all the associated daemon threads.

Thread.setDaemon(true) can be used to create a daemon thread in java. Let’s see a

small example of java daemon thread.JavaDaemonThread

123456789101112131415161718192021222324252627282930313233

package com.journaldev.threads; public class JavaDaemonThread {     public static void main(String[] args) throws InterruptedException {        Thread dt = new Thread(new DaemonThread(), "dt");        dt.setDaemon(true);        dt.start();        //continue program        Thread.sleep(30000);        System.out.println("Finishing program");    } } class DaemonThread implements Runnable{     @Override    public void run() {        while(true){            processSomething();        }    }     private void processSomething() {        try {            System.out.println("Processing daemon thread");            Thread.sleep(5000);        } catch (InterruptedException e) {            e.printStackTrace();        }    }     }

Page 2: Daemon Thread

34 When we execute this program, JVM creates first user thread with main() function and then a

daemon thread. When main function is finished, the program terminates and daemon thread is also shut down by JVM.

Here is the output of the above program.

1234567

Processing daemon threadProcessing daemon threadProcessing daemon threadProcessing daemon threadProcessing daemon threadProcessing daemon threadFinishing program

If we don’t set the thread to be run as daemon thread, the program will never terminate even after main thread is finished it’s execution. Try commenting the statement to set thread as daemon thread and run the program.

Usually we create a daemon thread for functionalities that are not critical to system, for example logging thread or monitoring thread to capture the system resource details and their state.

What is difference between User and Daemon Thread in Java? Java makes a distinction between a user thread and another type of thread known as a daemon thread. The

daemon threads are typically used to perform services for user threads. The main() method of the application

thread is a user thread. Threads created by a user thread are user thread. JVM doesn't terminates unless all the

user thread terminate.

You can explicitly specify a thread created by a user thread to be a daemon thread by calling setDaemon(true)

on a Thread object. For example, the clock handler thread, the idle thread, the garbage collector thread, the

screen updater thread, and the garbage collector thread are all daemon threads. A new created thread inherits

the "daemon-status" of the thread that created it unless you explicitly calling setDaemon on that Thread object

to change its status.

Note that the setDaemon() method must be called before the thread's start() method is invoked.Once a thread

has started executing (i.e., its start() method has been called) its daemon status cannot be changed. To

determine if a thread is a daemon thread, use the accessor method isDaemon().

The difference between these two types of threads is straightforward: If the Java runtime determines that the

only threads running in an application are daemon threads (i.e., there are no user threads in existence) the Java

runtime promptly closes down the application, effectively stopping all daemon threads dead in their tracks. In

order for an application to continue running, it must always have at least one live user thread. In all other

respects the Java runtime treats daemon threads and user threads in exactly the same manner.