Another point to note is that, a program using daemon thread will run to completion even if the daemon thread is still running, waiting or sleeping.
Here is an example to understand how it works:
class A extends Thread {
public void run() {
synchronized (this) {
try {wait();} catch (InterruptedException ie){}
}}
public static void main(
String[] args) {
A a1 = new A();
a1.setDaemon(true);
long startTime = System.currentTimeMillis();
a1.start();
System.out.print(System.currentTimeMillis() - startTime + ",");
}}
Which is a possible result of attempting to compile and run the program?
a. The number printed is greater than or equal to 0
b. The synchronized block inside the run method is not necessary
c. Thread a1 waits forever and the program runs forever
d. Compile-time error
e. Run-time error
f. None of the above
Sol: The number printed is greater than or equal to 0 as the program will complete.
Regards,
-Raj Poosarla
---------------------------
Success is sweet but secret is sweat