Java Threads Explained: Concurrency vs Parallelism and Why Every Java Program Is Multithreaded
What a Java thread is, how concurrency differs from parallelism, why even Hello World runs on a thread, and how per-thread call stacks power exceptions, debuggers and profilers. The starting point for modern Java concurrency.
kvi5hnuCode Sprout team member10 min readOn this page
Java had threads from its very first release in 1996, at a time when most mainstream languages left concurrency to operating-system libraries. Almost thirty years later, threads are still the unit that everything in Java concurrency is built on: thread pools, CompletableFuture, fork/join, virtual threads and structured concurrency. This course works through all of them, from the costs that made classic threads hard to scale to the APIs in Java 25, 26 and 27 that remove most of those costs.
This first article sets up the vocabulary: what a thread is, the difference between concurrency and parallelism, and why a thread matters even in a program that never creates one.
A short history of Java concurrency
Each step in Java's concurrency story fixed a problem left by the one before:
| Release | What arrived | Problem it addressed |
|---|---|---|
| Java 1.0 (1996) | Thread, Runnable, synchronized, wait/notify | Portable multithreading without OS-specific APIs |
| Java 5 (2004) | java.util.concurrent: executors, locks, atomics, concurrent collections | Hand-managed threads and low-level locking were error-prone |
| Java 7 (2011) | Fork/join framework | Using all cores for divide-and-conquer work |
| Java 8 (2014) | CompletableFuture, parallel streams | Composing asynchronous steps without nested callbacks |
| Java 9 (2017) | java.util.concurrent.Flow | A standard interface for reactive streams |
| Java 21 (2023) | Virtual threads (final) | Platform threads are too expensive to have one per waiting task |
| Java 25 (2025) | Scoped values (final) | Sharing context safely and cheaply with many threads |
| Java 21–27 | Structured concurrency (preview, final in Java 28) | Concurrent subtasks that leak, outlive their caller or hide failures |
Project Loom is the OpenJDK project that delivered the last three rows. Its goal was to keep the simple "one thread per task" style of programming while making it cheap enough to use at very large scale.
What a thread is
A thread is one independent path of execution inside a program. All threads of a Java process share the same heap, so they can read and write the same objects. Each thread also has things of its own:
- a program counter, recording which instruction it's executing;
- a call stack of frames, one per method call in progress, holding that call's local variables and parameters;
- its own state, name, priority and interrupt flag.
Sharing the heap makes it easy for threads to cooperate, and it's also what makes concurrency hard: two threads that update the same object without coordination can corrupt it. Later articles deal with that.
A classic Java thread, now called a platform thread, is a thin wrapper around an operating-system thread. The OS scheduler decides which threads run on which CPU cores and for how long. On a machine with several cores, several threads really do run at the same instant.
Concurrency vs parallelism
The two words are often used interchangeably, but they mean different things:
- Concurrency is about structure: a program is concurrent when it's made of tasks whose lifetimes overlap. They don't have to run at the same instant. One core can make progress on three tasks by switching between them, especially when each task spends time waiting.
- Parallelism is about execution: tasks run at literally the same moment on different cores. It needs more than one processing unit.

