- Loading...
...
scope1 is entered with a deadline that is now + 10s. It schedules a fiber in scope2 and cannot exit to scope1 until the fiber terminates. If the deadline expires in the meantime then the fiber will be cancelled and the thread/fiber will throw CancelledException(“Deadline expired”) when existing scope1. If the inner scope had a deadline that was further into the future that the deadline then the deadline for the outer scope will expire first.
Fibers communicate to other fibers or threads using queues or other mechanisms. They may also return a result, retrieved by invoking the Fiber’s join method. As a convenience when scheduling fibers in a FiberScope, the API defines schedule methods that specify a termination queue. The Fiber is queued to the termination queue when it terminates. The following examples schedules fibers to executes tasks that return Strings. The fibers are queued to a termination queue then they terminate. The results are obtained by invoking the Fiber::join method.
| Code Block | ||
|---|---|---|
| ||
try (var scope = FiberScope.cancellable()) {
var queue = new FiberScope.TerminationQueue<String>();
Arrays.stream(tasks).forEach(task -> scope.schedule(task, queue));
IntStream.range(0, tasks.length)
.mapToObj(x -> queue.takeUninterruptibly())
.map(Fiber::join)
.forEach(System.out::println);
} |