0% found this document useful (0 votes)
82 views

Daemon Thread in Java With Example: Properties of Daemon Threads

Daemon threads are low priority background threads in Java that perform tasks like garbage collection. The JVM can exit even if daemon threads are still running once all user threads have finished. Daemon threads inherit their status from their parent thread and can be set using the setDaemon() method before starting the thread. The key difference between daemon and user threads is that the JVM will wait for user threads to finish but can exit without waiting for daemon threads.

Uploaded by

NAKKA PUNEETH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views

Daemon Thread in Java With Example: Properties of Daemon Threads

Daemon threads are low priority background threads in Java that perform tasks like garbage collection. The JVM can exit even if daemon threads are still running once all user threads have finished. Daemon threads inherit their status from their parent thread and can be set using the setDaemon() method before starting the thread. The key difference between daemon and user threads is that the JVM will wait for user threads to finish but can exit without waiting for daemon threads.

Uploaded by

NAKKA PUNEETH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Daemon thread in Java with example

Daemon thread is a low priority thread (in context of JVM) that runs in background to
perform tasks such as garbage collection (gc) etc., they do not prevent the JVM from
exiting (even if the daemon thread itself is running) when all the user threads (non-
daemon threads) finish their execution.
JVM terminates itself when all user threads (non-daemon threads) finish their
execution, JVM does not care whether Daemon thread is running or not, if JVM finds
running daemon thread (upon completion of user threads), it terminates the thread
and after that shutdown itself.

Properties of Daemon threads:


1.A newly created thread inherits the daemon status of its parent. That’s the
reason all threads created inside main method (child threads of main thread)
are non-daemon by default, because main thread is non-daemon. However you
can make a user thread to Daemon by using setDaemon() method of thread
class.
Just a quick note on main thread: When the JVM starts, it creates a thread
called “Main”. Your program will run on this thread, unless you create additional
threads yourself. The first thing the “Main” thread does is to look for your static
void main (String args[]) method and invoke it. That is the entry-point to your
program. If you create additional threads in the main method those threads
would be the child threads of main thread.

2.Methods of Thread class that are related to Daemon threads:


public void setDaemon(boolean status): This method is used for making a user
thread to Daemon thread or vice versa. For example if I have a user thread t
then t.setDaemon(true) would make it Daemon thread. On the other hand if I
have a Daemon thread td then by calling td.setDaemon(false) would make it
normal thread(user thread/non-daemon thread).
public boolean isDaemon(): This method is used for checking the status of a
thread. It returns true if the thread is Daemon else it returns false.
Daemon thread in Java with example

3.setDaemon() method can only be called before starting the thread. This
method would throw IllegalThreadStateException if you call this method after
Thread.start() method. (refer the example)

Difference between Daemon threads and non-Daemon thread (user thread)


The main difference between Daemon thread and user threads is that the JVM does
not wait for Daemon thread before exiting while it waits for user threads, it does not
exit until unless all the user threads finish their execution.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy