
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Importance of isDaemon Method in Java
A daemon thread is a low-priority thread in java which runs in the background and mostly created by JVM for performing background tasks like Garbage Collection(GC).
If no user thread is running then JVM can exit even if daemon threads are running. The only purpose of a daemon thread is to serve user threads. The isDaemon() method can be used to determine the thread is daemon thread or not.
Syntax
Public boolean isDaemon()
Example
class SampleThread implements Runnable { public void run() { if(Thread.currentThread().isDaemon()) System.out.println(Thread.currentThread().getName()+" is daemon thread"); else System.out.println(Thread.currentThread().getName()+" is user thread"); } } // Main class public class DaemonThreadTest { public static void main(String[] args){ SampleThread st = new SampleThread(); Thread th1 = new Thread(st,"Thread 1"); Thread th2 = new Thread(st,"Thread 2"); th2.setDaemon(true); // set the thread th2 to daemon. th1.start(); th2.start(); } }
Output
Thread 1 is user thread Thread 2 is daemon thread
Advertisements