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

  1. it seem thatf0 behave identically f1 , f2 when global executioncontext not backing these futures. alternatively, given code shows global in scope, f1 , f2 might behave differently because f1 might spin more threads when rpc1 , rpc2 block since expressions wrapped within blocking 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) } }

  1. does scala 2.11.4 in fact handle execution of f1 , f2 differently?
  2. is behavior expected change in future scala releases?
  3. how can custom forkjoin threadpool, example 1 one provided akka's configuration, enhanced supports blocking?

     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")
  4. aleksandar prokopec told me easiest way make custom executioncontext responds blocking hint wrap forkjoinpool , implement execute method sets own blockcontext object, or worker threads blockcontext 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

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -