Use case scala.concurrent.blocking

I came across the scala.concurrent.blocking method, and according to the Scala documentation this is ...

Used to indicate a piece of code that potentially blocks, allowing the current BlockContext to configure runtime behavior. Properly labeled lock code can improve performance or avoid deadlocks.

I have some doubts:

  • What is the factor by which new threads will appear?
  • Is this applicable only to the scala.concurrent.ExecutionContext.Implicits.global runtime scala.concurrent.ExecutionContext.Implicits.global or to custom runtime contexts?
  • What happens if I complete any executable with blocking { ... } ?
  • Any practical case when we should use this design.
+58
Oct 30 '13 at 11:47
source share
2 answers
  • New threads appear in the fork / join pool when it discovers that all threads in the fork / join pool are waiting for each other using the join construct, and much work remains to be done that could finish one of the threads. Alternatively, if one of the ForkJoinWorker threads executes code that blocks, except with join , it can notify the pool using ManagedBlocker s .
  • It is potentially applicable to any execution contexts - it serves as a notification about the implementation of the ExecutionContext that the code executed by the work thread potentially blocks a condition and that this condition can 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 only work with the global default execution context.
  • If you terminate any executable file with a lock, you will impose a little time on execution, therefore you do not always do this.
  • If you have a calculation that takes a long time, such as seconds or minutes, or you expect when you finish using Await , or you expect that the state of the monitor will be resolved, and this condition can be resolved by another task / future, which should run in the same execution context - in all of these cases you should use blocking .

EDIT:

Take a look at Chapter 4 in Scala Learning Parallel Programming .

+50
Oct 30 '13 at 12:23
source share

I have always found the official documentation a bit poor in regards to the blocking design; so I tried to explain it more clearly in this blog post with examples of how you could implement your own and how it works under the hood.

+5
Jun 24 '16 at 21:58
source share



All Articles