Member-only story
User Threads vs Daemon Threads in Java — Explained with Examples
🔒 This is a Medium member-only article. If you’re not a Medium member, you can read the full article for free on my blog: Daemon Thread vs User Thread in Java.
In Java multithreading, all threads are categorized into two types: User Threads and Daemon Threads. Understanding the difference between these two is crucial for writing efficient, reliable, and properly-managed concurrent applications.
While both user and daemon threads share a similar structure and behavior (they are both instances of the Thread
class), they serve very different purposes.
In this article, we will explore the core differences between user threads and daemon threads, how the JVM handles them, and how to create and use them effectively.
What is a User Thread?
A User Thread is a foreground thread in Java that performs a specific task intended by the application. These are the threads that do the actual work — like processing user input, handling requests, reading or writing files, or performing calculations.
🔍 Key Characteristics:
- JVM waits for all user threads to finish before exiting.
- They are created by the application code.
- Typically high-priority threads.
- Run in the foreground and control the lifecycle of the JVM.
- Used to perform core business logic or task execution.
Example of a User Thread:
public class UserThreadExample {
public static void main(String[] args) {
Thread userThread = new Thread(() -> {
for (int i = 1; i <= 3; i++) {
System.out.println("User thread running: " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Interrupted");
}
}
});
userThread.start();
}
}
✅ Output:
User thread running: 1
User thread running: 2
User thread running: 3
In this case, the JVM waits for the user thread to complete before exiting the application.