#Threads compared with the Node.js event loop
AddedIn Node.js, your JavaScript runs on one thread. The event loop takes the next callback off a queue, runs it to the end, and takes the next. Waiting (for a socket, a file, a timer) happens outside your code, in libuv and the operating system, so one thread can juggle thousands of slow requests. The price is that one CPU-heavy callback stops everything: while it runs, no other request is served, and you reach for worker threads or more processes.
Java starts from the opposite end. A Java program can run many threads at once, truly in parallel on all cores, and they all share the same heap. Every object one thread can reach, another thread can read and change at the same moment. A thread that blocks (sleeps, waits for the database) just stops; the operating system runs the other threads meanwhile. That's why classic Java servers are thread-per-request: Tomcat, which Spring Boot uses by default, serves each request on its own thread from a pool of up to 200 (server.tomcat.threads.max, default 200 in Spring Boot 4.2), and your code is plain blocking code with no await anywhere.
The mental model: Node is one very fast waiter running between all the tables; Java is a waiter per table. A waiter per table needs no clever scheduling and can do heavy work without holding anyone up, but waiters are expensive (each platform thread is an OS thread with its own stack), and two waiters can grab the same plate. That second problem, shared mutable memory, is what most of this chapter is about.
| Node.js | Java | |
|---|---|---|
| Your code runs on | one thread (plus optional worker threads) | as many threads as you start |
| Waiting for I/O | await: the thread moves on | blocking call: this thread waits, others run |
| CPU-heavy work | blocks every request | runs in parallel on other cores |
| Memory between threads | not shared (workers pass messages; SharedArrayBuffer aside) | shared by default: the same objects |
| Typical bug | a callback that blocks the loop; a logical race across an await | a data race: two threads changing one object |
| Cheap concurrency | built in (the event loop) | virtual threads (Java 21), later in this chapter |
Here is the difference made visible: a CPU-heavy job and three quick "requests". In Java the heavy job runs on its own thread and the requests are served while it works. The same program in Node serves nothing until the heavy job ends.
void main() throws InterruptedException {
var log = Collections.synchronizedList(new ArrayList<String>());
Thread report = Thread.ofPlatform().name("report").start(() -> {
long end = System.nanoTime() + 1_000_000_000L; // 1 s of pure CPU work
while (System.nanoTime() < end) { }
log.add("report done");
});
for (int i = 1; i <= 3; i++) {
log.add("request " + i + " served");
Thread.sleep(100);
}
report.join();
IO.println(log);
}$ java Waiters.java
[request 1 served, request 2 served, request 3 served, report done]const log = [];
setTimeout(() => {
const end = Date.now() + 1000; // 1 s of pure CPU work
while (Date.now() < end) { }
log.push("report done");
}, 0);
for (let i = 1; i <= 3; i++) {
setTimeout(() => log.push(`request ${i} served`), 10 + i * 100);
}
setTimeout(() => console.log(log), 1500);$ node waiters.mjs
[
'report done',
'request 1 served',
'request 2 served',
'request 3 served'
]- Java:
Thread.ofPlatform().name("report").start(…)(the builder API, Java 21) creates an OS thread and starts it. From now on two threads run at once:reportspins the CPU for one second, andmaincarries on with the loop. main"serves" a request every 100 ms.Thread.sleep(100)blocks onlymain;reportkeeps running on another core. The three requests are logged at roughly 0, 100 and 200 ms, long before the report ends.report.join()makesmainwait until thereportthread has finished, so the report's entry is in the list before it's printed:[request 1 served, request 2 served, request 3 served, report done].- Both threads add to the same list object. That's normal in Java, and it's why the list is wrapped in
Collections.synchronizedList: a plainArrayListisn't safe when two threads change it at once (the next topic shows what goes wrong). - Node: the report callback runs first and holds the only thread for a full second. The request timers are due at 110, 210 and 310 ms, but nothing can run until the busy loop ends, so they all fire late and
'report done'is first in the list Node prints. - Neither is "better": Node's model needs no locks because nothing runs in parallel; Java's model uses every core but makes shared data your problem.
A platform thread (the only kind before Java 21) is a thin wrapper around an operating-system thread. Each one reserves its own stack (the -Xss default is 2 MB on macOS and Linux on ARM, 1 MB on x64, per the java man page), and the OS schedules them. A few hundred are fine; tens of thousands strain memory and scheduling. That cost is why Java code has used thread pools for 20 years (reuse a small set of threads for many tasks), and why virtual threads (later in this chapter) were added: threads cheap enough to have one per task again.