あなたが理解する必要があるものは、... Future
またはFutures
から値を取得しようとしないでください。
土地はFuturistic
です。
val futureList = List(
Future(1),
Future(2),
Future(throw new Exception("error"))
)
// addd 1 to futures
// map will propagate errors to transformed futures
// only successful futures will result in +1, rest will stay with errors
val tranformedFutureList = futureList
.map(future => future.map(i => i + 1))
// print values of futures
// simimlar to map... for each will work only with successful futures
val unitFutureList = futureList
.map(future => future.foreach(i => println(i)))
// now lets give you sum of your "future" values
val sumFuture = futureList
.foldLeft(Future(0))((facc, f) => f.onComplete({
case Success(i) => facc.map(acc => acc + i)
case Failure(ex) => facc
})
、以来、OP(@Manuチャンダは)私はPromise
はScalaであるかについて、いくつかのビットを付加しています、Promise
から値を「取得」について尋ねました。
まず、Future
について考えてみましょう。Scala
です。
Future[Int]
が表示されている場合は、Int
と表示されているan ongoing computation
と考えてみてください。計算はsuccessfully complete
となり、結果はSuccess[Int]
またはthrow an exception
となり、結果はFailure[Throwable]
となります。そして、あなたはonComplete
、recoverWith
、onFailure
のような計算を話すようなような機能を見ます。
val intFuture = Future {
// all this inside Future {} is going to run in some other thread
val i = 5;
val j = i + 10;
val k = j/5;
k
}
今は... Promise
とは何ですか?
名前が示すように... Promise[Int]
は、Int
という価値のものです。それ以上のものはありません。
親が子供に特定のおもちゃを約束したときと同じです。この場合、親が必ずしもそのおもちゃを手に入れ始めたわけではないことに注意してください。
約束を完了するためには、まずそれを完了するための作業を開始しなければなりません。市場に出...買い物をして...帰宅してください。忙しいので...他の人がそのおもちゃを親に持参しようとする(彼はそれを買うことができないかもしれない)、そして彼らは何でも彼らは彼から得た結果。
だから... Promise
は、Future
を内部にラップします。そして、"wrapped" Future
"値"はPromise
の値と考えることができます。そう
...
println("Well... The program wants an 'Int' toy")
// we "promised" our program that we will give it that int "toy"
val intPromise = Promise[Int]()
// now we can just move on with or life
println("Well... We just promised an 'Int' toy")
// while the program can make plans with how will it play with that "future toy"
val intFuture = intPromise.future
val plusOneIntFuture = intFuture.map(i => i + 1)
plusOneIntFuture.onComplete({
case Success(i) => println("Wow... I got the toy and modified it to - " + i)
case Failure(ex) => println("I did not get they toy")
})
// but since we at least want to try to complete our promise
println("Now... I suppose we need to get that 'Int' toy")
println("But... I am busy... I can not stop everything else for that toy")
println("ok... lets ask another thread to get that")
val getThatIntFuture = Future {
println("Well... I am thread 2... trying to get the int")
val i = 1
println("Well... I am thread 2... lets just return this i = 1 thingy")
i
}
// now lets complete our promise with whatever we will get from this other thread
getThatIntFuture.onComplete(intTry => intPromise.complete(intTry))
上記のコードは次のような出力になります、
Well... The program wants an 'Int' toy
Well... We just promised an 'Int' toy
Now... I suppose we need to get that 'Int' toy
But... I am busy... I can not stop everything else for that toy
Well... I am thread 2... trying to get the int
Well... I am thread 2... lets just return this i = 1 thingy
Wow... I got the toy and modified it to - 2
Promise
はFuture
から "取得" 値のお手伝いをしていません。非同期プロセス(またはScalaのFuture
)は別のファイルで実行されていますtimeline
...タイムラインをプロセスのタイムラインに合わせなければ、タイムラインで「価値」を「得る」ことはできません。
あなたは手を差し伸べなければならないいくつの先物を知っていますか? –
いいえ、サイズは限られています。しかし、私はf1、f2のような個別の値ではなく、List [Future]を使用したいと考えています。 –
次のようなものです:http://stackoverflow.com/questions/20874186/scala-listfuture-to-futurelist-disregarding-failed -futures?rq = 1 –