我应该如何在 Scala 中安排一些简单的延迟任务? [英] How should I schedule some simple delayed tasks in Scala?

查看:58
本文介绍了我应该如何在 Scala 中安排一些简单的延迟任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作 Chaos Monkey 程序,我希望它在一段时间后自行清理.我想要一种简单的方法来排队清理任务,以便将来在一定时间内调用.我想我可以和演员一起做一些事情,并挥舞很多手,但这似乎是错误的方法.Scala 标准库中是否有更好的工具来完成这项任务?

I'm making a Chaos Monkey program and I want it to clean up after itself after a certain period of time. I'd like a simple way to queue up cleanup tasks to be called a set amount of time in the future. I think I could do something with actors and a lot of hand-waving but that seems like the wrong approach. Is there a better tool for this task in the Scala standard library?

推荐答案

我用 Scala 编写了一个调度 DSL,名为 foil,可在 Github 上免费获得.它将与 Java 日历/日期或 Joda 库一起使用.语法如下所示:

I have written a scheduling DSL in Scala called foil, which is freely available on Github. It will work with either of Java Calendar/Date, or the Joda library. The syntax looks like this:

schedule(f) now
schedule(f) onceAfter 5.minutes
schedule(f) onceAt inst
schedule(f) onceAtNext time
schedule(f) todayNoEarlierThan time

其中 f 是一个闭包(即 () => Unit).还有更多的例子在维基上和一个示例 REPL 会话 带箔,展示如何使用它(Java 日期/日历和 JODA).

Where f is a closure (i.e. () => Unit). There's many more examples on the Wiki and an example REPL session with foil, showing how to use it (with both Java Date/Calendar and JODA).

这篇关于我应该如何在 Scala 中安排一些简单的延迟任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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