In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use Promise". Friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use Promise.
According to Erik, we can think of Promise as a mailbox / container for loading Future objects.
From its two method signatures, you can put successful data or failed Exception into your mailbox.
Def successful [T] (result: t): promise [T] def failed [T] (exception: Throwable): Promise [T]
After putting the value, you can call future () of Promise to get a completed Future.
The core of Promise is this logic: you can get a Future object through Promise.future (), and the calculation results in future are calculated somewhere else (usually in other threads, callback code, of course).
Val p = Promise [Int] () val f = p.future
Def produce () = Future {Thread.sleep (500) p.success (1) println ("Produce done")}
Def consume () = Future {f.foreach (r = > println (s "Get $r")) println (s "Consume done")}
Produce () consume () StdIn.readLine ("End?\ n")
This code prints something like this, and you can see that the consume () method has been executed before printing "Get 1".
Consume doneEnd?Produce doneGet 1
It's understandable that foreach only provides the callback mechanism for the future Success case. It is important to note that Future can register multiple callback through onComplete and foreach, but there is no guarantee that these callback will run successively and on which thread. This is different from map and flatMap.
I originally wanted to try to use Promise to realize the conversion from list [list [T]] to [list [T]] and find the way of foldLeft. As follows:
Def sequence [T] (fts: list [FutureList [T]): FutureList [T]] = {fts.foldLeft (Future {List.empty [T]}) ((acc, ft) = > acc.flatMap (ts = > ft.map (t = > ts: + t))}
Draw the lily and experience promise again:
Def sequenceByPromise [T] (fts: list [list [T]): FutureList [T]] = {val p = promise [list [T]] () val result = p.success (List.empty [T]).
Fts.foldLeft (result) ((acc, ft) = > acc.flatMap (ts = > ft.map (t = > ts: + t)) result}
The following code demonstrates how to convert callback style code to Future style code. In the Akka actor framework, if the code needs to be executed asynchronously and the subsequent code needs the result of asynchronous execution, we can encapsulate the result into Future through Promise.
Trait CallbackBasedApi {def computeIntAsync (continuation: Try [Int] = > Unit): Unit}
Trait FutureBasedApi {def computeIntAsync (): Future [Int]}
Def futurize (callbackBasedApi: CallbackBasedApi): FutureBasedApi = {val p = Promise [Int] ()
/ / consider "Try= > Unit" as the parameter of complete. Does t = > p.complete (t) have a sense of nesting? :) callbackBasedApi.computeIntAsync (t = > p.complete (t))
New FutureBasedApi {def computeIntAsync () = p.future}} so far, I believe you have a deeper understanding of "how to use Promise". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.