0% found this document useful (0 votes)
24 views

Daemon Threads

Daemon threads are background threads that provide services to other non-daemon threads. Daemon threads will exit when only daemon threads remain running as they have no non-daemon threads to service. To create a daemon thread, call setDaemon(true) on the thread object and check its status with isDaemon(). The example code creates a daemon thread that runs an infinite loop providing background services until the main thread exits, causing the process to terminate.

Uploaded by

Pramod Polamuri
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Daemon Threads

Daemon threads are background threads that provide services to other non-daemon threads. Daemon threads will exit when only daemon threads remain running as they have no non-daemon threads to service. To create a daemon thread, call setDaemon(true) on the thread object and check its status with isDaemon(). The example code creates a daemon thread that runs an infinite loop providing background services until the main thread exits, causing the process to terminate.

Uploaded by

Pramod Polamuri
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

Daemon Threads

In Java, any thread can be a Daemon thread. 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. 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. The following program demonstrates the Daemon Thread:
public class DaemonThread extends Thread { public void run() { System.out.println("Entering run method"); try { System.out.println("In run Method: currentThread() is" + Thread.currentThread()); while (true) { try { Thread.sleep(500); } catch (InterruptedException x) { } System.out.println("In run method: woke up again"); } } finally { System.out.println("Leaving run Method"); } } public static void main(String[] args) { System.out.println("Entering main Method"); DaemonThread t = new DaemonThread(); t.setDaemon(true); t.start(); try { Thread.sleep(3000); } catch (InterruptedException x) { } System.out.println("Leaving main method"); } }

The run() method for a daemon thread is typically an infinite loop that waits for a service request.

When the only remaining threads in a process are daemon threads, the interpreter exits. This makes sense because when only daemon threads remain, there is no other thread for which a daemon thread can provide a service. 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.

You might also like