Java Daemon Thread Last Updated : 04 Oct, 2025 Suggest changes Share Like Article Like Report A daemon thread is a low-priority background thread that does not prevent the JVM from exiting when the program finishes executionDaemon threads run in the background to support user threads.The JVM exits automatically when all user (non-daemon) threads complete.They are created using the same Thread class but are marked as daemon using the setDaemon(true) method.The setDaemon(true) method must be called before the thread starts.Common examples include Garbage Collector and Finalizer Thread.Syntax Java thread.setDaemon(true); Use CasesGarbage Collection: The Garbage Collector (GC) in Java runs as a daemon thread.Background Monitoring: Daemon threads can monitor the state of application components, resources, or connections.Logging and Auditing Services: Daemon threads can be used to log background activities continuously.Cleanup Operations: Daemon threads may periodically clear temporary files, release unused resources, or perform cache cleanup.Scheduler or Timer Tasks: Background schedulers often use daemon threads to trigger tasks at fixed intervals.Methods Usedvoid setDaemon(boolean on): Marks a thread as daemon or user thread. Must be called before start().boolean isDaemon(): Checks whether a thread is daemon.Creating a Daemon Thread Java public class DaemonExample extends Thread { public void run() { if (Thread.currentThread().isDaemon()) { System.out.println("Daemon thread running..."); } else { System.out.println("User thread running..."); } } public static void main(String[] args) { DaemonExample t1 = new DaemonExample(); DaemonExample t2 = new DaemonExample(); t1.setDaemon(true); // must be set before start() t1.start(); t2.start(); } } OutputDaemon thread running... User thread running... Behavior of Daemon Thread Java public class DaemonBehavior extends Thread { public void run() { while (true) { System.out.println("Daemon thread running..."); } } public static void main(String[] args) { DaemonBehavior t = new DaemonBehavior(); t.setDaemon(true); t.start(); System.out.println("Main (user) thread ends..."); } } OutputMain (user) thread ends... The JVM ends immediately after the main thread finishes, even though the daemon thread is still running.Important NotesA thread inherits the daemon status of the thread that creates it.The setDaemon(true) method throws IllegalThreadStateException if called after start().Daemon threads should not be used for tasks requiring completion, such as writing to a file or updating a database.JVM terminates all daemon threads abruptly without performing cleanup operations.Difference Between User Thread and Daemon ThreadBasisUser ThreadDaemon ThreadPurposeExecutes main application tasksPerforms background servicesLifecycleKeeps JVM alive until finishedTerminates when all user threads finishPriorityUsually higherUsually lowerJVM ExitJVM waits for completionJVM exits even if runningExamplesMain thread, worker threadsGarbage collector, background monitors K kartik Follow Article Tags : Misc Java Java-Multithreading java-advanced Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings8 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java9 min readAccess Modifiers in Java4 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read My Profile ${profileImgHtml} My Profile Edit Profile My Courses Join Community Transactions Logout Like