【发布时间】:2013-08-29 16:18:52
【问题描述】:
我在 Scala 中有一个包含一些方法的类,每个方法都有一个执行时间,例如 methodA 需要 10 秒,methodB 需要 5 秒。并且每个方法都异步调用。当我调用methodB 时,它应该取消正在运行另一个方法的线程。我首先调用methodA,2秒后调用methodB。这个问题的最佳解决方案是什么?
def methodA()={
async{
// a job that takes 10 seconds
}
}
def methodB()={
async{
// other methods should stop their job
// a job that takes 5 second
}
}
def async[T](fn: => Unit): Unit = scala.actors.Actor.actor {
fn
}
.....
methodA()
methodB()
【问题讨论】:
标签: scala