[go: up one dir, main page]

0% found this document useful (0 votes)
2 views3 pages

5 - 1 - 8 - Daemon Thread

Daemon threads in Java provide background services to user threads and are terminated automatically when all user threads finish. They are created by the JVM and have lower priority compared to user threads, which are designed to perform specific tasks and are not forcibly terminated by the JVM. Two methods for managing daemon threads include setting a thread as a daemon and checking if a thread is a daemon.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

5 - 1 - 8 - Daemon Thread

Daemon threads in Java provide background services to user threads and are terminated automatically when all user threads finish. They are created by the JVM and have lower priority compared to user threads, which are designed to perform specific tasks and are not forcibly terminated by the JVM. Two methods for managing daemon threads include setting a thread as a daemon and checking if a thread is a daemon.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Daemon Thread

o It provides services to user threads for background supporting tasks.


o Its life depends on user threads.
o It is a service provider thread that provides services to the user thread.
o Its life depends on the mercy of user threads i.e. when all the user threads die, JVM terminates
this thread automatically. There are many Daemon threads in java running automatically e.g. gc,
finalizer, etc.

Two Methods for Java Daemon thread by Thread class

No. Method Description

1) public void setDaemon(boolean status) is used to mark the current thread as daemon
thread or user thread.

2) public boolean isDaemon() is used to check that current is daemon.


#1) User thread: user thread is created when the application first starts. Then we can create as many user threads.
#2) Daemon thread: daemon threads are mainly used in the background and are used for tasks like cleaning the application, etc.

User Threads Vs Daemon Threads In Java :

User Threads Daemon Threads


User threads are mainly designed to do Daemon threads are designed to support
some specific task. the user threads.

JVM waits for user threads to finish JVM will not wait for daemon threads to
their work. It will not exit until all user finish their work. It will exit as soon as
threads finish their work. all user threads finish their work.
Daemon threads are background
User threads are foreground threads.
threads.
Daemon threads are low priority
User threads are high priority threads.
threads.
User threads are created by the Daemon threads, in most of time, are
application. created by the JVM.
JVM will not force the user threads to JVM will force the daemon threads to
terminate. It will wait for user threads to terminate if all user threads have
terminate themselves. finished their work.

Example
public class TestDaemonThread1 extends Thread output:
{ daemon thread work
user thread work
public void run() user thread work
{
if(Thread.currentThread().isDaemon())
{
System.out.println("daemon thread work");
}
else
{
System.out.println("user thread work");
}
}
public static void main(String[] args)
{
TestDaemonThread1 t1=new TestDaemonThread1();
TestDaemonThread1 t2=new TestDaemonThread1();
TestDaemonThread1 t3=new TestDaemonThread1();

t1.setDaemon(true);

t1.start();
t2.start();
t3.start();
}
}

Example1

You might also like