Skip to main content

Daemon Threads:

Daemon Threads:

Generally speaking, daemon threads are for background tasks, and non-daemon (user-threads) threads are for foreground tasks. The only difference is that JVM shuts down when the last non-daemon Thread terminates.


More often than not, background tasks run at a lower priority than foreground tasks (e.g. garbage collection). Still, many background tasks run at a higher priority (tracking the mouse etc.).


When an application starts, the main Thread (the Thread that calls the application's "main" method) is the only non-daemon Thread. When the application's main method exits, the main Thread terminates. If no other non-daemon threads are running, the application exits. 


Servers typically have a non-daemon thread listening for client connections and then use non-daemon threads for processing client requests. When the last client request thread closes, the connection listener thread is terminated to shut the server down. 


How to create a Daemon Thread:

This is an example is to demonstrate the usage of the setDaemon() and isDaemon() method.

public class DaemonThreadExample1 extends Thread{

   public void run(){  
		
	  // To check if method is Daemon or not:
	  if(Thread.currentThread().isDaemon()){ 
	      System.out.println("Daemon thread executing");  
	  }  
	  else{  
	      System.out.println("user thread executing");  
          }  
   }  
   public static void main(String[] args){  
	 //Creating 2 Threads
	 DaemonThreadExample1 threadOne=new DaemonThreadExample1();
	 DaemonThreadExample1 threadTwo=new DaemonThreadExample1();   
			 
	 //Making user Thread t1 to Daemon
        threadOne.setDaemon(true);
		     
        //starting both Threads 
        threadOne.start(); 
        threadTwo.start();  
   } 
}

Output:

Daemon thread executing
user thread executing

Popular posts from this blog

Executer vs ExecuterSevice: What's the Difference?

Executer and ExcuterService: Executor and ExecutorService are part of Java's Executor framework, which provides  thread pool  facilities to Java applications. Since creating and managing Threads are expensive, It's a better idea to create a thread pool that can execute in parallel rather than every time a new thread is requested, helping reduce the load. This improves the response time of the application. Differences between Executer and ExecuterService: One of the key differences between the Executor and ExecutorService interface is that the former is a parent interface while ExecutorService extends Executor.  A second significant difference between ExecutorService and Executor is that Executor defines an execute() method, which accepts the Runnable interface object. Simultaneously, a submit() method takes both Runnable and Callable object interfaces. Another difference between Executor and ExecutorService interface is that the execute() methods' return type is void, wh...

What happens if we don't override the run() method:

Consequences of not overriding the run() method: If we don’t override the run() method, the compiler will not flash any error, and it will execute the run() method of the Thread class that has been emptily implemented, So, there will be no output for this Thread. Below is an example of the method returning no output: public class Example extends Thread { public static void main(String[] args) { Thread thread = new Thread( new Example()); thread.start(); // will create a new thread and call thread class's run() method which has no implementation. } }

Creating and Running a Java Thread:

Ways to create a Thread: You can create a Thread in two ways: By extending the Thread class. By implementing a Runnable Interface. Thread class: The Thread class provides constructors and methods to create and perform operations on a thread. Runnable Interface: Runnable Interfaces have only one method,  named run() . The Runnable Interface should be performed by classes where their instances plan to be executed by a thread.  public void run() : is used to perform the action for a Thread. Starting a thread: The  start() method  of a Thread class is used to start a newly created thread. It performs the following tasks: A new thread starts(with their stack). The Thread moves to the Runnable state from New state. The  run() method  will run when the Thread gets a chance. Java Thread Example by extending Thread class: class  Multi  extends  Thread{   public   void  run(){   System.out.println( "Extended Thread Class E...