Home > DeveloperSection > Interviews > what is daemon thread in java

Posted on    July-09-2013 1:41 AM

 Java Java 
 1 Answer(s)
  988  View(s)
Rate this:

shreesh chandra shukla
shreesh chandra shukla

Total Post:1

Posted on    July-09-2013 12:00 AM

Daemon threads are like a service providers for other threads or objects running in the same process as the daemon thread. Daemon threads are used for background supporting tasks and are only needed while normal threads are executing. If normal threads are not running and remaining threads are daemon threads then the interpreter exits.
daemon threads always runs  as background thread. 
Daemon threads are typically used to perform services for your application/applet. the core difference between user threads and daemon threads is that the JVM will only shut down a program when all user threads have terminated. Daemon threads are terminated by the JVM when there are no longer any user threads running, including the main thread of execution.
to set thread as daemon thread use following line of code:
setDaemon(true/false) ? //This method is used to specify that a thread is daemon thread.

public boolean isDaemon() ?// This method is used to determine the thread is daemon thread or not.
public class DaemonThreadExample extends Thread {
    public void run() {
        System.out.println("Entering run method");

        try {
            System.out.println("In  Method: currentThread() is" + Thread.currentThread());

            while (true) {
                try {
                } catch (InterruptedException x) {}

                System.out.println("In run method: woke up again");
        } finally {
            System.out.println("exit run Method");
    public static void main(String[] args) {
        System.out.println("Entering in main Method");

        DaemonThread t = new DaemonThread();

        try {
        } catch (InterruptedException x) {}

        System.out.println("Leaving main method");


Don't want to miss updates? Please click the below button!

Follow MindStick