- Loading...
...
The main benefit of structured concurrency is abstraction. A caller of a method that is invoked to do a task should not care if the method decomposes the task and schedules a million fibers. When the method completes, any fibers scheduled by the method should have terminated.
Further reading:
Nathaniel J. Smith: Notes on structured concurrency, or: Go statement considered harmful
Martin Sustrik: Structured Concurrency
In Project Loom, a prototype API has been developed called FiberScope that is a scope in which fibers are scheduled. Here is a basic example:
...
FiberScope supports creating and entering a scope with a deadline, expressed as a java.time. Instant. If the deadline is reached before the thread/fiber exits the scope then all fibers scheduled in the scope are cancelled.
...
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 is reached in the meantime then the fiber will be cancelled.
In addition to withDeadline, FiberScope also defines open(Duration timeout) to enter a scope with a timeout, expressed as a java.time.Duration. If . If the timeout expires before thread/fiber exits the scope then all fibers scheduled in the scope are cancelled.
Further reading:
Nathaniel J. Smith: Notes on structured concurrency, or: Go statement considered harmful
Nathaniel J. Smith: Timeouts and cancellation for humans
Martin Sustrik: Structured Concurrency
...