scala.concurrent.blocking 的用例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19681389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 05:48:28  来源:igfitidea点击:

Use case of scala.concurrent.blocking

scalascala-2.10

提问by Sourav Chandra

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

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

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.

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

I have some doubts:

我有些疑惑:

  • what is the factor with which new threads will be spawned?
  • Is this applicable only for scala.concurrent.ExecutionContext.Implicits.globalexecution 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.
  • 产生新线程的因素是什么?
  • 这是否仅适用于scala.concurrent.ExecutionContext.Implicits.global执行上下文或用户创建的执行上下文?
  • 如果我用blocking {...包装任何可执行文件会发生什么}
  • 我们应该使用此构造的任何实际用例。

回答by axel22

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

EDIT:

编辑:

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

考虑查看在 Scala 中学习并发编程一书中的第 4 章。