Medium java spring

How do you build a highly concurrent app without running out of platform threads?

The 30 second answer

For blocking network and disk work I use virtual threads, which are final in Java 21. They are cheap, so one per request or task is fine, and when they block on that work they release the carrier thread. I don't pool them. Instead, I limit scarce resources like database connections with the connection pool or a semaphore, not with the thread count.

For blocking I/O work, use virtual threads (JEP 444, final in Java 21): Executors.newVirtualThreadPerTaskExecutor(). They are cheap, so one per request is fine, and when they block on I/O they unmount from the carrier thread. Don't pool them. Limit access to scarce resources such as DB connections with a semaphore or the pool itself, not by the thread count. Follow-up: What is pinning? (In Java 21, a virtual thread inside a synchronized block or native call can't unmount while blocked, per JEP 444; prefer ReentrantLock around long blocking calls.)

Sources (1)
  1. JEP 444: Virtual Threads