我有這樣的特質訪問的ExecutionContext
trait NonBlockingGoodness extends DataStore {
import akka.dispatch.{ Future, ExecutionContext }
import akka.util.duration._
import akka.util.Timeout
implicit val ec = ExecutionContext.fromExecutorService(yourExecutorServiceGoesHere)
implicit lazy val timeout = Timeout(5 seconds)
}
我想訪問ExecutionContext
在另一個特質像這樣
trait AsyncGoodness extends NonBlockingGoodness {
import akka.dispatch.Future
def doSomething = {
Future { "Future is the bomb." }
}
不過,我得到錯誤
Could not find implicit value for parameter executor: akka.dispatch.ExecutionContext
已更新: 我想出了h ow得到ExecutionContext
範圍內
trait AsyncGoodness extends NonBlockingGoodness {
import akka.dispatch.ExecutionContext
import akka.dispatch.Future
def doSomething()(implicit executor: ExecutionContext) = {
Future { "Future is the bomb." }
}
但是,我有一個後續問題。由於我可能在AsyncGoodness
中使用ExecutionContext
以上的一種以上方法,有沒有辦法將它傳遞到trait
級別,而不是像上面我做的那樣在每個方法上傳遞它。
只是一個傳遞的評論:你的名字NonBlockingGoodness意味着所有阻塞是壞的。這是近來常見的觀點,但有點天真:在某些情況下,非阻塞IO可能比阻塞IO更糟。其他問題,例如過度平行並且有(或者沒有)調整好的線程池,甚至避免Amdahl法則的陷阱必須被考慮。以下是一個案例歷史記錄:http://www.bigbeeconsultants.co.uk/blog/dispatch-http-critique –
在他使用它的環境中,非阻塞是否是好的就足夠了?或者你是否認爲這個名字僅僅意味着這個特質會有一些善意,而恰恰是在非阻塞地進行呢?這是什麼,Tumblr? – hoff2