A restaurant makes a good picture of the difference. One waiter serving five tables is concurrency: each table makes progress, because the waiter takes an order here while the kitchen cooks there. Five waiters serving five tables at once is parallelism. A real restaurant uses both: several waiters, each handling several tables.
The distinction matters for this course because the two problems need different tools:
| Workload | Main limit | Helps most |
|---|---|---|
| Many tasks that mostly wait (HTTP calls, database queries, file reads) | how many waiting tasks you can afford to keep | concurrency: virtual threads, asynchronous I/O |
| A few tasks that mostly compute (image processing, sorting, simulation) | number of CPU cores | parallelism: fork/join, parallel streams, a pool sized to the cores |
Virtual threads, the headline feature of modern Java, are a concurrency tool. They let you have millions of tasks in progress, but they don't add cores. Keep that in mind and most of the advice later in the course follows naturally.
Every Java program already runs on threads
Even the simplest program is multithreaded. The JVM runs main on a thread it creates, named main, and it starts several helper threads of its own before your code runs. You can list the ones that are visible from Java:
import java.util.Comparator;
public class ListJvmThreads {
public static void main(String[] args) {
Thread.getAllStackTraces().keySet().stream()
.sorted(Comparator.comparing(Thread::getName))
.forEach(t -> System.out.println(t.getName() + " daemon=" + t.isDaemon()));
}
}
On JDK 25 this prints something like the following. The exact list depends on the JDK version and on the options and tools in use:
Common-Cleaner daemon=true
Finalizer daemon=true
Notification Thread daemon=true
Reference Handler daemon=true
Signal Dispatcher daemon=true
main daemon=false
Only main is a non-daemon thread, so the JVM exits when main (and any other non-daemon thread you started) finishes. The others support the runtime: the reference handler and cleaner process objects the garbage collector found unreachable, the signal dispatcher handles OS signals such as the "dump threads" signal (Ctrl+\ on Linux and macOS, Ctrl+Break on Windows), and the notification thread delivers management events.
The JVM also runs internal threads that getAllStackTraces doesn't show: garbage-collector workers, the just-in-time (JIT) compiler threads that turn hot bytecode into machine code, and the VM thread that performs operations needing the whole JVM paused. A thread dump taken with jcmd <pid> Thread.print lists them all.
Threads are the backbone of Java's tools
Because each thread has its own call stack, the thread is also the unit that Java's diagnostic tools work with.
Exceptions. A stack trace is a snapshot of one thread's call stack at the moment the exception was created. It answers "how did this thread get here?" When an exception escapes a thread's run method, the JVM reports it together with the thread's name, and the thread dies. Other threads keep running.
Debuggers. A debugger suspends, steps and inspects threads one at a time. "Step over" means "let this thread execute one line". In a multithreaded program you choose which thread's stack to look at.
Profilers. Sampling profilers repeatedly capture every thread's stack and count where time is spent. Monitoring tools such as JDK Flight Recorder record per-thread events: which thread blocked, on which lock, for how long.
All three depend on the call stack telling a coherent story: the frames show the sequence of calls that led to the current point. Keep this in mind for later chapters. Asynchronous styles split a request across many callbacks and threads, so the stack no longer tells that story. Virtual threads bring it back.
Seeing a per-thread stack trace
This program starts a named worker thread whose task fails deep in a call chain. An uncaught exception handler receives the failure along with the thread it happened on. Instead of printing the full trace, the handler prints just the method names, so you can see the stack the worker built up:
public class Solution {
static class ReportFailedException extends RuntimeException {
private static final long serialVersionUID = 1L;
ReportFailedException(String message, Throwable cause) { super(message, cause); }
}
public static void main(String[] args) throws InterruptedException {
System.out.println("main runs on: " + Thread.currentThread().getName());
Thread worker = new Thread(Solution::buildReport, "report-worker");
worker.setUncaughtExceptionHandler((thread, error) -> {
System.out.println("Failure in thread: " + thread.getName());
System.out.println(" " + error);
System.out.println(" caused by: " + error.getCause());
for (StackTraceElement frame : error.getStackTrace()) {
System.out.println(" at " + frame.getMethodName());
}
});
worker.start();
worker.join();
System.out.println("main is still alive: " + Thread.currentThread().isAlive());
}
static void buildReport() { loadRows(); }
static void loadRows() { readFile(); }
static void readFile() {
try {
throw new java.io.IOException("disk not ready");
} catch (java.io.IOException e) {
throw new ReportFailedException("cannot build report", e);
}
}
}
Output:
main runs on: main
Failure in thread: report-worker
Solution$ReportFailedException: cannot build report
caused by: java.io.IOException: disk not ready
at readFile
at loadRows
at buildReport
at run
main is still alive: true
Three details are worth noticing:
- The bottom frame is
run, theThread.runmethod, notmain. A new thread starts with an empty stack. Its trace shows nothing about who started it or why. That missing parent–child link is exactly what structured concurrency adds back in Part 4. - The failure is wrapped: the low-level
IOExceptionis the cause of a more meaningfulReportFailedException. Wrapping keeps the original information while describing the failure at the right level. mainis unaffected. An uncaught exception kills only the thread it escapes from. If nobody watches for it, which is the default, the failure is printed to standard error and is easy to miss.
Without the custom handler, the default behavior prints Exception in thread "report-worker" followed by the full trace.
What's current
- The
ThreadandRunnablebasics have barely changed since Java 1.0, which is why decades-old code still compiles. - Since Java 21,
Threadcomes in two kinds: platform threads (OS-backed) and virtual threads (scheduled by the JVM). Both use the same API, so everything in this article applies to both. - Thread dumps from
jcmd <pid> Thread.dump_to_file -format=jsoninclude virtual threads and the structured-concurrency scopes that own them. Part 2 and Part 4 cover them.
Common questions
Is a multithreaded program on a one-core machine concurrent or parallel?
Concurrent but not parallel. The OS switches the core between threads quickly, so all threads make progress, but only one runs at any instant. Waiting threads (on I/O or sleep) don't need the core at all, which is why concurrency still helps on one core.
Does adding threads make a CPU-bound program faster?
Only up to the number of available cores. Beyond that, threads compete for the same cores, and the switching overhead makes things slightly slower. Runtime.getRuntime().availableProcessors() tells you how many cores the JVM may use.
Why does a stack trace from a worker thread never mention main?
Each thread has its own call stack, and a new thread's stack starts at Thread.run. The stack of the thread that called start() belongs to a different thread. Debugging tools have to correlate the two by thread names or IDs.
Does an uncaught exception in one thread stop the program?
No. It ends only that thread. The JVM keeps running as long as at least one non-daemon thread is alive. A handler set with setUncaughtExceptionHandler or Thread.setDefaultUncaughtExceptionHandler lets you log such failures properly.
Summary
- A thread is an independent path of execution with its own program counter and call stack. All threads share the heap.
- Concurrency means overlapping tasks; parallelism means simultaneous execution on several cores. Waiting work needs concurrency, computing work needs parallelism.
- Every Java program runs on threads:
mainplus JVM helper threads such as the reference handler, signal dispatcher and GC threads. - Stack traces, debuggers and profilers all work per thread, so code that keeps one task on one thread is much easier to diagnose.
- An uncaught exception kills only its own thread, and its trace starts at
Thread.run.
Practice questions
A program downloads 1,000 web pages, each taking about one second, mostly waiting on the network. Does it need concurrency, parallelism or both?
Answer
Mainly concurrency. The tasks spend almost all their time waiting, so the goal is to have many downloads in progress at once. Even a single core could handle this if waiting tasks don't hold scarce resources. Parallelism only matters for the small amount of CPU work per page.
What does this program print, and in what order?
public class Solution {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> { throw new IllegalStateException("boom"); }, "worker-7");
t.setUncaughtExceptionHandler((th, e) -> System.out.println(th.getName() + " failed: " + e.getMessage()));
t.start();
t.join();
System.out.println("done");
}
}
Answer
worker-7 failed: boom
done
The handler runs on the worker thread before it terminates. join() waits for termination, so the handler's line always comes first. main continues normally because the exception only ended worker-7.
Which JVM threads does Thread.getAllStackTraces() not show, and why?
Answer
Internal JVM threads, such as garbage-collector workers, JIT compiler threads and the VM thread. GC workers and the VM thread have no java.lang.Thread object at all, and JIT compiler threads have one but are deliberately hidden from Java-level APIs. A jcmd <pid> Thread.print thread dump lists them all.
Previous: Modern Java Concurrency Course: Full Outline and Reading Order
Next: Java Threads: Creating and Starting Them, and Why start() Is Not run()

