scala.concurrent.blocking 的用例 [英] Use case of scala.concurrent.blocking

查看:29
本文介绍了scala.concurrent.blocking 的用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了 scala.concurrent.blocking 方法,根据 Scala 文档,这是...

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

用于指定一段可能阻塞的代码,允许当前 BlockContext 以调整运行时的行为.适当地标记阻塞代码可能会提高性能或避免死锁.

Used to designate a piece of code which potentially blocks, allowing the current BlockContext to adjust the runtime's behavior. Properly marking blocking code may improve performance or avoid deadlocks.

我有一些疑问:

  • 产生新线程的因素是什么?
  • 这仅适用于 scala.concurrent.ExecutionContext.Implicits.global 执行上下文还是也适用于用户创建的执行上下文?
  • 如果我用 blocking { ... } 包装任何可执行文件会发生什么?
  • 我们应该使用此构造的任何实际用例.
  • what is the factor with which new threads will be spawned?
  • Is this applicable only for scala.concurrent.ExecutionContext.Implicits.global execution context or for user-created execution contexts as well?
  • What happens if I wrap any executable with blocking { ... }?
  • Any practical use case where we should use this construct.

推荐答案

  1. 当检测到新线程时,它会在 fork/join 池中产生fork/join 池中的所有线程都在互相等待使用 join 构造,还有更多工作需要完成这可能会完成其中一个线程.或者,如果 ForkJoinWorker 线程之一正在执行阻塞的代码而不是使用 join,它可以使用 ManagedBlockers.
  2. 它可能适用于任何类型的执行上下文——它作为对 ExecutionContext 实现的通知,即工作线程执行的代码可能在某些情况下阻塞,并且这种情况可以通过使用其他线程计算其他内容来解决.执行上下文可能会也可能不会对此采取行动.在当前 (2.10, 2.11) 实现中,blocking 仅适用于默认的全局执行上下文.
  3. 如果您使用阻塞来包装任何可执行文件,则会产生一些运行时开销,因此不要总是这样做.
  4. 如果您有一个持续很长时间的计算,例如秒或分钟,或者您正在使用 Await 等待未来完成,或者您正在等待监视器的条件得到解决,并且此条件可以由应该执行的其他任务/未来解决在相同的执行上下文中——在所有这些情况下,你应该使用 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.

考虑查看 Learning Concurrent Programming in Scala 一书中的第 4 章.

这篇关于scala.concurrent.blocking 的用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