Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

How long does this take to run, roughly, and how would the answer change with

Java
Executors.newFixedThreadPool(200)? try (var ex = Executors.newVirtualThreadPerTaskExecutor()) {
for (int i = 0; i < 10_000; i++) ex.submit(() -> {
Thread.sleep(1000);
return null;
});
}

With newVirtualThreadPerTaskExecutor(), all 10,000 tasks sleep at the same time, so the block takes about one second plus a little overhead, and the try-with-resources close() waits for all of them to finish. Thread.sleep in a virtual thread unmounts it, so only a handful of carrier threads are needed. With Executors.newFixedThreadPool(200), only 200 tasks can sleep at once, so the 10,000 tasks run in 50 rounds of one second each, about 50 seconds. Virtual threads raise the throughput of waiting tasks; they do not make any single task faster.

Why this question is asked

This small snippet is a favourite interview question because it separates people who know that virtual threads are fast from people who know why. The answer is a simple calculation once we understand what each thread is doing while it sleeps. In this answer, we will walk through the code line by line, see what happens on the virtual-thread executor, then do the same calculation for a fixed pool of 200, and finally look at what the result does and does not tell us about real workloads.

1Reading the Code

Let us first be clear about what the code does. It creates an executor that starts a new virtual thread for every task, submits 10,000 tasks, and each task sleeps for one second and returns null. The lambda returns a value, so it is a Callable, which is allowed to throw the checked InterruptedException that Thread.sleep declares; that is why the code compiles without a try-catch.

Java
Instant start = Instant.now();
try (var ex = Executors.newVirtualThreadPerTaskExecutor()) {
for (int i = 0; i < 10_000; i++)
ex.submit(() -> { Thread.sleep(1000); return null; }); // Callable: may throw InterruptedException
} // close() waits for all 10,000 tasks
System.out.println(Duration.between(start, Instant.now()).toMillis()); // about 1000 to 1200

The try-with-resources block matters. Since Java 19, ExecutorService implements AutoCloseable, and its close() method shuts the executor down and waits until all submitted tasks have completed. So the block does not end when the loop finishes submitting; it ends when the last task wakes up. Without it, we would be measuring how long it takes to submit, not to run.

2What Happens on the Virtual-Thread Executor

Each submit() creates a new virtual thread and schedules it. The virtual thread is mounted on a carrier, a platform thread of the scheduler's ForkJoinPool, runs until Thread.sleep(1000), and then unmounts: its small stack is saved on the heap, a timer is set to wake it, and the carrier immediately picks up the next virtual thread. Because each task runs for only microseconds before it sleeps, a handful of carriers, about one per CPU core, can start all 10,000 tasks in a few milliseconds.

So all 10,000 virtual threads are asleep at the same time. After one second their timers fire, they are scheduled again, each runs for a moment to return null, and they finish. The total is about one second plus the overhead of creating and scheduling 10,000 threads, typically tens of milliseconds.

Step throughOne task on the virtual-thread executor
Main threadExecutorVirtual threadCarrier
submit(task) x 10,000
start a new virtual thread
mount and run
Thread.sleep(1000)
unmount: carrier runs the next task
after 1 s: mount again, return null
close(): wait for all tasks
all done after about 1 s

8 messages in order. Play them, or step through them one at a time.

3The Same Code With newFixedThreadPool(200)

Now replace the executor with a fixed pool of 200 platform threads. Each task still sleeps for one second, but a platform thread that sleeps keeps its OS thread busy for that whole second. Only 200 tasks can be sleeping at any moment; the other 9,800 wait in the pool's queue.

Java
try (var ex = Executors.newFixedThreadPool(200)) {
for (int i = 0; i < 10_000; i++)
ex.submit(() -> { Thread.sleep(1000); return null; });
}
// 10,000 tasks / 200 threads = 50 rounds
// 50 rounds x 1 s = about 50 s
The same 10,000 sleeping tasks
ExecutorTasks sleeping at onceApproximate time
newVirtualThreadPerTaskExecutor()10,000about 1 s
newFixedThreadPool(200)200about 50 s
newFixedThreadPool(1000)1,000about 10 s
newFixedThreadPool(10000)10,000about 1 s, with 10,000 OS threads

