2016-12-28 1 views
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の結果 - 決定論的(同じタイプの)複数のコールバックの実行順序ですか?ドキュメントから

答えて

5

次の引用は、両方のあなたの質問に答えることがあります。

onCompleteの、するonSuccess、およびONFAILURE方法は、これらのメソッドの呼び出しが連鎖させることができないことを意味結果タイプ ユニットを、持っています。 呼び出しが登録された コールバック(同じ将来に登録されたコールバックコールバックの順番は)の実行の順序付けを示唆している可能性があることを示唆しないように、この設計は意図的であることに注意してください(注: )。

A1:彼らは、「ランダム」の順序で実行されます:あなたは

A2を好きなだけコールバックを登録することができます。

+0

私はコールバックがいくつかの別々の実行コンテキストで実行されると仮定しますか? onSuccess(v => Thread.sleep(2000); println( "バスケットの成功サイズ" + v)の場合)でスリープを追加すると、メインプロセスが終了する前に印刷が実行されません。 –

+0

はい、それらは別々のスレッドで実行され、定義されたスレッドプール(あなたのケースではグローバル実行コンテキスト)から取得されます。 – Rumoku

関連する問題