Questions about scala.concurrent.Blocking -
i believe following true. corrections , clarifications welcome:
i/o bound tasks can tie threads , idle them extended periods of time. global
threadpool has special provision detecting , handling specially marked code within future
s block. code contained within blocking {}
block within future
backed forkjoin
threadpool monitored idleness, , threads allocated necessary. enclosing code within blocking {}
not executed future
backed suitably configured forkjoin
threadpool has no effect. blocking
method has following signature:
def blocking[t](body: → t): t
blocking
invocations seem designed aware of each other , co-operative, via 1 or more blockcontext
references of no concern average scala programmer.
questions
- it seem that
f0
behave identicallyf1
,f2
when globalexecutioncontext
not backing thesefuture
s. alternatively, given code showsglobal
in scope,f1
,f2
might behave differently becausef1
might spin more threads whenrpc1
,rpc2
block since expressions wrapped withinblocking
expressions.
import scala.concurrent._ import scala.concurrent.executioncontext.implicits.global val f0 = future { val w = nonblockingcomputation1() val x = rpc1(param1, param2, etc) val y = nonblockingcomputation2() val z = rpc2(param3, param4, etc) dosomethingwith(w, x, y, z) } val f1 = future { val w = nonblockingcomputation1() val x = <span class="yellow">blocking</span> { rpc1(param1, param2, etc) } val y = nonblockingcomputation2() val z = blocking { rpc2(param3, param4, etc) } dosomethingwith(w, x, y, z) } val f2 = future { blocking { val w = nonblockingcomputation1() val x = rpc1(param1, param2, etc) val y = nonblockingcomputation2() val z = rpc2(param3, param4, etc) dosomethingwith(w, x, y, z) } }
- does scala 2.11.4 in fact handle execution of
f1
,f2
differently? - is behavior expected change in future scala releases?
how can custom
forkjoin
threadpool, example 1 one provided akka's configuration, enhanced supportsblocking
?import scala.concurrent._ import scala.concurrent.executioncontext.implicits.global val config = """ |akka { | my-dispatcher { | type = dispatcher | executor = "fork-join-executor" | fork-join-executor { | parallelism-min = 2 | parallelism-factor = 2.0 | parallelism-max = 10 | } | throughput = 100 | } |} |""".stripmargin implicit val executioncontext = system.dispatchers.lookup("my-dispatcher")
- aleksandar prokopec told me easiest way make custom
executioncontext
respondsblocking
hint wrapforkjoinpool
, implementexecute
method sets ownblockcontext
object, or worker threadsblockcontext
objects. see: github.com/scala/scala/blob/v2.11.4/src/library/scala/concurrent/blockcontext.scala. unfortunately did not tell me much. can show me concrete example?
Comments
Post a Comment