This section describes about the daemon threads in java.
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 {
|
Output of this program is:
C:\j2se6\thread>javac DaemonThread.java C:\j2se6\thread>java DaemonThread Entering main Method Entering run method In run Method: currentThread() isThread[Thread-0,5,main] In run method: woke up again In run method: woke up again In run method: woke up again In run method: woke up again In run method: woke up again In run method: woke up again Leaving main method C:\j2se6\thread> |