3
裏面我有一個功能重試基本上看起來像這樣(簡化):調用了Thread.sleep演員
object SomeObject {
def retry[T](n: Int)(fn: => T): Option[T] = {
val res = try {
Some(fn)
} catch {
case _: Exception => None
}
res match {
case Some(x) => Some(x)
case None =>
if (n > 1)
//make it sleep for a little while
retry(n - 1)(fn)
else None
}
}
}
我需要做的嘗試之間的一些暫停。據我所知,這是不能接受的調用Thread.sleep(123)
演員裏面:
class MyActor extends Actor {
//......
def someFunc = {
Thread.sleep(456) // it's not acceptable in an actor, there is another way to do it
}
}
很顯然,我不知道客戶是否會使用SomeObject.retry
演員裏面:
class MyActor extends Actor {
//......
def someFunc = {
SomeObject.retry(5)(someRequestToServer) // ops, SomeObject.retry uses Thread.sleep!
}
}
所以,如果我只是補充:
res match {
case Some(x) => Some(x)
case None =>
if (n > 1)
//make it sleep for a little while
Thread.sleep(123) // ops, what if it's being called inside an actor by a client?!
retry(n - 1)(fn)
else None
}
}
它不會是明智的,會嗎?如果不是,我該怎麼辦?
如果您將'SomeObject.attempt'中的所有代碼移出它,那麼SomeObject.attempt'方法中剩下的代碼是什麼? –
不是很多。在'attempt'中留下的所有內容都將是'retry'方法中的'try'' catch',它會返回'None'或'Some'。因爲在一個Actor系統中等待/重試的代碼與在純多線程系統中等待/重試的代碼有很大不同,所以很難從兩者中獲得任何可重用的公共代碼。 – theon
可以請你展示完整的例子嗎?我找不到應該在1)SomeObject.attempt 2)def嘗試(...)case Some(x)=> ... –