Multithreading
Thread Priority
Understand thread priority, daemon threads, and their impact on application lifecycle.
Interview: Focuses on daemon thread rules (JVM shutdown behavior) and the pitfalls of relying on Thread Priorities.
Java threads support priority configurations and execution roles (daemon vs. user threads). Relying on priorities for execution ordering is a major concurrency anti-pattern, whereas setting daemon status correctly is critical for clean application termination.
Core Idea
Daemon threads run background tasks. The JVM will exit immediately once all non-daemon (user) threads have finished executing.
Why It Matters
Forgetting to mark a worker thread as a daemon can prevent the JVM process from shutting down properly.
Interview Lens
Focuses on thread priorities' platform dependency and daemon thread JVM shutdown rules.
Daemon Threads vs. User Threads
Every Java thread is either a user thread or a daemon thread:
- User (Non-Daemon) Threads: Spawned to execute main business logic. The JVM will wait for user threads to complete before exiting.
- Daemon Threads: Spawned to run background services (e.g. Garbage Collector, monitor loops). The JVM will terminate daemon threads immediately when all user threads exit, without running any
finallyblocks on them.
Thread Priority Pitfall
You can assign a priority to a thread using setPriority(int) with values from 1 (MIN_PRIORITY) to 10 (MAX_PRIORITY). However:
Warning: Java thread priorities are only hints to the underlying OS scheduler. The OS scheduler can ignore priorities completely, and prioritize low-priority threads due to scheduler heuristics. Relaing on priorities for thread synchronization results in thread starvation bugs and platform-dependent failures.
Code Walkthrough
This program shows how setting a thread as daemon changes JVM exit behavior.
public class DaemonThreadDemo { public static void main(String[] args) { Thread daemonWorker = new Thread(() -> { while (true) { try { System.out.println("Daemon processing background stats..."); Thread.sleep(1000); } catch (InterruptedException e) { break; } } });daemonWorker.setDaemon(true); // Must be set BEFORE starting the thread daemonWorker.start();
System.out.println("Main user thread finishes execution. JVM exits now."); // The JVM exits immediately and kills daemonWorker, ignoring finally blocks inside it. } }
Interview-Relevant Information
Q: What happens if you call setDaemon(true) on a thread after calling start()?
Answer: It will throw an IllegalThreadStateException at runtime. A thread's daemon status is immutable once execution begins.
Quick Checklist
Can low-priority threads still run before high-priority ones? What happens to daemon threads when main finishes? If yes, you understand daemon threads.
Use Cases
Running telemetry senders that shouldn't block application shutdown.
Background file system monitoring or logging tasks.
Common Mistakes
Forgetting to call setDaemon(true) before starting the thread, leaving a zombie user thread that locks up JVM exit.
Writing critical disk write operations in a daemon thread (finally blocks might not execute when JVM exits).