1
ScalaのFuturesを試している間、私はOnCompletionを複数回呼び出すことができます!複数のonCompletionがScalaのFuturesで動作する
質問1 - 明らかに、私はこのようにコードを書くべきではありませんが、コンパイラがこの場合にエラーを発生させるべきかどうか疑問に思っていますか?
import scala.concurrent._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.{Failure,Success}
object ConcurrencyExample extends App {
val time = System.currentTimeMillis()
println("looking at inventory")
//create code we want to execute concurrently
val f: Future[Int] = Future //or just f = Future
{
println("add item to shopping basket")
Thread.sleep(30) //simulate backend process delay
println("Item added")
1 //simulate the no. of items in basket
}
//this works
f onComplete (x => println("on complete1 " + x))
//and this too
f onComplete {
case Success(value) => println("on complete2 size of basket" + value)
case Failure(e) => println(e)
}
//this is called as well though I do not see problem in this as I can segregate the code for completion, success and failure
f onSuccess {
case v => println("on success size of basket"+v)
}
f onFailure {
case e => println("on failure. Reason"+e)
}
for (i<- 1 to 5)
{
println("looking at more items in inventory ")
Thread.sleep(10)
}
Thread.sleep(500)
}
//
looking at inventory
add item to shopping basket
looking at more items in inventory
Item added
on success size of basket1
**on complete2 size of basket1
on complete1 Success(1)**
looking at more items in inventory
looking at more items in inventory
looking at more items in inventory
looking at more items in inventory
質問2の結果 - 決定論的(同じタイプの)複数のコールバックの実行順序ですか?ドキュメントから
私はコールバックがいくつかの別々の実行コンテキストで実行されると仮定しますか? onSuccess(v => Thread.sleep(2000); println( "バスケットの成功サイズ" + v)の場合)でスリープを追加すると、メインプロセスが終了する前に印刷が実行されません。 –
はい、それらは別々のスレッドで実行され、定義されたスレッドプール(あなたのケースではグローバル実行コンテキスト)から取得されます。 – Rumoku