Wednesday, 14 March 2012

Daemon thread

Daemon is used in multitasking operating system . Daemon  is  a computer program that run as a background  process . it meas that it doesn't have direct control of an interactive user. in UNIX environment init   process is the parent process ( not always ) .  System often start Daemon at boot time ,  they often serve the function of responding to network requests, hardware activity, or other programs by performing some task  .

In java Daemon thread  are like service provider for other thread or object run in the same process as daemon thread . this thread used as back ground process. This thread only needed if  normal thread executing . all normal thread are not running and renaming thread are Daemon then interpreter exists.

            setDaemon(true/false)   :- used for setting a thread as Daemon .

           public boolean isDaemon()  :- Used for testing this thread is daemon or not .

example code :-

    public class BackGround extends Thread {
    public void run() {
       System.out.println("Entering Run Method");

       try {
         System.out.println("Current Thread : currentThread() is"
         + Thread.currentThread());

         while (true) {
           try {
           catch (InterruptedException x) {

           System.out.println("Start thread again again");

       } finally {
              System.out.println("Exiting run Method");

  public static void main(String[] args) {
     System.out.println("Entering main Method");

     DaemonThread t = new DaemonThread();

     try {
    catch (InterruptedException x) {

    System.out.println("Exiting main method");



Entering main Method
Entering Run Method
Current Thread : currentThread() is [Thread-0,5,main]  
Start thread again again
Start thread again again
Start thread again again
Start thread again again
Start thread again again
Start thread again again  
Exiting main method

Difference between User Thread and Daemon thread

 The daemon threads are typically used to perform services for user threads. The main() method of the application thread is a user thread. Threads created by a user thread are user thread. JVM doesn't terminates unless all the user thread terminate.

We can change user thread to daemon thread by setting setDaemon(true) on thread object.

Newly created thread get inherited the thread property("daemon-status") from the  parent thread (on this thread new thread is created eg:- main() ). So explicitly change the thread property .

Should call setDaemon(true) before thread start (call before run() method) .

If the Java runtime determines that the only threads running in an application are daemon threads (i.e., there are no user threads in existence) the Java runtime promptly closes down the application, effectively stopping all daemon threads dead in their tracks. In order for an application to continue running, it must always have at least one live user thread. In all other respects the Java runtime treats daemon threads and user threads in exactly the same manner.

Advance Concept of Daemon :-

Daemons are usually instantiated as process (is running instant of a program , it managed by kernel each process have unique processId (PID) .) .

there are 3 type of process  in Linux .

1. Intactive :- Inactive process interactivity by a user at the command line .
2. Batch  :- This process are submitted from a queue of process that will not intact with command line .
3. Daemon :-     This process recognize any process as whose parent process have PID 1 .

1 comment: