Core Java · Chapter 40 of 48

Virtual threads

By the end of this chapter you can run thousands of waiting tasks at once on virtual threads, name them for the logs, and say when a small pool of platform threads is the better choice. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Beyond the basics
  • 5 tasks
  • Java
  • In the all labs plan

The first 2 chapters of Core Java are free. This one comes with the all labs plan.

Why you need this

Diwali week. Every counter in the country takes UPI, and every payment has to be confirmed with the bank before the chai goes out. The bank answers in about a second. Meera wants the queue at NDLS gone: a thousand payments checked at once, not four a second.

The pools from the last chapter use platform threads, each a wrapper around an operating system thread with its own large stack. A pool of four confirms four payments a second; a pool of a thousand means a thousand OS threads, nearly all of them just waiting for the bank.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    UpiChecks.java: start a named virtual thread for one job

    In UpiChecks.java, make startVirtual(name, job) start the job on a new virtual thread with that name, and return that thread. The thread must be started, so the job runs on it.

    For example, startVirtual("upi-42", job) returns a virtual thread named upi-42, and job runs on that very thread.

  2. Task 2

    UpiChecks.java: check a thousand UPI payments at once

    In UpiChecks.java, make checkPayments(ids, gateway) check every payment id with the gateway, each check on its own virtual thread, all of them running and waiting at the same time, and return the gateway's answers in the order of the ids. No ids give an empty list.

    For example, 1,000 ids TXN1 to TXN1000, where each check waits until all 1,000 checks have started and then answers id plus " ok", return [TXN1 ok, TXN2 ok, ..., TXN1000 ok].

  3. Task 3

    UpiChecks.java: number virtual threads for the logs

    In UpiChecks.java, make namedVirtual(prefix) return a ThreadFactory that makes virtual threads, without starting them, named with the prefix, a dash and a number counting from 0. Every factory counts from 0 on its own.

    For example, namedVirtual("upi") makes threads named upi-0, upi-1 and upi-2, each still in the state NEW; a new factory from namedVirtual("cash") then makes cash-0.

  4. Task 4

    UpiChecks.java: a small pool of platform threads for CPU work

    In UpiChecks.java, make cpuPool() return a fixed thread pool of platform threads with exactly as many threads as the JVM has processors available, for work that keeps the CPU busy. Its size never changes. The caller shuts it down.

    For example, on a machine with 4 available processors it is a pool of exactly 4 platform threads, and a job adding 0 to 99 on it returns 4950.

  5. Task 5 · interview question

    Interview: UpiChecks.java, predict how a virtual thread differs

    In UpiChecks.java, make predictOutput() return the lines the code in its comment prints, one string per line, in order, written exactly as Java prints them (true or false in lower case).

    The check runs the same code and compares your list with what Java really printed.

What you will learn

  • Virtual threads: threads the JVM runs on a few platform threads, and parks for almost nothing while they wait.
  • Making them: Thread.ofVirtual(), a numbered ThreadFactory, and an executor that starts one per task.
  • When not to use them: work that keeps the CPU busy wants a small fixed pool of platform threads.

More in Beyond the basics

Build "Virtual threads" yourself

Tapri runs chai counters at railway stations, and every counter runs a Java program you write. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Core Java are free. This one comes with the all labs plan.