Programmer A: I want a java program to keep polling my inbox and alert when a new message is received.
Programmer B: Thats simple, write a daemon thread to do it.
Programmer A: A daemon thread? How do i write one? Will it keep doing the polling job forever? What are its pre-requisites?
This is not just the case with Programmer A or Programmer B, most of the java programmers dont realise (incuding me) the power and pitfalls of a daemon thread.
Programmer A: How do I make a thread daemon?
Programmer B: Declare the setDaemon(boolean) property to true.
Programmer B's answer is partly right and partly not. Setting the daemon flag to true is just the work half done. To make a thread daemon, you need to do more.
Consider the java program,
package test;
public class DaemonThreads {
public static void main(String[] args) {
System.out.println("Start Program....");
new Daemon().start();
try {
System.out.println("Main thread going to sleep....");
Thread.sleep(100);
System.out.println("Main thread woke up....");
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("End of program....");
}
}
class Daemon extends Thread{
long count = 0L;
Daemon(){
this.setDaemon(true);
}
@Override
public void run() {
System.out.println("Count = "+ ++count);
}
}
(The main thread sleeps just to show that the daemon isn't doing anything. It is intentional and doesn't serve any other purpose.)
E:\test\Test\src>javac test/DaemonThreads.java
E:\test\Test\src>java test.DaemonThreads
Start Program....
Main thread going to sleep....
Count = 1
Main thread woke up....
End of program....
E:\test\Test\src>
It is evident that the daemon doesnt keep running, as we expected it to. The reason being, just on declaring as setDaemon(true), the thread is daemon only according to JVM (JVM doesnt consider it as a User Thread) but the job performed by the thread isnt that of a daemon.
A daemon thread is expected to be running always and does some job as long as the program is kept running. Hence this behavior is inserted into this thread prgrammatically, by using a indefinite loop, a infinite for loop or do-while(true) or the most widely used while(true) as shown below.
public void run() {
while(true)
System.out.println("Count = "+ ++count);
}
When run,the count is printed even after End of program is printed, clearly showing the behavior of a daemon which keeps running in the background irrespective of what happens in the main thread.
For those who clearly didn't understand the idea behind making the thread daemon, keep reading. The program exits even when Thread that was started by us isn't finished. (The thread should have been running till my JVM crashes as i have placed a while(true) condition, which can never be false to terminate the loop). Hence the idea is, daemon threads cannot outlive the javaw they were started in. Once all the processes started in the javaw is completed, it checks for any running User threads (note: not daemon threads, threads that don't have the daemon flag set) and when it doesn't find any, terminates the javaw, and of course the daemon that was running in it.
Hence setting the daemon flag to true doesnt just make a thread daemon, providing an infinte loop alone doesnt make a thread daemon.
Simple Ideas. Powerful Results.