Build a concurrent Java service with virtual threads
8 steps · about 4 h 10 min · Intermediate
- Understand virtual-thread concurrency (guide, dev.java, 30 min)
Separate lightweight task concurrency from the limited platform threads that execute it.
- Create and inspect virtual threads (reference, Oracle Java documentation, 30 min)
Use the standard thread builders and learn which thread operations still matter.
- Choose an appropriate workload (guide, Oracle Java documentation, 30 min)
Apply virtual threads to numerous mostly blocking tasks rather than CPU-bound parallel work.
- Run one task per virtual thread (reference, Oracle Java documentation, 30 min)
Create a closeable executor and give each request an independent task lifetime.
- Bound the scarce resource (reference, Oracle Java documentation, 25 min)
Limit access to a database or remote dependency without shrinking the number of concurrent tasks.
- Coordinate related subtasks (guide, Oracle Java documentation, 35 min)
Treat forked work as one operation whose failures and cancellation have a clear scope.
- Compare asynchronous composition (reference, Oracle Java documentation, 30 min)
Recognise when an existing completion-stage API is useful and when direct blocking code is clearer.
- Observe and test the service (guide, Oracle Java documentation, 40 min)
Exercise cancellation and failure paths, then inspect thread activity instead of judging concurrency only by elapsed time.