如何在 Scala 中编写计时器 Actor?

发布于 2024-12-14 02:40:18 字数 111 浏览 4 评论 0原文

我需要一个演员每分钟发送一条消息。我如何最好地实现这种行为?据我所知,我担心使用 java.lang.Thread.sleep(long millis) ,因为线程可以在 Scala 中的许多参与者之间共享。

I need an actor to send a message every minute. How do I best achieve this behaviour? I am afraid of using java.lang.Thread.sleep(long millis) as a thread can be shared among many actors in Scala, as far as I understand.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

冬天旳寂寞 2024-12-21 02:40:18

或者正如 @Daniel 提到的,这里有一个运行示例:

import scala.actors._
import scala.actors.Actor._

class TimerActor(val timeout: Long,val who: Actor,val reply: Any) extends Actor {
  def act {
    loop {
      reactWithin(timeout) {
        case TIMEOUT => who ! reply
      }
    }
  }
}

val a = actor {
  loop {
    react {
      case x => println(x)
    }
  }
}

val t = new TimerActor(1000, a, "Go for it")

a.start
t.start

Or as @Daniel mentioned, here a running example:

import scala.actors._
import scala.actors.Actor._

class TimerActor(val timeout: Long,val who: Actor,val reply: Any) extends Actor {
  def act {
    loop {
      reactWithin(timeout) {
        case TIMEOUT => who ! reply
      }
    }
  }
}

val a = actor {
  loop {
    react {
      case x => println(x)
    }
  }
}

val t = new TimerActor(1000, a, "Go for it")

a.start
t.start
滥情空心 2024-12-21 02:40:18

使用 receiveWithin 创建一个 Actor 来充当计时器。

Create an actor with receiveWithin to act as the timer.

桃扇骨 2024-12-21 02:40:18

您可以使用 Akka FSM 来建模保持 forMax 的 Actor millis 处于等待状态,然后发送消息,例如,通过在使用 onTransition 时切换到另一个状态并停留 0 millis 以切换回等待状态。 akka 页面上有一个很好的例子。

You can use Akka FSM to model an actor that stays forMax millis in a waiting state and then sends a message, e.g. by switching to another state while using onTransition and staying there for 0 millis to switch back to waiting state. There is a good example at the akka page.

南风几经秋 2024-12-21 02:40:18
import scala.actors._
class Wakeup[A](millis: Int, who: ReplyReactor, alarm: A) extends Thread {
  val done = new java.util.concurrent.atomic.AtomicBoolean(false)
  override def run {
    while (!done.get()) {
      who ! alarm
      Thread.sleep(millis)
    }
  }
}
case object BEEP {}
val a = new ReplyReactor { def act { loop { react {
  case BEEP => println("Wha?!  "+new java.util.Date)
  case _ =>
}}}}
val b = new Wakeup(60000,a,BEEP)
a.start

当您想要线程时为什么要使用演员呢?

scala> b.start

scala> Wha?!  Mon Nov 07 18:43:18 EST 2011
Wha?!  Mon Nov 07 18:44:18 EST 2011
Wha?!  Mon Nov 07 18:45:18 EST 2011
Wha?!  Mon Nov 07 18:46:18 EST 2011
Wha?!  Mon Nov 07 18:47:18 EST 2011
Wha?!  Mon Nov 07 18:48:18 EST 2011
Wha?!  Mon Nov 07 18:49:18 EST 2011
Wha?!  Mon Nov 07 18:50:18 EST 2011
Wha?!  Mon Nov 07 18:51:18 EST 2011
Wha?!  Mon Nov 07 18:52:18 EST 2011
import scala.actors._
class Wakeup[A](millis: Int, who: ReplyReactor, alarm: A) extends Thread {
  val done = new java.util.concurrent.atomic.AtomicBoolean(false)
  override def run {
    while (!done.get()) {
      who ! alarm
      Thread.sleep(millis)
    }
  }
}
case object BEEP {}
val a = new ReplyReactor { def act { loop { react {
  case BEEP => println("Wha?!  "+new java.util.Date)
  case _ =>
}}}}
val b = new Wakeup(60000,a,BEEP)
a.start

