@@ -618,26 +618,37 @@ This will allow external frameworks to provide more specialized utilities.
618
618
619
619
## Blocking
620
620
621
- ### in a Future
621
+ Futures are generally asynchronous and do not block the underlying execution threads.
622
+ However, in certain cases, it is necessary to block.
623
+ We distinguish two forms of blocking the execution thread:
624
+ invoking arbitrary code that blocks the thread from within the future,
625
+ and blocking from outside another future, waiting until that future gets completed.
626
+
627
+
628
+ ### Blocking inside a Future
622
629
623
630
As seen with the global ` ExecutionContext ` , it is possible to notify an ` ExecutionContext ` of a blocking call with the ` blocking ` construct.
624
631
The implementation is however at the complete discretion of the ` ExecutionContext ` . While some ` ExecutionContext ` such as ` ExecutionContext.global `
625
- implement ` blocking ` by means of ` ManagedBlocker ` , some just do nothing:
632
+ implement ` blocking ` by means of a ` ManagedBlocker ` , some execution contexts such as the fixed thread pool:
633
+
634
+ ExecutionContext.fromExecutor(Executors.newFixedThreadPool(x))
635
+
636
+ will do nothing, as shown in the following:
626
637
627
638
implicit val ec = ExecutionContext.fromExecutor(
628
639
Executors.newFixedThreadPool(4))
629
640
Future {
630
- // here blocking serves only for documentation purpose
631
641
blocking { blockingStuff() }
632
642
}
633
643
634
- Is equivalent to
644
+ Has the same effect as
635
645
636
646
Future { blockingStuff() }
637
647
638
648
The blocking code may also throw an exception. In this case, the exception is forwarded to the caller.
639
649
640
- ### on a Future
650
+
651
+ ### Blocking outside the Future
641
652
642
653
As mentioned earlier, blocking on a future is strongly discouraged
643
654
for the sake of performance and for the prevention of deadlocks.
0 commit comments