8000 addressing review comments · Renkai/scala.github.com@e61c7f5 · GitHub
[go: up one dir, main page]

Skip to content

Commit e61c7f5

Browse files
committed
addressing review comments
1 parent 307ab00 commit e61c7f5

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

overviews/core/_posts/2012-09-20-futures.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -618,26 +618,37 @@ This will allow external frameworks to provide more specialized utilities.
618618

619619
## Blocking
620620

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
622629

623630
As seen with the global `ExecutionContext`, it is possible to notify an `ExecutionContext` of a blocking call with the `blocking` construct.
624631
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:
626637

627638
implicit val ec = ExecutionContext.fromExecutor(
628639
Executors.newFixedThreadPool(4))
629640
Future {
630-
// here blocking serves only for documentation purpose
631641
blocking { blockingStuff() }
632642
}
633643

634-
Is equivalent to
644+
Has the same effect as
635645

636646
Future { blockingStuff() }
637647

638648
The blocking code may also throw an exception. In this case, the exception is forwarded to the caller.
639649

640-
### on a Future
650+
651+
### Blocking outside the Future
641652

642653
As mentioned earlier, blocking on a future is strongly discouraged
643654
for the sake of performance and for the prevention of deadlocks.

0 commit comments

Comments
 (0)
0