Build a concurrent Java service with virtual threads

8 steps · about 4 h 10 min · Intermediate

  1. Understand virtual-thread concurrency (guide, dev.java, 30 min)

    Separate lightweight task concurrency from the limited platform threads that execute it.

  2. Create and inspect virtual threads (reference, Oracle Java documentation, 30 min)

    Use the standard thread builders and learn which thread operations still matter.

  3. Choose an appropriate workload (guide, Oracle Java documentation, 30 min)

    Apply virtual threads to numerous mostly blocking tasks rather than CPU-bound parallel work.

  4. Run one task per virtual thread (reference, Oracle Java documentation, 30 min)

    Create a closeable executor and give each request an independent task lifetime.

  5. 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.

  6. Coordinate related subtasks (guide, Oracle Java documentation, 35 min)

    Treat forked work as one operation whose failures and cancellation have a clear scope.

  7. 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.

  8. 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.