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 futures 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
f0behave identicallyf1,f2when globalexecutioncontextnot backing thesefutures. alternatively, given code showsglobalin scope,f1,f2might behave differently becausef1might spin more threads whenrpc1,rpc2block since expressions wrapped withinblockingexpressions.
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,f2differently? - is behavior expected change in future scala releases?
how can custom
forkjointhreadpool, 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
executioncontextrespondsblockinghint wrapforkjoinpool, implementexecutemethod sets ownblockcontextobject, or worker threadsblockcontextobjects. 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