More in Modern Java Concurrency
Quiz: Java Concurrency Changes From Java 21 to Java 28
Sixteen questions on what changed release by release: virtual threads final in 21, synchronized without pinning in 24, scoped values final in 25, and the long road of structured concurrency from subclasses to open() and Joiner, the Java 26 and 27 refinements, and final in 28.
Quiz: Java Concurrency's Delicate Details
Twenty edge cases of modern Java concurrency that surprise experienced developers: virtual thread priorities and names, semaphores without owners, null scoped values, cancelling a CompletableFuture, pool sizing with unbounded queues, and StructuredTaskScope's strict rules.
Quiz: Java Concurrency Interview Questions (Virtual Threads, Structured Concurrency, Scoped Values)
Twenty questions of the kind interviewers ask about modern Java concurrency: platform vs virtual threads, pinning, pools and limits, CompletableFuture, structured concurrency, scoped values vs ThreadLocal, reactive programming, and production diagnostics.
Quiz: Modern Java Concurrency Final Exam (All 7 Parts)
The final exam for the modern Java concurrency course: thirty questions across all seven parts, from platform threads, pools and CompletableFuture through virtual threads, the scheduler, structured concurrency, scoped values, reactive streams and frameworks.
Discussion
Sign in to ask a question or share what you learned.