What is a Daemon Thread in Java

๐Ÿ’ก Concept: Daemon Thread

A daemon thread is a background thread that runs in the background to support user threads. JVM exits when only daemon threads remain.

๐Ÿ“˜ Quick Intro

Daemon threads are service providers for user threads. They run low-priority tasks like garbage collection and do not prevent JVM termination.

๐Ÿง  Analogy

Daemon threads are like janitors in a building โ€” they do background tasks quietly and leave when all the people (user threads) are gone.

๐Ÿ”ง Technical Explanation

  • Daemon threads are created using Thread.setDaemon(true) before start().
  • They automatically terminate when all user threads finish.
  • Used for background support tasks like logging, GC, etc.
  • Cannot convert a thread to daemon after starting it.
  • JVM doesn't wait for daemon threads to finish on shutdown.

๐ŸŽฏ Use Cases

  • โœ… Background logging services.
  • โœ… Garbage collection.
  • โœ… Monitoring or housekeeping threads.

๐Ÿ’ป Example: Daemon Thread


public class DaemonExample {
    public static void main(String[] args) {
        Thread daemonThread = new Thread(() -> {
            while (true) {
                System.out.println("Daemon thread running...");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        daemonThread.setDaemon(true);
        daemonThread.start();

        System.out.println("Main thread finished");
    }
}

โ“ Interview Q&A

Q1: What is a daemon thread?
A: A background thread that doesn't block JVM exit.

Q2: How to make a thread daemon?
A: Call setDaemon(true) before starting the thread.

Q3: Can daemon thread prevent JVM exit?
A: No, JVM exits when only daemon threads remain.

Q4: Can we set daemon after start()?
A: No, it throws IllegalThreadStateException.

Q5: Is garbage collection a daemon thread?
A: Yes.

Q6: Can daemon thread create another daemon thread?
A: Yes.

Q7: Are daemon threads low priority?
A: Often, but not enforced.

Q8: What happens to incomplete daemon tasks on JVM shutdown?
A: They are abandoned.

Q9: Can user thread become daemon?
A: Only before start().

Q10: What are typical examples of daemon threads?
A: GC, timer threads, logging.

๐Ÿ“ MCQs

Q1. What is the purpose of a daemon thread?

  • Handle exceptions
  • Start other threads
  • Run background tasks without blocking JVM shutdown
  • None of the above

Q2. How do you create a daemon thread?

  • Call run()
  • Use static method
  • Call setDaemon(true) before start()
  • Subclass Thread

Q3. Do daemon threads stop JVM from exiting?

  • Yes
  • No
  • Sometimes
  • Only in exceptions

Q4. What happens if you call setDaemon(true) after start()?

  • Nothing
  • Throws IllegalThreadStateException
  • Thread becomes user
  • JVM exits

Q5. Is garbage collector a daemon thread?

  • No
  • Yes
  • Sometimes
  • Not applicable

Q6. What is a common use of daemon threads?

  • UI rendering
  • File upload
  • Logging
  • Networking

Q7. Can daemon threads create more daemon threads?

  • No
  • Yes
  • Only in Java 11+
  • Only with ExecutorService

Q8. Do daemon threads get executed after main thread ends?

  • Always
  • Never
  • Only if JVM is still running
  • Only if not sleeping

Q9. What priority do daemon threads have?

  • High
  • Medium
  • Usually low
  • Same as main thread

Q10. What happens to running daemon threads on JVM exit?

  • They complete
  • They sleep
  • They are terminated immediately
  • They turn into user threads

๐Ÿ’ก Bonus Insight

Daemon threads are ideal for background support logic, but they should never be used for tasks that require guaranteed completion.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

๐Ÿ” Navigation

Share:

Tags:


Feedback Modal Popup