Scala邮箱大小限制 [英] scala mailbox size limit

查看:91
本文介绍了Scala邮箱大小限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在Scala中设置演员邮箱的最大大小吗?

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

解决生产者-消费者问题.使用线程,我可以在缓冲区填满时阻止生产者.我看到了一些用Scala编写的生产者-消费者示例,它们都使用actor,并将邮箱用作缓冲区".我可以设置邮箱大小以使生产者等待消费者准备就绪吗?还有其他优雅的解决方案可以避免邮箱的增长不受控制吗?

Take the Producer-Consumer problem. With threads I can block the producers when the buffer fills up. I saw a couple of producer-consumer examples written in Scala and they all use actors with mailboxes used as a "buffer". Can I set mailbox size to cause producers to wait until a consumer is ready? Any other elegant solution to avoid uncontrollable growth of mailboxes?

推荐答案

您可以创建一个充当生产者和消费者之间缓冲的角色.缓冲区将其邮箱检出到其循环数据.当缓冲产品的数量过多时,它会向生产者发送过载"消息.并在一切恢复正常后发送清除"消息.如果有太多邮件,它只会丢弃传入的(或最旧的)邮件.

You can create an actor that acts as a buffer between the producer and consumer. The buffer checks out its mailbox to its loop data. It sends back an "overload" message to the producer when the number of buffered products is too high; and sends a "clear" message once everything is back in order. In case of too many messages it simply drops incoming ones (or oldest ones).

消费者主动从缓冲区中请求产品,缓冲区又将其发送回一种产品.如果缓冲区为空,则使用者将继续等待输入.

The consumer actively requests for products from the buffer, which in turn sends back one product. If the buffer is empty, the consumer keeps waiting for the input.

生产者将产品发送给缓冲actor.如果收到过载"消息,则可以知道产品可能会掉落的事实,从而停止生产或继续生产.

The producer sends products to the buffer actor. If it receives an "overload" message it can stop production, or it can continue producing, knowing the fact that the products might get dropped.

当然可以直接在生产者或消费者本身中实现此逻辑,但是单独的缓冲区将使您可以更轻松地引入多个生产者和/或消费者.

Of course this logic could directly be implemented into the producer or consumer itself, but a separate buffer will allow you to introduce multiple producers and/or consumers more easily.

这篇关于Scala邮箱大小限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