VTask: Deferred Tasks and Virtual Threads
Describe work that may block, then run it when you choose, alone or alongside other work.
"Concurrency is about dealing with lots of things at once. Parallelism is about doing lots of things at once."
— Rob Pike, Concurrency is not Parallelism
A VTask is built for the dealing. Par and Scope can keep thousands of tasks waiting on replies at once, each on a virtual thread, while only as many run at any moment as the machine has cores.
| Models | Work that may block or fail, described now and run when you choose, alone or at the same time as other work |
| Replaces | A Callable submitted to an ExecutorService, and the Future you wait on |
| Effect Path | VTaskPath, and VResultPath for a typed error |
| Instances | Functor, Applicative, Monad, MonadError<Throwable> |
| Choose it when | The work throws a checked exception, or should run at the same time as other work; otherwise IO, or compare every type in Choosing a Type |
- Describe work that blocks or throws a checked exception, and run it when you choose
- Run two lookups at the same time on a
VTaskPath, and recover from one that fails - Keep a refusal the caller expects as a typed
Left, not an exception, on aVResultPath - Say which thread runs a task, and what
run(),runSafe()andrunAsync()do with a failure - Use
VTaskin generic code through itsMonadErrorinstance
The code on this page is VTaskBook.java: the page includes it, so the build compiles and runs it. VTaskBookTest.java holds what the page says each method does.
Describing work that may block
In plain Java, you run blocking work on another thread by submitting a Callable to an ExecutorService. With Executors.newVirtualThreadPerTaskExecutor(), each task gets a virtual thread of its own, which costs little while it waits. The work starts as soon as you submit it, and Future.get() makes you catch InterruptedException and ExecutionException. A VTask<A> holds a Callable that nothing has run yet. You compose tasks first, and choose afterwards how to run them. run() runs a task on the calling thread, runAsync() on a virtual thread of its own, and Par at the same time as other tasks. A Callable may throw a checked exception, which the Supplier behind an IO cannot.
The code on this page uses these imports, with Order from the order records the Optics chapter introduces:
import java.io.IOException;
import java.math.BigDecimal;
import org.higherkindedj.example.book.optics.cast.Order;
import org.higherkindedj.hkt.effect.Path;
import org.higherkindedj.hkt.effect.VTaskPath;
Pricing an order in euros needs two lookups, the exchange rate and a courier's quote, and neither service is called until you run the task. Each call blocks until its service answers, and may throw an IOException:
// Two remote services: each call blocks until it answers, and may throw an IOException
interface Rates {
BigDecimal toEuros() throws IOException;
}
interface Courier {
BigDecimal quote(Order order) throws IOException;
}
On a VTaskPath
Application code holds tasks on a VTaskPath. Path.vtask puts a Callable on one, via chains a step that needs the previous result, and map changes the value, as on every Path. handleError gives a value in place of a failure, here CACHED_RATE, a stored rate of 1.15 for when the rate service cannot answer. parZipWith runs two tasks at the same time and joins their results:
static VTaskPath<BigDecimal> priceInEuros(Order order, Rates rates, Courier courier) {
VTaskPath<BigDecimal> rate = Path.vtask(rates::toEuros).handleError(error -> CACHED_RATE);
VTaskPath<BigDecimal> shipping = Path.vtask(() -> courier.quote(order));
// The two lookups run at the same time, each on a virtual thread
return rate.parZipWith(shipping, (r, s) -> inEuros(order, r, s));
}
parZipWith runs its tasks through Par, which with Scope and ScopeJoiner is built on Java 25's structured concurrency API, JEP 505, a preview feature. Their class files are marked as using it. Code that names Par, Scope or ScopeJoiner needs --enable-preview to compile and to run. Code that reaches them only through a Path, such as a VTaskPath's zipWith, zipWith3, parZipWith or race, compiles without it and needs it when it runs. The HKJ Gradle plugin adds it by default. The JDK's API may change in a later release; your code calls the library's types, not the JDK's.
LIVE_RATES and RATES_DOWN stand in for the rate service, answering 1.17 or failing. COURIER stands in for the courier, which quotes 7.99 and counts its quotes in QUOTES. ORDER is worth 92.50 in pounds, and inEuros adds the shipping and converts the sum, so the live rate gives (92.50 + 7.99) × 1.17 = 117.57:
VTaskPath<BigDecimal> price = priceInEuros(ORDER, LIVE_RATES, COURIER);
int quotesBeforeRunning = QUOTES.get();
// 0
BigDecimal euros = price.unsafeRun();
// 117.57
BigDecimal withCachedRate = priceInEuros(ORDER, RATES_DOWN, COURIER).unsafeRun();
// 115.56
Building the path asks neither service. unsafeRun() waits on the calling thread while both lookups run. It is where the work happens, so call it once, at the edge of the application. Each run asks the services again. On a VTaskPath, zipWith runs both paths at once too, as parZipWith does.
A VTaskPath also cancels a slow task with timeout(duration), and retries with withRetry, as VTaskPath shows. When the time runs out, the task is interrupted, and the TimeoutException arrives once it has stopped, so the task is not left running. A task that ignores interruption, or blocks in CompletableFuture.join(), which an interrupt cannot reach, holds the failure back until it finishes. To fail on time instead, see Timeouts.
A refusal as a value, on a VResultPath
An exception suits a failure nobody plans for, such as a service that is down. A refusal the caller expects belongs in the result. A VResultPath<E, A> holds a VTask<Either<E, A>>, and works as an EitherPath whose steps may block: via stops at the first Left, and recover and mapError work on the error. The courier refuses to collect an order that has already shipped. These are the extra imports, with OrderError, the order service's refusals, from the Either page:
import org.higherkindedj.example.book.foundations.OrderError;
import org.higherkindedj.example.book.optics.cast.OrderStatus;
import org.higherkindedj.hkt.effect.VResultPath;
import org.higherkindedj.hkt.either.Either;
// The courier refuses a collection for an order that has already shipped
static VResultPath<OrderError, String> bookCollection(Order order) {
return Path.vresultDefer(
() ->
order.status() == OrderStatus.SHIPPED
? Either.left(OrderError.alreadyShipped(order.id()))
: Either.right("collection booked for " + order.customer().name()));
}
static VResultPath<OrderError, String> confirmCollection(Order order) {
return bookCollection(order)
.via(booking -> Path.<OrderError, String>vresultRight(booking + ", confirmed"));
}
SHIPPED is ORDER once it has shipped. run() gives the VTask, and running that gives the Either:
Either<OrderError, String> booked = confirmCollection(ORDER).run().run();
// Right(collection booked for Ada, confirmed)
Either<OrderError, String> refused = confirmCollection(SHIPPED).run().run();
// Left(AlreadyShipped[orderId=00000000-0000-0000-0000-000000000001])
withTimeout(duration, onTimeout) gives a typed Left when the time runs out. Like a VTaskPath's timeout, it interrupts the task and waits for it to stop first. An interrupted booking may already have reached the courier, so the Left says the outcome is unknown, not that nothing was booked. A VResultPath's zipWith runs its paths one after another and stops at the first Left. To run typed work at the same time, see VResultPath.
Running a task
VTaskPath.run() hands you the VTask inside, without running it. That VTask runs in one of three ways. Its run() runs it on the calling thread and waits. runSafe() does the same, and holds a failure in a Try; prefer it in code that handles failures as values. runAsync() starts it on a new virtual thread and returns a CompletableFuture, whose cancel(true) interrupts the task. Running a task does not move it to a virtual thread: main runs on a platform thread, an ordinary thread the operating system schedules, and run() keeps the task there. Only runAsync(), timeout, Par and Scope move work to a virtual thread. These are the extra imports:
import org.higherkindedj.hkt.trymonad.Try;
import org.higherkindedj.hkt.vtask.VTask;
VTask<Boolean> onAVirtualThread = VTask.of(() -> Thread.currentThread().isVirtual());
boolean fromRun = onAVirtualThread.run();
// false
boolean fromRunAsync = onAVirtualThread.runAsync().join();
// true
On a failure, a VTask's runSafe() holds the exception the task threw, a checked one included:
VTask<BigDecimal> rateWhileDown = VTask.of(RATES_DOWN::toEuros);
Try<BigDecimal> held = rateWhileDown.runSafe();
// Failure(java.io.IOException: the rate service is down)
run() throws an unchecked exception as it is. It declares no checked exception, so it wraps a checked one in a VTaskExecutionException, whose getCause() gives the original. The future from runAsync() fails with the task's exception: join() throws it wrapped in a CompletionException, and get() in an ExecutionException.
Running tasks at the same time
Par runs tasks at the same time, each on a virtual thread of its own, and waits for them all. map2 and zip join two tasks, and all a list of them. traverse runs the task a function gives for each item of a list. race keeps the first task to succeed and cancels the rest. Par.all is close to ExecutorService.invokeAll, and race to invokeAny. What Par adds is that when one task fails, map2, zip, all and traverse cancel the others and fail with its exception. parZipWith on a VTaskPath runs its two tasks through Par.map2. These are the extra imports:
import java.util.Collections;
import org.higherkindedj.hkt.vtask.Par;
COURIERS holds three couriers, which quote 7.99, 6.50 and 9.20. The three quotes take as long as the slowest of them, not the three added together:
VTask<BigDecimal> cheapest =
Par.traverse(COURIERS, courier -> VTask.of(() -> courier.quote(ORDER)))
.map(Collections::min);
BigDecimal best = cheapest.run();
// 6.50
Creating instances
VTask.of(callable) describes work that may throw any exception, and VTask.delay(supplier) work that throws no checked exception. VTask.exec(runnable) describes work that gives Unit. VTask.succeed(value) and VTask.fail(exception) give a task that has already succeeded or failed. VTask.blocking(callable) is of under a name that says the work blocks, and runs the same.
On a Path, Path.vtask matches of, Path.vtaskExec matches exec, Path.vtaskPure matches succeed and Path.vtaskFail matches fail, and Path.vtaskPath(task) takes a VTask you already have. For a VResultPath, they are Path.vresultDefer, Path.vresultRight, Path.vresultLeft and Path.vresult(task).
A task always gives a value
A VTask that succeeds gives a value, never null. VTask.succeed(null) throws NullPointerException at the call. A callable, supplier, map function or recovery function that returns null fails the task when it runs, and the exception names that function, as in callable must not return null. A task with nothing to return gives Unit.INSTANCE, as VTask.exec does, and one whose value may be missing says so with a Maybe. These are the extra imports:
import org.higherkindedj.hkt.Unit;
import org.higherkindedj.hkt.maybe.Maybe;
// Nothing to return: give Unit.INSTANCE, as VTask.exec does
static final VTask<Unit> LOGGED = VTask.exec(() -> System.out.println("priced " + ORDER.id()));
// A value that may be missing: say so with a Maybe
static final VTask<Maybe<String>> NICKNAME =
VTask.of(() -> Maybe.fromNullable(System.getenv("NICKNAME")));
So VTask.of(() -> { work(); return null; }), typed VTask<Void>, fails when it runs. Use VTask.exec, or return Unit.INSTANCE. NullAway flags that lambda only with the options in What a nullness checker sees. VTask is itself a functional interface, so you can write a task as a lambda, which NullAway checks with no options set; run(), runSafe() and runAsync() refuse a null from one of those too.
The factories and functions of every lazy type follow the same rule: IO, VStream, State, Reader, Writer, Lazy, Trampoline, Free and Context, and every Effect Path over one. A stream refuses a null element. A nullness checker flags a hand-written IO or Reader lambda that returns null, and an IOPath refuses one when it runs. Only Id holds a null, since every optic's modify runs through it. A CompletableFuture outside a Path keeps the JDK's null too.
Methods
| Method | What it does |
|---|---|
VTask.of(callable), delay(supplier), exec(runnable) | Describe work; nothing runs until the task does |
VTask.succeed(value), VTask.fail(exception) | Give a task that has already succeeded or failed |
map(f), flatMap(f), via(f), then(next) | Build a new task, running nothing |
recover(f), recoverWith(f), mapError(f) | Give a value or another task in place of a failure, or change the exception |
timeout(duration) | Runs the task on a virtual thread of its own; if it has not finished in time, interrupts it and fails with a TimeoutException once it has stopped |
run(), runSafe() | Run the task on the calling thread, throwing its failure or holding it in a Try |
runAsync() | Starts the task on a new virtual thread, and returns a CompletableFuture |
Par.map2, zip, all, traverse, race | Run tasks at the same time, each on a virtual thread |
Path.vtask(callable), Path.vresultDefer(supplier) | Put work on a VTaskPath, or on a VResultPath |
VTASK.widen(task), VTASK.narrow(kind), VTASK.run(kind) | Move a task in and out of Kind, or run one |
Working with Kind
Generic code, written once for any container, takes VTask through its MonadError instance, a VTaskMonad, from Instances.monadError(vtask()). Its error type is Throwable, as MonadError shows. The instance works over Kind<VTaskKind.Witness, A>, so you widen a task to pass it in, and run the result with VTASK.run. These are the extra imports:
import static org.higherkindedj.hkt.instances.Witnesses.vtask;
import static org.higherkindedj.hkt.vtask.VTaskKindHelper.VTASK;
import org.higherkindedj.hkt.Kind;
import org.higherkindedj.hkt.MonadError;
import org.higherkindedj.hkt.instances.Instances;
import org.higherkindedj.hkt.vtask.VTaskKind;
MonadError<VTaskKind.Witness, Throwable> tasks = Instances.monadError(vtask());
Kind<VTaskKind.Witness, BigDecimal> rate =
tasks.handleErrorWith(
VTASK.widen(VTask.of(RATES_DOWN::toEuros)), error -> tasks.of(CACHED_RATE));
BigDecimal fallback = VTASK.run(rate);
// 1.15
The handler receives the exception the task threw. This one ignores it and gives a constant, which the instance's recover says directly, given the widened task and CACHED_RATE. Those instance-level forms take the Kind first. The fluent recover on a VTask takes a function of the exception.
The instance's ap, and map2 to map5, run their tasks one after another, so a failure in the first stops the second from running; Par runs tasks at the same time. Widening allocates nothing: VTask implements VTaskKind itself, so widen checks for null and returns the same object.
When to use VTask
| You need | Use |
|---|---|
| Work that may block or throw a checked exception, run when you choose | VTask, on a VTaskPath in application code |
| Two or more tasks at the same time, each on a virtual thread | parZipWith, or Par |
A refusal the caller expects, as a typed Left rather than an exception | VResultPath |
| Tasks that start and end together, joined the way you choose | Structured Concurrency |
| A resource released however the work ends | Resource Management |
| A side effect with no checked exception, never run alongside other work | IO |
| Work already started, from an API that gives you a future | CompletableFuture |
| Many values, produced one at a time | VStream |
Practise VTask in TutorialVTask (9 exercises), and VTaskPath in TutorialVTaskPath (9 exercises), both part of the VTask journey.
Benchmarks: VTaskBenchmark and VTaskPathBenchmark measure building and running tasks, and VTaskParBenchmark the Par combinators. VTaskVsIOBenchmark and VTaskVsPlatformThreadsBenchmark compare VTask with IO and with a pool of platform threads. Benchmarks & Performance says how to run them.
Previous: CompletableFuture Next: Structured Concurrency