Share
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Aviance School is one of the largest web solutions platform in India for developers to learn and share their programming knowledge and build their careers.
A daemon thread is a thread that does not prevent the JVM from exiting when the program finishes but the thread is still running. An example for a daemon thread is the garbage collection.
The basic Example:
public class DaemonThreadExp extends Thread {
public void run() {
System.out.println("Entering to the run method..");
try {
System.out.println("In the run Method: currentThread() is" + Thread.currentThread());
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException x) {}
System.out.println("In the run method: woke up again");
}
} finally {
System.out.println("Leaving from the run Method");
}
}
public static void main(String[] args) {
System.out.println("Entering the main Method");
DaemonThreadExp t = new DaemonThreadExp();
t.setDaemon(true);
t.start();
try {
Thread.sleep(5000);
} catch (InterruptedException x) {}
System.out.println("Leaving from the main method");
}
}