Java Daemon Thread
Daemon thread runs as a low priority thread in the background to perform certain tasks such as garbage collection, finalizer, etc.
- A daemon thread will execute only as long as the rest of the program continues to execute.
- When all user threads finish execution after JVM terminates itself.
- In Java, many daemon threads are running automatically e.g. gc, finalizer etc.
- It is a very low priority thread.
- The daemon thread life depends on user threads.
Syntax:
public void setDaemon(boolean status) public boolean isDaemon()
Methods for Java Daemon thread
There are two methods in daemon thread.
| S.No | Method | Description |
|---|---|---|
| 1 | public void setDaemon(boolean status) | It is used to mark the current thread as daemon thread or user thread. |
| 2 | public boolean isDaemon() | It is used to check that current is daemon. |
Example:
public class DaemonThread extends Thread {
public void run() {
if(Thread.currentThread().isDaemon()){
System.out.println("Daemon Thread");
}
else{
System.out.println("Normal Thread");
}
}
public static void main(String[] args) throws Exception {
DaemonThread t = new DaemonThread();
System.out.println("Current thread is " + t.currentThread());
t.setDaemon(true);
t.start();
}
}
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com