In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-22 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
Original http://www.cnblogs.com/cjsblog/p/8406401.html
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.