- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尝试掌握如何从参与者而不是线程的角度进行思考。我对以下用例有点困惑:
Consider a system that has a producer process that creates work (e.g. by reading data from a file), and a number of worker processes that consume the work (e.g. by parsing the data and writing it to a database). The rates at which work is produced and consumed can vary, and the system should remain robust to this. For example, if the workers can't keep up, the producer should detect this and eventually slow down or wait.
这很容易用线程实现:
val producer:Iterator[Work] = createProducer()
val queue = new LinkedBlockingQueue[Work](QUEUE_SIZE)
val workers = (0 until NUM_WORKERS) map { i =>
new Thread() {
override def run() = {
while (true) {
try {
// take next unit of work, waiting if necessary
val work = queue.take()
process(work)
}
catch {
case e:InterruptedException => return
}
}
}
}
}
// start the workers
workers.foreach(_.start())
while (producer.hasNext) {
val work = producer.next()
// add new unit of work, waiting if necessary
queue.put(work)
}
while (!queue.isEmpty) {
// wait until queue is drained
queue.wait()
}
// stop the workers
workers.foreach(_.interrupt())
这个模型并没有什么问题,而且我以前也成功地使用过它。这个示例可能过于冗长,因为使用 Executor 或 CompletionService 非常适合此任务。但我喜欢 Actor 抽象,并且认为在很多情况下它更容易推理。有没有办法使用参与者重写这个示例,特别是确保不存在缓冲区溢出(例如邮箱已满、消息丢失等)?
最佳答案
因为参与者“离线”处理消息(即消息的消费与消息的接收无关),所以很难看出如何精确模拟“生产者等待消费者 catch ”。
我唯一能想到的是消费者向生产者参与者请求工作(使用reply
):
case object MoreWorkPlease
class Consumer(prod : Producer) extends Actor {
def act = {
prod ! MoreWorkPlease
loop {
react {
case Work(payload) => doStuff(payload); reply(MoreWorkPlease)
}
}
}
}
class Producer extends Actor {
def act = loop {
react {
case MoreWorkPlease => reply(Work(getNextItem))
}
}
}
当然,这并不完美,因为生产者不会“向前读取”,并且仅在消费者准备好时才开始工作。用法如下:
val prod = new Producer
(1 to NUM_ACTORS).map(new Consumer(prod)).foreach(_.start())
prod.start()
关于scala - 从线程模型到参与者模型的转变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4159340/
我需要并行处理多个数据值(“SIMD”)。我可以使用 java.util.concurrent API (Executors.newFixedThreadPool()) 使用 Future 实例并行处
说未引用的参与者仍然订阅事件流是否正确?至少,这是我从 Akka 实验中得到的…… 我正在尝试在 EventBus 场景中为参与者实现弱引用。在这些情况下,事件监听器/参与者通常来来去去。与应该一直在
场景:我有这个代码: class MyActor extends Actor { def act() { react { case Message() => printl
我正在尝试对一条新闻帖子进行建模,其中包含有关发布该新闻的用户的信息。我认为最好的方法是发送用户摘要信息以及创建新闻帖子的消息,但我有点困惑如果底层用户信息发生变化如何更新该摘要信息。现在我有以下 N
开始学习Actor模型(使用Akka.net),打算在现在的项目中使用。我当前的任务是使用 IFilter 从不同的文件中提取文本。 IFilter 是一组带有 native 代码的 COM 服务器。
我在我的 Linux 服务器上安装了 openfire(版本:Openfire 3.9.3)。它在 2 天前工作正常,具有所有功能。我专注于存档消息插件。什么是问题: 1.当我开始与用户聊天时,它显示
感谢阅读这个问题。我有一个包含 5 个参与者的数据集,其中 10 个参与者有 8 个变量。我需要将数据集的每个参与者/行导出到一个单独的 pdf 文件中,并根据参与者名称为导出的 pdf 文件命名。附
在 Twilio REST API 中,是否可以检索已完成 session 的参与者列表?我试图在 session 结束时计算每个参与者的通话时长。我在 session 创建时传递了一个“ Actio
当使用 LDAP 存储实现 RBAC 模型时(我使用 Apache Directory 1.0.2 作为测试平台),一些参与者显然可以映射到特定的对象类: 资源 - 我没有看到此资源的清晰映射。 ap
我需要将数千个不同格式的 ms office 文档转换为一种通用格式。为了加快速度,我会使用 akka.net 将其并行化。 WordSaveAsActor 应该: 在单线程单元中运行 持有 Word
现在我们的 ASF 集群正在运行: Web API 项目 - 无状态且面向公众 Actor 项目 - 主要是 volatile 的,将数据保存在内存中,由某些 API 使用 我们正在试用 Applic
我在 R 中有一个 data.frame,其中一列指示实验中的参与者 subject,另一列指示 condition、trial_type 和最后一列,我的数字因变量 rt. 这是我使用 dput()
APARAPI project self 描述为: Aparapi allows Java developers to take advantage of the compute power of G
我正在使用 Vidyo.io 服务将视频 session 集成到我的 Android 应用程序中。我已经能够使用 android-vidyo-sdk 附带的 VidyoConnector 示例应用程序
只是一个小介绍: 有任务(任务表) 有用户(用户表) 用户可以参与不同的任务(task_has_user表) 参与者分为作者、实现者、检查者等。 我想获取以逗号分隔的参与者的所有任务列表,如下所示(数
我正在尝试实现 JUnit 测试来测试参与者。 我有这个 ActorTest : import org.junit.Test; import play.libs.Akka; import playte
我是一名优秀的程序员,十分优秀!