scala mailbox size limit

前端 未结 2 1082
刺人心
刺人心 2020-12-16 14:53

Can I set maximum size for an actor\'s mailbox in Scala?

Take the Producer-Consumer problem. With threads I can block the producers when the buffer fills up. I saw a

2条回答
  •  囚心锁ツ
    2020-12-16 14:58

    The Actor.mailboxSize method returns the number of pending messages in the Actor's mailbox.

    This can be used for throttling the producer in various ways.

    For example, one possibility could be,

    The producer checks if the consumer's mailboxSize is greater than some threshold. If it is, then it sends a SpecialMessage to the consumer, and blocks on a semaphore. When the consumer receives this SpecialMessage it releases the semaphore. The producer can now merrily continue it's business.

    This avoids polling as well as any dropped messages.

提交回复
热议问题