Why use an actor when a thread is what you want?

scala> b.start

scala> Wha?!  Mon Nov 07 18:43:18 EST 2011
Wha?!  Mon Nov 07 18:44:18 EST 2011
Wha?!  Mon Nov 07 18:45:18 EST 2011
Wha?!  Mon Nov 07 18:46:18 EST 2011
Wha?!  Mon Nov 07 18:47:18 EST 2011
Wha?!  Mon Nov 07 18:48:18 EST 2011
Wha?!  Mon Nov 07 18:49:18 EST 2011
Wha?!  Mon Nov 07 18:50:18 EST 2011
Wha?!  Mon Nov 07 18:51:18 EST 2011
Wha?!  Mon Nov 07 18:52:18 EST 2011
獨角戲 2024-12-21 02:40:18

我最终创建了专用的 Runnable 实例,它不断向目标参与者发送消息。喜欢

case class QueueTick()

class QueueWatcherActor extends Actor {

  override def receive = {
    case QueueTick() => // do it here
  }

}

val ref = ActorSystem("xxx")

val actor = ref.actorOf(Props[QueueWatcherActor])

val executor = Executors.newSingleThreadScheduledExecutor()

executor.scheduleAtFixedRate(new Runnable {
  def run() {
    actor ! QueueTick()
  }
},1,60,TimeUnit.SECONDS)

I ended up in creation of dedicated Runnable instance, which keeps sending a message to the target actor. Like

case class QueueTick()

class QueueWatcherActor extends Actor {

  override def receive = {
    case QueueTick() => // do it here
  }

}

val ref = ActorSystem("xxx")

val actor = ref.actorOf(Props[QueueWatcherActor])

val executor = Executors.newSingleThreadScheduledExecutor()

executor.scheduleAtFixedRate(new Runnable {
  def run() {
    actor ! QueueTick()
  }
},1,60,TimeUnit.SECONDS)
却一份温柔 2024-12-21 02:40:18

由于 scala.actors 现已被弃用并被 akka Actor 取代(并且由于 akka Actor 中没有 React 或 receiveWithin ),因此以下是如何使用 akka Actor 来做到这一点(无论如何,它实际上比使用 receiveWithin 更不“黑客”) )。

下面的示例安排一个可运行对象在 5 秒后调用:

import akka.actor.{ActorSystem, Scheduler}
import scala.concurrent.duration.FiniteDuration
import scala.concurrent.ExecutionContext.Implicits.global

class TimerExample {
def example() = {

    def scheduler: Scheduler = ActorSystem.create("timer-example").scheduler

    val myRunnable = new Runnable {
      override def run(): Unit = {
        println("run invoked")
      }
    }

    println("scheduling...")
    scheduler.scheduleOnce(FiniteDuration(5,TimeUnit.SECONDS),myRunnable)
    Thread.sleep(6000)
    println("should have printed 'run invoked'")
}

Since scala.actors is now deprecated and being replaced with akka actors (and since there is no react or receiveWithin in akka actors), here is how to do it using akka actors (it's actually less of a 'hack' than using receiveWithin anyways IMHO).

The example below schedule a runnable to be invoked after 5 seconds:

import akka.actor.{ActorSystem, Scheduler}
import scala.concurrent.duration.FiniteDuration
import scala.concurrent.ExecutionContext.Implicits.global

class TimerExample {
def example() = {

    def scheduler: Scheduler = ActorSystem.create("timer-example").scheduler

    val myRunnable = new Runnable {
      override def run(): Unit = {
        println("run invoked")
      }
    }

    println("scheduling...")
    scheduler.scheduleOnce(FiniteDuration(5,TimeUnit.SECONDS),myRunnable)
    Thread.sleep(6000)
    println("should have printed 'run invoked'")
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文