Quick revision
The short answer
The image-resizing service is CPU-bound: at 100% CPU, throughput is set by the number of cores, and no kind of thread can add cores, so it should use a platform thread pool sized to the cores and reduce the work per image if it needs more. The second team put virtual threads in a fixed pool of 50, which caps concurrency at 50 tasks exactly as their old platform pool did, so nothing changed. Virtual threads should be created per task, with limits expressed as semaphores around specific resources, and both teams should have measured where the time went before changing the threading model.
What to remember
In this answer, we saw that the image service is CPU-bound, so its throughput is set by cores and no thread type can raise it; a platform pool sized to the cores and less work per image are the real levers. The second team pooled virtual threads in a fixed pool of 50, which limited concurrency to 50 exactly as before. Virtual threads belong one per task, with limits as semaphores around specific resources, and in both cases measuring where time goes would have predicted the outcome.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The image-resizing service is CPU-bound: at 100% CPU, throughput is set by the number of cores, and no kind of thread can add cores, so it should use a platform thread pool sized to the cores and reduce the work per image if it needs more. The second team put virtual threads in a fixed pool of 50, which caps concurrency at 50 tasks exactly as their old platform pool did, so nothing changed. Virtual threads should be created per task, with limits expressed as semaphores around specific resources, and both teams should have measured where the time went before changing the threading model.
Why this question is asked
Two teams tried the same feature and got the same disappointing result for opposite reasons. One applied virtual threads to a problem they cannot solve; the other applied them in a way that cancels their benefit. Interviewers like this question because it checks both halves of understanding virtual threads: when they help, and how to use them. In this answer, we will recap what virtual threads improve, explain the first team's result with a little arithmetic, fix their service, then reproduce the second team's mistake, show the correct pattern, and finish with how both teams should have measured first.
1A Quick Recap: What Virtual Threads Improve
A virtual thread is a lightweight thread scheduled by the JVM on a small set of platform carrier threads, by default one per core. When it blocks on I/O, it is unmounted and the carrier runs another virtual thread. So the benefit is that a service can have a very large number of tasks waiting at once without a large number of operating system threads. Two conditions must hold for that to raise throughput: the tasks must spend much of their time waiting, and the number of tasks allowed to run at once must be allowed to grow.
2Team One: Why a CPU-Bound Service Did Not Change
Resizing an image means decoding pixels, scaling them and encoding the result. That is pure computation; the thread never waits. With 100% CPU, every core is already busy. Suppose the service runs on 8 cores and each resize takes 200 ms of CPU. The maximum throughput is 8 / 0.2 = 40 images per second, whether we use 8 platform threads, 200 platform threads or 10,000 virtual threads. More threads only take turns on the same 8 cores, and each image takes longer because it is sharing them.
Virtual threads cannot help here because there is no blocking to turn into cheap waiting. Notice too that a long computation on a virtual thread holds its carrier until it finishes, so CPU-heavy virtual threads can delay any I/O-bound virtual threads in the same JVM.
So the fix is to stay with a platform pool sized to the cores, and if 40 images per second is not enough, reduce the work per image: decode at a reduced size when the format supports it, use a faster scaling algorithm, skip sizes nobody requests, cache results, or add machines. If the service also downloads source images over the network, that I/O part can run on virtual threads and hand the computation to the small CPU pool.
3Team Two: Virtual Threads in a Fixed Pool of 50
The second team's code probably looked like this. They kept their existing structure and only changed the thread factory.
A fixed pool of size 50 means at most 50 tasks are in progress at any time. That was the limit before, with platform threads, and it is still the limit now. The gain virtual threads offer, letting thousands of waiting tasks be in progress at once, is exactly what the pool takes away. In fact the pool adds a little cost: virtual threads are reused, so any ThreadLocal values leak between tasks just as with platform pools, and the queue in front of the pool adds latency.
4The Correct Pattern: One Virtual Thread per Task, Limits by Resource
Virtual threads are cheap to create, so they should never be pooled. We start one per task. If something really needs a limit, for example a partner API that allows 50 concurrent calls, we express that limit directly around the call with a Semaphore, so it applies only to that resource and states why it exists.
| Approach | Tasks in progress | Limit applies to |
|---|---|---|
| Fixed pool of 50 virtual threads | At most 50 | Every task, for no stated reason |
| Virtual thread per task | As many as arrive | Nothing |
Per task + Semaphore(50) on partner | As many as arrive | Only the 50 partner calls in flight |
Notice the difference from the pool. Tasks that do not touch the partner are not held back, and if the same task also calls a database with its own limit, it can have a second semaphore sized for that resource. With the pool, every task was limited by one number that had nothing to do with any specific resource.
5Measure Before Changing the Threading Model
Both teams could have predicted their result. For the first, a CPU profile or simply the 100% CPU graph shows that the threads are computing, not waiting. For the second, the question is what limits throughput today: if thread pool utilisation is at 100% while CPU and downstream resources are idle, more concurrency helps, but only if we actually allow more. Java Flight Recorder with a profile setting shows where threads spend their time, split into running, waiting on sockets and waiting on locks. Running a load test with the old and new setting, and comparing throughput and latency percentiles, turns a hope into a measurement.
6Follow-up questions
Would ForkJoinPool or parallel streams help the image service?
They can make a single large image use several cores, for example by splitting rows across tasks, which lowers the latency of one image. They cannot raise total throughput beyond what the cores allow when the CPU is already at 100%, because they use the same cores.
Is it ever right to put virtual threads in an executor with a limit?
An executor that creates one virtual thread per task, like newVirtualThreadPerTaskExecutor, is fine because it does not pool. What we avoid is reusing a fixed number of them. If we want a cap on concurrent tasks overall, a semaphore in front of the work expresses that without pooling.
Why do virtual threads not preempt long computations?
The JVM's scheduler only switches a virtual thread off its carrier at blocking points, such as I/O, locks from java.util.concurrent or sleep. A loop that never blocks keeps its carrier. That is acceptable for short work but means CPU-heavy tasks belong on their own platform pool.
7Conclusion
In this answer, we saw that the image service is CPU-bound, so its throughput is set by cores and no thread type can raise it; a platform pool sized to the cores and less work per image are the real levers. The second team pooled virtual threads in a fixed pool of 50, which limited concurrency to 50 exactly as before. Virtual threads belong one per task, with limits as semaphores around specific resources, and in both cases measuring where time goes would have predicted the outcome.
Open with
These are two different mistakes. The image-resizing service is CPU-bound. At 100% CPU every core is busy computing, so throughput is cores divided by CPU time per image, say 8 cores at 200 ms is 40 images a second, whatever threads we use.
Then, if they want more
Virtual threads help when threads spend their time blocked on I/O, because blocking becomes cheap. Resizing never blocks, so there is nothing to gain, and long computations even hold their carriers.
I would keep a platform pool sized to the number of cores and look at reducing the work per image, like decoding at lower resolution, caching results or scaling out. The second team put virtual threads in a fixed pool of 50.
That means at most 50 tasks run at once, which is exactly the concurrency they had with 50 platform threads, so of course nothing changed. Virtual threads are meant to be created per task, for example with newVirtualThreadPerTaskExecutor, never pooled.
If there is a real limit, like a partner API that allows 50 concurrent calls, I put a semaphore of 50 around that specific call, so other work is not held back. For both teams, the lesson is to measure first: a CPU graph or a JFR profile tells you whether threads are computing or waiting.
About this answer
- Last updated
- 12 November 2026
- 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 "A team enabled virtual threads in an image-resizing service and saw no change in...": https://scrivio-interview-library.netlify.app/java/virtual-threads/533554a642/