akka源码导读

时间:2024-04-23 13:06:06

akka的actor模型提供了强大的并发,本人就akka源码进行了详细的阅读,下面是一些体会。

1、object SystemMessageList:
@tailrec
private[sysmsg] def reverseInner(head: SystemMessage, acc: SystemMessage): SystemMessage = {
if (head eq null) acc else {
val next = head.next
head.next = acc
reverseInner(next, head)
}
}

分析:这是一个反向插入的尾递归,例如:head: m1 -> m2 -> m3 -> m4
1) next = head.next (next = m2 -> m4)
head.next = acc 即 m1 -> acc
reverseInner(m2 -> m4, m1 -> acc)

2) next = head.next(next = m3 -> m4)
head.next = acc 即 m2 -> m1 -> acc
reverseInner(m3 -> m4, m2 -> m1 -> acc)

3) next = head.next (next = m4)
head.next = acc 即 m3 -> m2 -> m1 -> acc
reverseInner(m4, m3 -> m2 -> m1 -> acc)

4) next = head.next (next = null)
head.next = acc 即 m4 -> m3 -> m2 -> m1 -> acc
reverseInner(null, m4 -> m3 -> m2 -> m1 -> acc)

5) head eq null, return m4 -> m3 -> m2 -> m1 -> acc,tailRec结束