睡觉的演员? [英] Sleeping actors?

查看:94
本文介绍了睡觉的演员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

演员睡觉的最好方法是什么?我已经将参与者设置为代理,他们希望维护数据库的不同部分(包括从外部资源获取数据)。由于多种原因(包括数据库或通信不过载以及一般的负载问题),我希望参与者在每次操作之间休眠。我正在查看类似10个actor对象的东西。

What's the best way to have an actor sleep? I have actors set up as agents which want to maintain different parts of a database (including getting data from external sources). For a number of reasons (including not overloading the database or communications and general load issues), I want the actors to sleep between each operation. I'm looking at something like 10 actor objects.

actor将无限运行,因为总是会有新数据传入,或者坐在表中等待可以将其传播到数据库的其他部分等。这个想法是使数据库在任何时间点都尽可能完整。

The actors will run pretty much infinitely, as there will always be new data coming in, or sitting in a table waiting to be propagated to other parts of the database etc. The idea is for the database to be as complete as possible at any point in time.

我可以使用无限循环,并在每个循环结束时进行睡眠,但根据 http://www.scala -lang.org/node/242 actor使用线程池,只要所有线程都被阻塞,线程池就会扩展。因此,我想每个角色中的Thread.sleep都是一个坏主意,因为这会不必要地浪费线程。

I could do this with an infinite loop, and a sleep at the end of each loop, but according to http://www.scala-lang.org/node/242 actors use a thread pool which is expanded whenever all threads are blocked. So I imagine a Thread.sleep in each actor would be a bad idea as would waste threads unnecessarily.

我也许可以有一个中央角色,其自己的循环发送消息

I could perhaps have a central actor with its own loop that sends messages to subscribers on a clock (like async event clock observers)?

有没有人做过类似的事情或有什么建议?抱歉,需要额外的信息(可能是多余的信息)。

Has anyone done anything similar or have any suggestions? Sorry for extra (perhaps superfluous) information.

欢呼声

Joe

推荐答案

不需要显式地使演员睡觉:使用 loop react 表示每个参与者都意味着底层线程池将具有等待线程,而没有消息可供参与者进行处理。

There's no need to explicitly cause an actor to sleep: using loop and react for each actor means that the underlying thread pool will have waiting threads whilst there are no messages for the actors to process.

您想安排事件以供演员处理,这很容易使用 java.util.concurrent 实用程序中的单线程计划程序进行:

In the case that you want to schedule events for your actors to process, this is pretty easy using a single-threaded scheduler from the java.util.concurrent utilities:

object Scheduler {
  import java.util.concurrent.Executors
  import scala.compat.Platform
  import java.util.concurrent.TimeUnit
  private lazy val sched = Executors.newSingleThreadScheduledExecutor();
  def schedule(f: => Unit, time: Long) {
    sched.schedule(new Runnable {
      def run = f
    }, time , TimeUnit.MILLISECONDS);
  }
}

您可以将其扩展为执行定期任务,并且可能可以这样使用:

You could extend this to take periodic tasks and it might be used thus:

val execTime = //...  
Scheduler.schedule( { Actor.actor { target ! message }; () }, execTime)

然后,您的目标参与者只需实施适当的 react 循环来处理给定的消息。不需要演员睡觉。

Your target actor will then simply need to implement an appropriate react loop to process the given message. There is no need for you to have any actor sleep.

这篇关于睡觉的演员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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