Sleeping actors?

前端 未结 4 1006
面向向阳花
面向向阳花 2021-01-31 20:44

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).

4条回答
  •  清歌不尽
    2021-01-31 21:14

    There unfortunately are two errors in the answer of oxbow_lakes.

    One is a simple declaration mistake (long time vs time: Long), but the second is some more subtle.

    oxbow_lakes declares run as

    def run = actors.Scheduler.execute(f) 
    

    This however leads to messages disappearing from time to time. That is: they are scheduled but get never send. Declaring run as

    def run = f
    

    fixed it for me. It's done the exact way in the ActorPing of lift-util.

    The whole scheduler code becomes:

    object Scheduler {
        private lazy val sched = Executors.newSingleThreadedScheduledExecutor();
        def schedule(f: => Unit, time: Long) {
            sched.schedule(new Runnable {
              def run = f
            }, time - Platform.currentTime, TimeUnit.MILLISECONDS);
        }
    }
    

    I tried to edit oxbow_lakes post, but could not save it (broken?), not do I have rights to comment, yet. Therefore a new post.

提交回复
热议问题