所以我看了一下Akka源代码,发现这个方法Mailbox
似乎实现了以下行为throughput-deadline-time
:
/**
* Process the messages in the mailbox
*/
@tailrec private final def processMailbox(
left: Int = java.lang.Math.max(dispatcher.throughput, 1),
deadlineNs: Long = if (dispatcher.isThroughputDeadlineTimeDefined == true) System.nanoTime + dispatcher.throughputDeadlineTime.toNanos else 0L): Unit =
if (shouldProcessMessage) {
val next = dequeue()
if (next ne null) {
if (Mailbox.debug) println(actor.self + " processing message " + next)
actor invoke next
if (Thread.interrupted())
throw new InterruptedException("Interrupted while processing actor messages")
processAllSystemMessages()
if ((left > 1) && ((dispatcher.isThroughputDeadlineTimeDefined == false) || (System.nanoTime - deadlineNs) < 0))
processMailbox(left - 1, deadlineNs)
}
}
这段代码很清楚:throughput-deadline-time
配置在切换到另一个参与者的邮箱之前处理同一个邮箱所花费的最长时间。
换句话说,如果您配置调度程序:
my-dispatcher {
throughput = 100
throughput-deadline-time = 1ms
}
然后参与者的邮箱一次最多处理 100 条消息,在最多 1 毫秒内,每当达到这些限制中的第一个时,Akka 就会切换到另一个参与者/邮箱。