Daemon Threads - Coddicted

Daemon Threads

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.
 
 
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.
 
For example, the HotJava browser uses up to four daemon threads named “Image Fetcher” to fetch images from the file system or network for any thread that needs one.
 
Daemon threads are typically used to perform services for your application/applet (such as loading the “fiddley bits”). The core difference between user threads and daemon threads is that the JVM will only shut down a program when all user threads have terminated. Daemon threads are terminated by the JVM when there are no longer any user threads running, including the main thread of execution.
 
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.
 
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”);
}
}
 
output:
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

Back To Top
Rate this post

Leave a Reply