The arithmetic is direct: 10,000 divided by 200 is 50 rounds, and each round takes one second, so about 50 seconds. close() still waits for all of them, so the block ends after the last round. Notice that the CPU is idle almost the whole time in both versions; the difference is only in how many tasks are allowed to wait at once.

4Why Not Just Use a Pool of 10,000 Platform Threads?

A fixed pool of 10,000 platform threads would also take about one second. The difference is cost. Ten thousand OS threads each reserve a stack, often 1 MB of address space by default, and the OS must schedule them all. That might work on a big machine, but at 100,000 or a million tasks it fails, usually with OutOfMemoryError: unable to create native thread. Virtual threads scale to a million sleeping tasks because a sleeping virtual thread is just a small object on the heap waiting for a timer.

5What the Result Does and Does Not Tell Us

The result shows that virtual threads improve the throughput of tasks that spend their time waiting. Each task still takes one full second; none of them became faster. In a real server, the waiting is network calls and database queries instead of sleep, and the benefit is the same: many more requests can be in progress at once without adding OS threads.

It also hints at a risk. If the sleep were a call to a database, the virtual-thread version would send 10,000 concurrent queries, while the pool of 200 would have sent at most 200. The pool was limiting concurrency as a side effect. With virtual threads, that limit must be added explicitly where it matters, for example with a Semaphore or a properly sized connection pool. And if the tasks were CPU-bound instead of sleeping, virtual threads would give no speed-up at all, because computation is limited by cores, not threads.

6Follow-up questions

What would happen without the try-with-resources block?

The loop would submit all tasks in a few milliseconds and the code would continue immediately, while tasks keep running in the background. Since virtual threads are daemon threads, if main then ended, the JVM could exit before they finish. close(), or shutdown() followed by awaitTermination(), is what waits.

Does the answer change with a Runnable instead of a Callable?

A Runnable cannot throw InterruptedException, so the lambda would need a try-catch around Thread.sleep. The timing would be the same. Returning null is a common trick to make the lambda a Callable.

How many carrier threads are used?

By default about as many as there are available processors, because the scheduler's parallelism defaults to the CPU count. Because each task runs for microseconds between sleeps, those few carriers are mostly idle.

7Conclusion

In this answer, we saw that with virtual threads all 10,000 tasks sleep concurrently, because Thread.sleep unmounts each virtual thread and frees its carrier, so the block takes about one second plus overhead, and try-with-resources close() waits for every task. With a fixed pool of 200, only 200 tasks sleep at once, so the work takes 50 rounds, about 50 seconds. Virtual threads raise the throughput of waiting tasks, not the speed of each task, and they remove the implicit concurrency limit a pool provided.

What to say in the interviewAbout 80 seconds aloud

Open with

With the virtual-thread executor it takes about one second, plus some tens of milliseconds of overhead. Each submit creates a new virtual thread, which runs for a moment and then calls Thread.sleep, and in a virtual thread sleep unmounts it, so the carrier, which is one of a handful of platform threads, about one per core, immediately runs the next task.

Then, if they want more

So all 10,000 are asleep at the same time, and after a second they all wake up and finish. The try-with-resources matters: since Java 19 ExecutorService is AutoCloseable and close waits for all submitted tasks, so we're measuring the whole run, not just submission.

With newFixedThreadPool of 200, a sleeping platform thread holds its OS thread for the full second, so only 200 tasks sleep at a time and the rest queue. That's 10,000 divided by 200, 50 rounds of a second each, so about 50 seconds.

The lesson is that virtual threads improve throughput for tasks that wait, not the speed of each task; each still takes a full second. And I'd add the flip side: if that sleep were a database call, the pool was limiting us to 200 concurrent queries, and with virtual threads we'd need an explicit limit like a semaphore.

About this answer

Last updated
12 November 2026
Versions it names
Java 19
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
JEP 444, Virtual Threads
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How long does this take to run, roughly, and how would the answer change with": https://scrivio-interview-library.netlify.app/java/virtual-threads/df92e93565/