Why are Futures within Futures running sequentially when started on Akka Dispatcher

后端 未结 3 1918
野趣味
野趣味 2021-01-02 19:59

We observed a strange behavior when we tried to start a number of futures from within an actor\'s receive method. If we use our configured dispatchers as ExecutionContext,

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-02 20:28

    From the description of Akka's internal BatchingExecutor (emphasis mine):

    Mixin trait for an Executor which groups multiple nested Runnable.run() calls into a single Runnable passed to the original Executor. This can be a useful optimization because it bypasses the original context's task queue and keeps related (nested) code on a single thread which may improve CPU affinity. However, if tasks passed to the Executor are blocking or expensive, this optimization can prevent work-stealing and make performance worse....A batching executor can create deadlocks if code does not use scala.concurrent.blocking when it should, because tasks created within other tasks will block on the outer task completing.

    If you're using a dispatcher that mixes in BatchingExecutor--namely, a subclass of MessageDispatcher--you could use the scala.concurrent.blocking construct to enable parallelism with nested Futures:

    Future {
      Future {
        blocking {
          doBlockingWork()
        }
      }
    }
    

    In your example, you would add blocking in the startFuture method:

    private def startFuture(id: Int)(implicit executionContext: ExecutionContext): Future[Unit] = Future {
      blocking {
        println(s"Future $id should run for 500 millis on thread ${Thread.currentThread().getName()}")
        Thread.sleep(500)
        println(s"Future $id finished on thread ${Thread.currentThread().getName()}")
      }
    }
    

    Sample output from running startFutures(true)(actorSystem.dispatcher) with the above change:

    Start Futures on thread Experimental-akka.actor.default-dispatcher-2
    Started Futures on thread Experimental-akka.actor.default-dispatcher-2
    Future 1 should run for 500 millis on thread Experimental-akka.actor.default-dispatcher-2
    Future 3 should run for 500 millis on thread Experimental-akka.actor.default-dispatcher-3
    Future 5 should run for 500 millis on thread Experimental-akka.actor.default-dispatcher-6
    Future 7 should run for 500 millis on thread Experimental-akka.actor.default-dispatcher-7
    Future 4 should run for 500 millis on thread Experimental-akka.actor.default-dispatcher-5
    Future 9 should run for 500 millis on thread Experimental-akka.actor.default-dispatcher-10
    Future 6 should run for 500 millis on thread Experimental-akka.actor.default-dispatcher-8
    Future 8 should run for 500 millis on thread Experimental-akka.actor.default-dispatcher-9
    Future 2 should run for 500 millis on thread Experimental-akka.actor.default-dispatcher-4
    Future 1 finished on thread Experimental-akka.actor.default-dispatcher-2
    Future 3 finished on thread Experimental-akka.actor.default-dispatcher-3
    Future 5 finished on thread Experimental-akka.actor.default-dispatcher-6
    Future 4 finished on thread Experimental-akka.actor.default-dispatcher-5
    Future 8 finished on thread Experimental-akka.actor.default-dispatcher-9
    Future 7 finished on thread Experimental-akka.actor.default-dispatcher-7
    Future 9 finished on thread Experimental-akka.actor.default-dispatcher-10
    Future 6 finished on thread Experimental-akka.actor.default-dispatcher-8
    Future 2 finished on thread Experimental-akka.actor.default-dispatcher-4
    

提交回复
热议问题