Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Daemon Thread in Java

Multithreading in Java allows for concurrent execution of multiple threads within a single program. One important concept in multithreading is the daemon thread.

In Java, a daemon thread is a special type of thread that provides services to user threads. Its existence is dependent on the user threads; once all user threads are terminated, the JVM automatically terminates the daemon thread as well.


Several Java daemon threads, such as the finalizer and gc, run automatically in the background.

In simple words, a daemon thread is there to support and assist user threads with tasks running in the background. Apart from serving user threads, it doesn't have any specific purpose.


Multithreading in Java brings the capability of running multiple threads simultaneously, and the concept of daemon threads helps in managing and supporting these user threads efficiently.


Java Daemon Thread Properties





  • Termination of JVM : When all user threads finish their execution, the JVM (Java Virtual Machine) can be stopped from running.
  • Daemon Thread Handling : If the JVM identifies the presence of an active daemon thread, it will terminate that thread and subsequently shut down the daemon. The status of the daemon thread, whether it is actively running or not, is not important to the JVM.
  • Lowest Priority : Daemon threads are assigned the lowest possible priority among all threads.

Daemon Thread vs User Threads

  • Priority : When a process consists only of daemon threads, the interpreter terminates. This makes sense because when there are only daemon threads remaining, there are no other threads available for the daemon thread to provide services to.
  • Usage : User threads receive services from daemon threads to handle supporting tasks in the background.

Why does JVM kill the daemon thread when there is no user thread running?





  • The purpose of a daemon thread is to assist the user thread with background supporting tasks. Therefore, if there are no user threads present, it is unnecessary for the JVM to continue running the daemon thread. Consequently, if there are no user threads, the JVM terminates the daemon thread.

Methods of Daemon Thread

  • boolean isDaemon() : This method is used to determine whether the currently active thread is a daemon thread. It returns true if the thread is a daemon, and false otherwise.
  • public final boolean isDaemon()


  • void setDaemon(boolean status) : By using this method, the current thread can be marked as either a user thread or a daemon thread.
  • public final void setDaemon(boolean on)

Simple example of Daemon thread in java


import java.io.*;
public class Main extends Thread{  
	 public void run(){  
		  if(Thread.currentThread().isDaemon()){ //checking for daemon thread  
		   System.out.println("Daemon thread work");  
		  }  
		  else{  
		  System.out.println("User thread work");  
		  }  
	 }  
	 public static void main(String[] args){  
	  Main thread1=new Main();//creating thread  
	  Main thread2=new Main();  
	  Main thread3=new Main();  
	  thread1.setDaemon(true);//now t1 is daemon thread    
	  thread1.start();//starting threads  
	  thread2.start();  
	  thread3.start();  
	}  
}



Output:

Daemon thread work
User thread work
User thread work 

Example 2 : Daemon thread in Java


public class Main extends Thread{  
    public void run(){  
		 System.out.println("Current Thread Name: "+Thread.currentThread().getName());  
		 System.out.println("Daemon Thread: "+Thread.currentThread().isDaemon());  
	 }  
	 public static void main(String[] args){  
	  Main thread1=new Main();//creating thread  
	  Main thread2=new Main();  
	  thread1.start();//starting threads 
	  thread1.setDaemon(true);//now t1 is daemon thread    
	  thread2.start();  
	}  
}

A user thread must not be started if you want to create a daemon because doing so will cause it to throw an IllegalThreadStateException.



Output:

Current Thread Name: Thread-0
Exception in thread "main" Daemon Thread: false
java.lang.IllegalThreadStateException
	at java.base/java.lang.Thread.setDaemon(Thread.java:1403)
	at HELLO/DockerTpoint.Main.main(Main.java:15) 
Thread Pool in java Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class

Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.