Multithreading: a daemon thread never blocks JVM shutdown

A thread is non-daemon by default, and setDaemon(true) must be called before start(). Daemon status only affects whether the JVM waits for the thread on exit; it does not change how the thread runs.

Code
Thread worker = new Thread(() -> { });
System.out.println("Default daemon flag: " + worker.isDaemon());
worker.setDaemon(true);
System.out.println("After setDaemon(true): " + worker.isDaemon());
worker.start();
worker.join();
System.out.println("Daemon thread finished normally");
Output
Default daemon flag: false
After setDaemon(true): true
Daemon thread finished normally
Advertisement
More in JAVA

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-09-27

© Java Coding Hub · About · Contact · Privacy · Terms