0 ratings0% found this document useful (0 votes) 48 views8 pagesDaemon Thread in Java
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
s21pamna, 11:87 PM.
|nps:siwwjavatpoin.comidaemon-thread
Daemon Thread in Java -javatpoint
18s21pamna, 11:87 PM. Daemon Thread in Java -javatpoint
Daemon Thread in Java
Daemon thread in Java is a service provider thread that provides services to the user thread. Its life
depend on the mercy of user threads ie. when all the user threads dies, JVM terminates this thread
automatically.
There are many java daemon threads running automatically e.g. gc, finalizer etc.
You can see all the detail by typing the jconsole in the command prompt. The jconsole tool provides
information about the loaded classes, memory usage, running threads ete.
Points to remember for Daemon Thread in Java
© It provides services to user threads for background supporting tasks. It has no role in life than to
serve user threads.
© Its life depends on user threads.
© It is a low priority thread.
Why JVM terminates the daemon thread if there is no user thread?
The sole purpose of the daemon thread is that it provides services to user thread for background
supporting task. If there is no user thread, why should JVM keep running this thread. That is why JVM
terminates the daemon thread if there is no user thread.
Methods for Java Daemon thread by Thread class
The java.lang.Thread class provides two methods for java daemon thread.
‘nps:swwjavatpoin.comidaemon-threadsa1raina, 10:87 PM. Daemon Thread in Java -javatpoint
No. Method Description
1) public void setDaemon(boolean is used to mark the current thread as daemon thread
status) or user thread.
2) public boolean isDaemon() is used to check that current is daemon.
Simple example of Daemon thread in java
File: MyThread,java
public class TestDaemonThread1 extends Thread{
public void run0{
if(Thread.currentThread(.isDaemon(){//checking for daemon thread
System.out.printIn(“daemon thread work");
)
else
System.out.printin(“user thread work");
)
)
public static void main(String[] args){
TestDaemonThread1 t1=new TestDaemonThread1();//creating thread
TestDaemonThread1 t2=new TestDaemonThread1();
TestDaemonThread1 t3=new TestDaemonThreadi1();
tl.setDaemon(true);//now t1 is daemon thread
tl startQ;//starting threads
t2.start0;
t3.start();
)
)
Output:
‘nps:swwjavatpoin.comidaemon-thread aesa1raina, 11:87 PM. Daemon Thread in Java -javatpoint
daemon thread work
work
id work
Note: If you want to make a user thread as Daemon, it must not be started otherwise it will throw
IllegalThreadStateException.
File: MyThread,java
class TestDaemonThread2 extends Thread{
public void run0(
System.out printin("Name: “+Thread.currentThread().getName();
System.out printin("Daemon: "+Thread.currentThread()isDaemon();
)
public static void main(Stringl] args){
TestDaemonThread2 t1
ew TestDaemonThread2();
TestDaemonThread2 t2=new TestDaemonThread20;
tH startd;
tl_setDaemon(true);//will throw exception here
t2startd;
}
)
Test it Now
Output:
exception in thread main: java. lang. Illegal ThreadstateExceptior
Pre Next >
‘nps:swwjavatpoin.comidaemon-thread 48s21pamna, 11:87 PM. Daemon Thread in Java -javatpoint
#Youtube For Videos Join Our Youtube Channel: Join Now
Feedback
© Send your Feedback to feedback @javatpoint.com
Help Others, Please Share
Bog
‘nps:swwjavatpoin.comidaemon-threads21pamna, 11:87 PM.
Learn Latest Tutorials
Splunk tutorial
Splunk
Tumblr tutorial
Tumblr
BIR Programming
tutorial
R Programming
& Python Pillow.
tutorial
Python Pillow
Preparation
Aptitude
Aptitude
2 Company
Interview
Questions
Company Questions
|nps:siwwjavatpoin.comidaemon-thread
SPSS tutorial
SPs
# React tutorial
ReactlS
ARIS tutorial
Rus
& Python Turtle
‘tutorial
Python Turtle
Logical
Reasoning
Reasoning
Daemon Thread in Java -javatpoint
2) Swagger
tutorial
Swagger
Regex tutorial
Regex
#) React Native
tutorial
React Native
#Keras tutorial
Keras
LANerbal Ability
Verbal Ability
2T-SQL tutorial
Transact-SQL
i) Reinforcement
Iearning tutorial
Reinforcement
Leaning
A Python Design
Patterns
Python Design
Pattems
1A) Interview
Questions
Interview Questions21222, 1:87 PM
Trending Technologies
#1 Artificial LAWS Tutorial
Intelligence ws
Artificial
Intelligence
\SHadoop tutorial = g}ReactIS.
Hadoop Tutorial
ReactIS
2 Blockchain (AGit Tutorial
Tutorial
Git
Blockchain
B.Tech / MCA
DBMS tutorial —_g Data Structures
ems tutorial
Data Structures
B Computer BE Compiler
Network tutorial Design tutorial
Computer Network Compiler Design
‘nps:siwwjavatpoin.comidaemon-thread
Daemon Thread in Java -javatpoint
2) Selenium 2 Cloud
tutorial ‘Computing
Selenium Cloud Computing
&) Data Science A Angular 7
Tutorial Tutorial
Data Science Angular 7
2) Machine 2 DevOps
Learning Tutorial Tutorial
Machine Learning DevOps
DAA tutorial 2) Operating
DAA System
Operating System
2) Computer A Discrete
‘Organization and Mathematics
Architecture Tutorial21222, 11:87 PM Daemon Thread in Java -javatpoint
Computer Diserete
Organization Mathematies
PEthical Hacking Computer A_ Software \otthtm! tutorial
Ethical Hacking Graphics Tutorial Engineering Web Technolony
Computer Graphics Software
Engineering
#)Cyber Security Automata 2) C Language
tutorial Tutorial tutorial cu
Cyber Security Automata Programming
(Flava tutorial iNet Python tutorial — ig} List of
Framework Programs
Java torial Python
Programs
Net
2 Control Data Mining ‘Data
‘Systems tutorial Tutorial Warehouse
Control System Data Mining Tutorial
Data Warehouse
‘nps:swwjavatpoin.comidaemon-thread