Use case of scala.concurrent.blocking

  1. The new threads are spawned in the fork/join pool when it detects
    that all the threads in the fork/join pool are waiting on each other
    using the join construct, and there is more work to be completed
    that could potentially finish one of the threads.
    Alternatively, if one of the ForkJoinWorker threads is executing code that blocks other than by using join, it can notify the pool using ManagedBlockers.
  2. It is potentially applicable to any kind of execution contexts — it serves as a notification to the ExecutionContext implementation that the code executed by a worker thread is potentially blocking on some condition, and that this condition might be resolved by computing something else using some other thread. The execution context may or may not act on this. In the current (2.10, 2.11) implementation, blocking will work only with the default global execution context.
  3. If you wrap any executable with blocking you will induce a bit of runtime overhead, so don’t always do it.
  4. If you have a computation that lasts a long time, e.g. seconds or minutes, or you are waiting on a future to complete using Await, or you are waiting on a monitor’s condition to become resolved, and this condition can be resolved by some other task/future that should execute on the same execution context — in all these cases you should use blocking.

EDIT:

Consider taking a look at Chapter 4 in the Learning Concurrent Programming in Scala book.

Leave a Comment