VTaskPath
VTaskPath<A> wraps VTask<A> for virtual thread-based concurrency.
It brings the lightweight threading of Project Loom to the Effect Path API,
letting you write simple blocking code that scales to millions of concurrent
operations.
"The perfect is the enemy of the good."
— Voltaire, Dictionnaire philosophique
Java's reactive libraries pursued a kind of perfection: maximum throughput, minimal thread usage, non-blocking from end to end. The result was powerful but demanding; developers needed to master publishers, subscribers, backpressure, and schedulers before writing their first concurrent operation. The perfect abstraction for performance became the enemy of good code that anyone could read and maintain.
Virtual threads offer a different bargain. Rather than perfecting the abstraction, they changed the underlying economics. When threads cost almost nothing, simple blocking code performs well enough for most applications. VTaskPath embraces this pragmatism: it provides a fluent, composable API for concurrent operations without requiring you to abandon familiar patterns. Good code that works is better than perfect code that mystifies.
- Creating VTaskPath instances via the Path factory
- Composing concurrent operations with familiar
mapandviapatterns - Error handling and recovery strategies
- Timeout management for long-running operations
- Parallel execution with the
Parutilities - Advanced structured concurrency with
ScopeandResource - When to choose VTaskPath over IOPath
Creation
// From a computation (the primary pattern)
VTaskPath<String> fetchData = Path.vtask(() -> httpClient.get(url));
// Pure value (no computation)
VTaskPath<Integer> pure = Path.vtaskPure(42);
// Immediate failure
VTaskPath<String> failed = Path.vtaskFail(new IOException("Network error"));
// From a Runnable (returns Unit)
VTaskPath<Unit> logAction = Path.vtaskExec(() -> logger.info("Starting..."));
// From an existing VTask
VTaskPath<Config> config = Path.vtaskPath(existingVTask);
A VTaskPath always gives a value. Path.vtaskPure(null) throws at the call, and a callable or map function that returns null fails the path when it runs, naming that function. A step with nothing to return is Path.vtaskExec, which gives Unit. A Task Always Gives a Value sets out the rule every lazy type follows.
Like an IOPath, a VTaskPath defers execution: nothing happens until
you run the task, and unsafeRun() and runSafe() run it on the calling
thread. What differs is that its work may throw a checked exception, and
that runAsync(), zipWith, Par and Scope run tasks on virtual
threads, at the same time.
Execution Model
┌─────────────────────────────────────────────────────────────────┐
│ VTaskPath<A> │
│ │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌─────────────┐ │
│ │ describe │ → │ compose │ → │ transform│ → │ execute │ │
│ │ effect │ │ effects │ │ results │ │ when you │ │
│ │ │ │ │ │ │ │ run it │ │
│ └──────────┘ └──────────┘ └──────────┘ └─────────────┘ │
│ │
│ Nothing runs until unsafeRun(), runSafe() or runAsync(), or │
│ until toTryPath(), which runs the task at once │
└─────────────────────────────────────────────────────────────────┘
Virtual threads are managed by the JVM, not the operating system. Each consumes only kilobytes of memory (versus megabytes for platform threads), so you can spawn millions without exhausting resources.
Core Operations
VTaskPath<String> greeting = Path.vtaskPure("Hello");
// Transform with map
VTaskPath<Integer> length = greeting.map(String::length);
// Chain with via
VTaskPath<String> result = greeting
.map(String::toUpperCase)
.via(s -> Path.vtask(() -> enrichWithTimestamp(s)))
.map(s -> s + "!");
// Sequence, discarding first result
VTaskPath<Unit> setup = Path.vtaskExec(() -> initResources());
VTaskPath<Data> withSetup = setup.then(() -> Path.vtask(() -> loadData()));
// Debug with peek
VTaskPath<Integer> debugged = Path.vtaskPure(42)
.peek(v -> System.out.println("Before: " + v))
.map(v -> v * 2)
.peek(v -> System.out.println("After: " + v));
Running Tasks
VTaskPath provides three execution methods:
VTaskPath<Integer> task = Path.vtask(() -> compute());
// 1. unsafeRun() - Blocks, may throw
// Checked exceptions are wrapped in VTaskExecutionException;
// RuntimeException and AppError are thrown directly.
try {
Integer result = task.unsafeRun();
} catch (VTaskExecutionException e) {
// Checked exception wrapped: original available via e.getCause()
handleError(e.getCause());
} catch (RuntimeException e) {
handleError(e);
}
// 2. runSafe() - Returns Try<A> for functional error handling
Try<Integer> tryResult = task.runSafe();
tryResult.match(
value -> System.out.println("Success: " + value),
error -> System.err.println("Failure: " + error.getMessage())
);
// 3. runAsync() - Returns CompletableFuture<A> for async composition
CompletableFuture<Integer> future = task.runAsync();
future.thenAccept(value -> System.out.println("Async result: " + value));
Prefer runSafe() for most use cases. It captures failures in a Try,
maintaining functional error handling throughout your codebase. Use unsafeRun()
at system boundaries where you need to interact with exception-based APIs.
runSafe(), catching and handleError see the exception the task threw,
a checked one included. Only unsafeRun(), which cannot throw a checked
exception, wraps one in VTaskExecutionException.
Error Handling
VTaskPath<Config> loadConfig = Path.vtask(() -> configService.load());
// handleError: Transform failure to success value
VTaskPath<Config> withDefault = loadConfig
.handleError(error -> Config.defaults());
// handleErrorWith: Transform failure to another VTaskPath
VTaskPath<Config> withFallback = loadConfig
.handleErrorWith(error -> Path.vtask(() -> loadFallbackConfig()));
Fallback Chains
Build resilient services with cascading fallbacks:
VTaskPath<Config> resilientConfig =
Path.vtask(() -> loadFromPrimarySource())
.handleErrorWith(e -> Path.vtask(() -> loadFromSecondarySource()))
.handleErrorWith(e -> Path.vtask(() -> loadFromCache()))
.handleError(e -> Config.defaults());
Timeouts
Prevent runaway operations with timeouts:
VTaskPath<Data> slowOperation = Path.vtask(() -> {
Thread.sleep(5000);
return fetchData();
});
VTaskPath<Data> withTimeout = slowOperation.timeout(Duration.ofSeconds(2));
Try<Data> result = withTimeout.runSafe();
// result.isFailure() == true (TimeoutException)
withTimeout(duration) is the same operation under the family-wide with* name, so resilience chains read uniformly across IOPath, VTaskPath, and VResultPath:
VTaskPath<Data> guarded = Path.vtask(() -> fetchData())
.withRetry(RetryPolicy.exponentialBackoffWithJitter(3, Duration.ofMillis(200)))
.withTimeout(Duration.ofSeconds(2))
.withCircuitBreaker(serviceBreaker)
.withBulkhead(serviceBulkhead);
When the time runs out, the computation is interrupted, and the TimeoutException arrives once it has stopped, so the computation is not left running. A computation that ignores interruption holds the failure back until it finishes, as in a Scope with a timeout. So does one blocked where an interrupt cannot reach it, such as in CompletableFuture.join(), Lock.lock() or a synchronized block. Never wait on a timeout while holding a lock the computation needs: it cannot finish, and the timeout waits for it.
To fail when the time runs out instead, put detachOnCancel() before the timeout. The computation is still interrupted, but nothing waits for it to stop:
VTaskPath<Data> onTime = Path.vtask(() -> fetchData())
.detachOnCancel()
.withTimeout(Duration.ofSeconds(2));
Put it inside the timeout: withTimeout(d).detachOnCancel() changes nothing for that timeout. A circuit breaker guarding a call that can hang works the same way: protect the detached call, and the breaker counts it when its callTimeout runs out.
Detach only work whose leftovers are harmless, since it may still be running after the caller has moved on. A cancelled detached call no longer counts against a bulkhead's limit, and each retry after a timeout can leave one more behind.
A TimeoutException thrown inside the computation is reported as the budget running out too, with that exception as its cause, where the budget's own has none. A cancellation reaches it too: when the thread waiting for the result is interrupted, so is the computation, and the timeout throws InterruptedException once it stops. The path's unsafeRun() reports it wrapped in VTaskExecutionException, and runSafe() holds the InterruptedException itself; each sets the interrupt status. To land the timeout as a typed Left instead of a failure, use VResultPath.withTimeout(duration, onTimeout). See Resilience Patterns for the per-carrier availability table.
Parallel Execution with Par
The Par utility provides combinators for running VTasks concurrently:
┌───────────────────────────────────────────────────────────────┐
│ Par Combinators │
│ │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ zip │ │ map2 │ │ all │ │ race │ │
│ │ (A, B) │ │ (A,B)→R │ │ [A]→[A] │ │ first A │ │
│ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │
│ │
│ All use StructuredTaskScope for proper lifecycle management │
└───────────────────────────────────────────────────────────────┘
import org.higherkindedj.hkt.vtask.Par;
// zip: Combine two tasks into a tuple
VTask<String> userTask = VTask.of(() -> fetchUser(id));
VTask<String> profileTask = VTask.of(() -> fetchProfile(id));
VTask<Par.Tuple2<String, String>> both = Par.zip(userTask, profileTask);
// Both execute in parallel
// map2: Combine two tasks with a function
VTask<UserProfile> combined = Par.map2(
userTask,
profileTask,
(user, profile) -> new UserProfile(user, profile)
);
// all: Execute a list of tasks in parallel
List<VTask<Integer>> tasks = List.of(
VTask.of(() -> compute(1)),
VTask.of(() -> compute(2)),
VTask.of(() -> compute(3))
);
VTask<List<Integer>> allResults = Par.all(tasks);
// race: Return first successful result
VTask<String> fastest = Par.race(List.of(
VTask.of(() -> fetchFromServerA()),
VTask.of(() -> fetchFromServerB())
));
Par combinators use Java's StructuredTaskScope internally. If any task
fails, sibling tasks are cancelled automatically. This prevents resource
leaks and ensures clean shutdown semantics.
Scope and Resource
For advanced structured concurrency patterns, use Scope and Resource directly with VTask:
import org.higherkindedj.hkt.vtask.Scope;
import org.higherkindedj.hkt.vtask.Resource;
// Scope: fluent builder for structured concurrency
VTask<List<String>> results = Scope.<String>allSucceed()
.fork(VTask.of(() -> fetchUserData(id)))
.fork(VTask.of(() -> fetchUserProfile(id)))
.timeout(Duration.ofSeconds(5))
.join();
// AppError accumulation with Validated
VTask<Validated<List<AppError>, List<String>>> validation =
Scope.<AppError, String>accumulating(AppError::from)
.fork(validateField1())
.fork(validateField2())
.join();
// Resource: bracket pattern for safe resource management
Resource<Connection> conn = Resource.fromAutoCloseable(
() -> dataSource.getConnection()
);
VTask<List<User>> users = conn.use(c ->
VTask.of(() -> userDao.findAll(c))
);
For comprehensive documentation on Scope, ScopeJoiner, and Resource, see:
- Structured Concurrency - Scope and ScopeJoiner
- Resource Management - Bracket pattern
Converting Between Path Types
// VTaskPath to TryPath (executes immediately)
VTaskPath<Data> vtask = Path.vtask(() -> fetchData());
TryPath<Data> tryResult = vtask.toTryPath();
// VTaskPath to IOPath (preserves laziness)
IOPath<Data> io = vtask.toIOPath();
// From underlying VTask
VTask<Data> underlying = vtask.run();
VTaskPath<String> backToPath = Path.vtaskPath(VTask.succeed("restored"));
VTaskPath vs IOPath
| Aspect | VTaskPath | IOPath |
|---|---|---|
| Thread Model | Caller's thread for unsafeRun(); virtual threads for runAsync(), zipWith, Par and Scope | Caller's thread |
| Parallelism | Built-in via Par, Scope | Manual composition |
zipWith Behaviour | Parallel (uses Par.map2) | Sequential |
| Structured Concurrency | Yes, with Scope and Resource | No |
| Async Support | runAsync() returns CompletableFuture | No built-in async |
| Resource Usage | Kilobytes per task | N/A (single-threaded) |
Choose VTaskPath when:
- You need lightweight concurrency at scale
- You want structured concurrency with proper cancellation
- Simple blocking code is preferable to reactive complexity
Choose IOPath when:
- Single-threaded execution is sufficient
- You want explicit control over which thread runs the computation
- You're building a library that shouldn't impose thread choices
Real-World Example
// Parallel service aggregation with timeout and fallback
VTaskPath<Dashboard> loadDashboard(UserId userId) {
VTask<User> userTask = VTask.of(() -> userService.get(userId));
VTask<List<Order>> ordersTask = VTask.of(() -> orderService.recent(userId));
VTask<Analytics> analyticsTask = VTask.of(() -> analyticsService.get(userId));
return Path.vtaskPath(
Par.map3(
userTask,
ordersTask,
analyticsTask,
Dashboard::new
)
)
.timeout(Duration.ofSeconds(5))
.handleError(e -> Dashboard.empty());
}
// Usage
Try<Dashboard> dashboard = loadDashboard(userId).runSafe();
- Virtual Threads:
runAsync(),zipWith,ParandScoperun tasks on lightweight JVM-managed threads, enabling millions of concurrent tasks;unsafeRun()andrunSafe()use the calling thread - Laziness: Nothing runs until you call
unsafeRun(),runSafe()orrunAsync(), or convert the path withtoTryPath(), which runs it at once - Composition: Use
mapfor transformations,viafor dependent chains, andParcombinators for parallel execution - Scope: Use
Scopefor flexible structured concurrency withallSucceed,anySucceed,firstComplete, oraccumulatingjoiners - Resource: Use
Resourcefor bracket-pattern resource management with guaranteed cleanup - Error Handling: Prefer
runSafe()to capture failures inTry; usehandleErrorandhandleErrorWithfor graceful fallbacks - Pragmatism: Write simple blocking code that composes naturally; let virtual threads handle the scalability
Practise VTaskPath composition in TutorialVTaskPath.java (9 exercises).
VTaskPath has dedicated JMH benchmarks measuring wrapper overhead on top of raw VTask. Key expectations:
- VTaskPath vs raw VTask: 5-15% overhead (wrapper allocation plus delegation cost)
let()vsfrom():let()is ~20% faster as it avoids VTaskPath wrapping- Wrapper overhead > 30% is a warning sign suggesting unnecessary allocation
- Comparison benchmarks against IOPath are also available
./gradlew :hkj-benchmarks:jmh -Pincludes=".*VTaskPathBenchmark.*"
./gradlew :hkj-benchmarks:jmh -Pincludes=".*VTaskPathVsIOPathBenchmark.*"
See Benchmarks & Performance for full details, comparison benchmarks, and how to interpret results.
- VTask Monad - Underlying type with full API details
- VResultPath - The composition of this path with the typed-error half:
VTask<Either<E, A>>as one railway - Structured Concurrency - Scope and ScopeJoiner for task coordination
- Resource Management - Bracket pattern for safe resource handling
- IOPath - Platform thread-based effect path for single-threaded scenarios
- Composition Patterns - More composition techniques applicable to all Path types
- Patterns and Recipes - Resilience and resource patterns
Previous: IOPath Next: VResultPath