Skip to content

Threads Types

Short answer: 2 main types But like most things in Java, there’s a deeper layer if you peek under the hood πŸ‘€


🧠 1. User Threads (a.k.a. Non-Daemon Threads)

These are the main actors on stage 🎭

  • Created by your application
  • JVM waits for them to finish
  • Keep the program alive
Thread t = new Thread(() -> {
    System.out.println("User thread running");
});
t.

start();

πŸ‘‰ Examples:

  • main() thread
  • Worker threads
  • Business logic threads

πŸŒ™ 2. Daemon Threads

These are the background workers πŸ› οΈ

  • Run in background
  • JVM does NOT wait for them
  • Die automatically when all user threads finish
Thread t = new Thread(() -> {
    while (true) {
        System.out.println("Daemon working...");
    }
});
t.setDaemon(true);
t.start();

πŸ‘‰ Examples:

  • Garbage Collector thread
  • JVM housekeeping threads

βš”οΈ User vs Daemon

Feature User Thread πŸ§‘ Daemon Thread πŸŒ™
Purpose Main work Background work
JVM waits? βœ” Yes ❌ No
Lifecycle Independent Depends on user threads

🧠 Internal JVM Threads (bonus insight)

Even if you don’t create threads, JVM already runs some:

  • Garbage Collector
  • JIT Compiler
  • Signal Dispatcher

πŸ‘‰ Most of these are daemon threads


⚠️ Important Rule

t.setDaemon(true);

πŸ‘‰ Must be called before start() Otherwise β†’ IllegalThreadStateException


🎯 Interview Answer

Java has two main types of threads: User threads and Daemon threads. User threads perform main application tasks and keep JVM alive, while daemon threads run in the background and terminate when all user threads finish.


🧠 Memory Trick

User Thread = Life of program πŸ§‘ Daemon Thread = Support system πŸŒ™


If you want deeper:

  • Thread lifecycle (NEW β†’ RUNNABLE β†’ BLOCKED...)
  • Or how threads actually map to OS threads (important for interviews)