Skip to main content

Daemon Thread

Daemon Thread:

  1. The thread which are executing in background is called Daemon thread .
        For example:-Garbage Collector,Signal Dispatcher,Attach Listener etc.
   
  2.The main objective of daemon Thread is to provide support to Non-daemon thread .
   
  3. Usually daemon thread having low priority but based on our requirement daemon thread can also run with high priority by JVM .
  
  4. By default main thread is always non-daemon and for all remaining thread daemon nature is inherited i.e if the parent thread is daemon then automatically child thread is also daemon and vice -versa.
 5.JVM terminate all daemon thread when all user thread finish their execution.
  
 Note:-Changing nature of thread to daemon is only possible before starting a thread .After starting a thread if we are trying to change thread to daemon thread then it will throw an exception illegalThreadStateException on runtime.
               it is impossible to convert main thread to daemon thread because it is already started by JVM at the beginning.

Methods:
 1.public boolean isDaemon();
 2.public void setDaemon();

Programs:-

class daemonThread extends Thread{
  public void run(){}
 public static void main(String args[])
  {
    System.out.println("Is main thread is daemon thread -->"+Thread.currentThread().isDaemon());
    daemonThread d1=new daemonThread();
    System.out.println("Is d1 thread is daemon thread -->"+d1.isDaemon());
    d1.setDaemon(true);//setting thread to daemon thread  
    System.out.println("Is d1 thread is daemon thread -->"+d1.isDaemon());    
  }
}

After starting a thread if we are trying to change thread to daemon thread then it will throw an exception illegalThreadStateException on runtime.

class daemonThread extends Thread{
  public void run(){}
 public static void main(String args[])
  {
      daemonThread d1=new daemonThread();
       d1.start();
       d1.setDaemon(true); 
  }
}
 
  
it is impossible to convert main thread to daemon thread because it is already started by JVM at the beginning.

class daemonThread extends Thread{
  public void run(){}
 public static void main(String args[])
  {
       Thread.currentThread().setDaemon(true);    
  }
}

Application:

Daemon thread is to provide services to user thread for background supporting task.





   

Comments

Popular posts from this blog

DeadLock

What is DeadLock? if two thread executing on two different objects.  Let's assume thread t1,t2 and objects ob1 ,ob2   t1 got the lock of object ob1.   t2 got the lock of object ob2. let's assume the situation when t1 wann a execute method which is present in ob2(t2 has a lock on ob2) and  t2 wanna execute method which is present in ob1(t1 has a lock on ob1).  Note-Both methods are synchronized.     Thread t1 asked t2 to release the lock of object ob2. So that t1 can execute the method present in ob2. But t2 disagree to release the lock of object ob2 and t2 asked t1 the same thing what t1 has asked for t2 to do.t1 also disagrees with t2. Both threads not to release lock is simple stubbornness(zidd in Hindi). This is the only reason for infinite waiting which is called deadlock. Points about DeadLock: If two thread is waiting for each other forever. Such type of infinite waiting is called DeadLock. Synchronized Keyword is the only reason for the DeadLock...

Deep Analysis of Algorithm(part 1)

 What is the need for speed in the algorithm? Suppose there is the software that provides lots of features including security but it takes almost 30 min to perform one task. And on the other hand, there is the software that provides you the same feature but it takes 2 min to perform one task. which one would you prefer? This is the reason why we should design an algorithm that is faster. Given two algorithms for a task, how do we find out which one is better? One naive way of doing this is – implement both the algorithms and run the two programs on your computer for different inputs and see which one takes less time. There are many problems with this approach for the analysis of algorithms. 1) It might be possible that for some inputs, the first algorithm performs better than the second. And for some inputs second performs better. 2) It might also be possible that for some inputs, the first algorithm performs better on one machine and the second works better on other machines for s...

Multi-threading enhancement

java.util.concurrent.lock package{1.5 version} Problem with tradition synchronized  keyword: We are not having any flexibility to try for a lock without waiting. There is no way to specify the maximum waiting   time for a thread to get the lock whereas in tradition synchronized thread will wait until getting the lock which may cause performance problems or deadlock. In  tradition synchronized,  if a thread releases the lock then which waiting thread will get that lock we are not having control over this To overcome this problem sun organization introduces java.util.lock package in JDK 1.5v. It also provides several enhancement to the programmers.  Lock Interface: Lock object is similar to the implicit lock to execute synchronized methods or blocks. Lock interface provides more extensive operation than traditional lock(synchronized). Important methods of Lock interface: void lock() - we can use this method to acquire a lock.       Different Cases:...