liveBook · Manning

@RunOnVirtualThread

As explained in Quarkus in Action

@RunOnVirtualThread is a Quarkus annotation that directs an annotated method or class's methods to execute on virtual threads.

@RunOnVirtualThread directs an annotated method to execute on a virtual thread. Placing it on a class applies the behavior to all of that class’s methods. section 8.5.1

Use the annotation with these execution expectations:

  • Create a new virtual thread for each task instead of maintaining a reusable virtual-thread pool. section 8.5.1
  • Expect virtual threads to be cheap to create, start, and reclaim after completing their work. section 8.5.1
  • Request virtual-thread execution explicitly because Quarkus does not provide a global switch for every execution. section 8.5.2
@GET
@Path("/virtualThread")
@RunOnVirtualThread
public void virtualThread() {
  Log.info("Running on " + Thread.currentThread().getName());
}

Annotate an HTTP endpoint with @RunOnVirtualThread and inspect the current thread from its handler. section 8.5.1

Observing execution

Repeated calls to an annotated endpoint can report names such as quarkus-virtual-thread-0, quarkus-virtual-thread-1, and quarkus-virtual-thread-2, showing that each call uses a newly created virtual thread. section 8.5.1

Virtual threads are not a universal replacement for reactive execution. Transactions, ThreadLocal-dependent libraries, and pinning can make adoption difficult; when library compatibility is insufficient, Quarkus can switch to reactive programming. section 8.5.2

How Quarkus in Action covers it

How deep it goes
Explains method and class targeting, thread lifecycle, and explicit opt-in, while deferring detailed transaction and library compatibility solutions.
Examples
Demonstrates an annotated HTTP endpoint and repeated log outputs with increasing Quarkus virtual-thread identifiers.
What it assumes
Assumes familiarity with Quarkus methods and classes, while introducing virtual-thread creation, reuse, and explicit opt-in.
What you can do after
Apply it via Quarkus to opt individual methods or classes into virtual-thread execution and inspect the resulting thread.
How it connects
Connects virtual threads with Project Loom, reactive programming, transactions, ThreadLocal, scoped values, and pinning.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Quarkus in Action. The section links point to the parts of the book it draws on.