Main Thread in Java Applications
Fast recall
Quick revision
Core answer
public static void main(String[] args). It is not necessarily the first thread the JVM process creates, because the runtime may have internal service threads. When main returns, the JVM continues while any started non-daemon thread is still alive.Basic thread awareness — understanding how Java programs start and manage lifecycle
Thinking the JVM exits as soon as main method returns — it waits for all non-daemon threads
Mention daemon threads and how they differ from non-daemon threads in JVM shutdown behavior
Interview-ready guide
Interview answer
The main thread is the application thread used by the Java launcher to invoke the main method. I would not call it the first JVM thread or the parent of every other thread, because the runtime and libraries can create internal threads independently.
When main returns, that entry work is finished, but the JVM remains alive while any started non-daemon thread is still running. If only daemon threads remain, the JVM may terminate without waiting for their work to finish.
Main Thread in Java Applications
Direct Answer
The main thread is the application thread that invokes public static void main(String[] args). It is not necessarily the first thread the JVM process creates, because the runtime may have internal service threads. When main returns, the JVM continues while any started non-daemon thread is still alive.
Code Example
public class MainThreadDemo {
public static void main(String[] args) {
// This code runs in the main thread
System.out.println("Main thread: " + Thread.currentThread().getName());
// Spawn a non-daemon thread
var workerThread = new Thread(() -> {
try {
Thread.sleep(3000);
System.out.println("Worker finished after main");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
workerThread.start(); // Non-daemon by default
// Spawn a daemon thread
var daemonThread = new Thread(() -> {
while (true) {
System.out.println("Daemon running...");
try { Thread.sleep(500); } catch (InterruptedException e) { break; }
}
});
daemonThread.setDaemon(true); // Mark as daemon BEFORE start()
daemonThread.start();
System.out.println("Main method ending");
// JVM waits for workerThread but kills daemonThread
}
}
Common Mistakes & Pitfalls
- Expecting Immediate Exit: Freshers start background threads in
main, then wonder why their program hangs aftermainreturns. Those threads are non-daemon by default, so the JVM waits for them. - Forgetting setDaemon: Creating monitoring or logging threads without marking them daemon, causing the application to never exit even after the main work is done.
- Not Joining Threads: In test code, the main thread finishes before spawned threads complete their assertions, causing flaky tests that pass or fail depending on timing.
Check your understanding
Answer these without looking back at the article.
- What is a daemon thread in Java?
- What happens if main() throws an uncaught exception?
- How do you set a thread as a daemon thread?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.