I am learning Akka Actor recently. I read the document of dispatchers in Actor. I am curious about the blocking operation in an actor. The last topic in the document describes how to solve the problem. And I am trying to reproduce the example experiment in the document.
Here is my code:
package dispatcher
import akka.actor.{ActorSystem, Props}
import com.typesafe.config.ConfigFactory
object Main extends App{
var config = ConfigFactory.parseString(
"""
|my-dispatcher{
|type = Dispatcher
|
|executor = "fork-join-executor"
|
|fork-join-executor{
|fixed-pool-size = 32
|}
|throughput = 1
|}
""".stripMargin)
// val system = ActorSystem("block", ConfigFactory.load("/Users/jiexray/IdeaProjects/ActorDemo/application.conf"))
val system = ActorSystem("block")
val actor1 = system.actorOf(Props(new BlockingFutureActor()))
val actor2 = system.actorOf(Props(new PrintActor()))
for(i <- 1 to 1000){
actor1 ! i
actor2 ! i
}
}
package dispatcher
import akka.actor.Actor
import scala.concurrent.{ExecutionContext, Future}
class BlockingFutureActor extends Actor{
override def receive: Receive = {
case i: Int =>
Thread.sleep(5000)
implicit val excutionContext: ExecutionContext = context.dispatcher
Future {
Thread.sleep(5000)
println(s"Blocking future finished ${i}")
}
}
}
package dispatcher
import akka.actor.Actor
class PrintActor extends Actor{
override def receive: Receive = {
case i: Int =>
println(s"PrintActor: ${i}")
}
}
I simply create an ActorSystem
with the default dispatchers and all actors depend on those. The BlockingFutureActor
has a blocking operation that is encapsulated in a Future
. The PrintActor
is merely printing a number instantly.
In the document's explanation, the default dispatchers will be occupied by Future
s in the BlockingFutureActor
, which leads to the message blocking of the PrintActor
. The application gets stuck somewhere like:
> PrintActor: 44
> PrintActor: 45
Unfortunately, my code is not blocked. All outputs from PrintActor
show up smoothly. But outputs from BlockingFutureActor
show up like squeezing toothpaste. I try to monitor my thread info by Intellij's Debug, I got:
You may find only two dispatchers are sleeping(BlockingFutureActor
makes this happen). Others are waiting, which means they are available for new message delivering.
I have read an answer about blocking operation in Actor(page). It is quoted that "Dispatchers are, effectively, thread-pools. Separating the two guarantees that the slow, blocking operations don't starve the other. This approach, in general, is referred to as bulk-heading, because the idea is that if a part of the app fails, the rest remains responsive."
Do default dispatchers spare some dispatcher for blocking operation? Such that the system can handle messages even if there are so many blocking operations asking for dispatchers.
Can the experiment in the Akka document be reproduced? Is there something wrong with my configuration.
Thanks for your suggestions. Best Wishes.
PrintActor
show up smoothly." Are you saying that you see all 1000println
statements fromPrintActor
? – Jeffrey Chungprintln
show up at the moment that the application starts. – jiexray