launch और async
In this page:
runBlocking {
launch { // fire-and-forget
// work
}
val deferred = async { // returns a result
value
}
val result = deferred.await()
}
Using launch for Fire-and-Forget Work
launch { } side effects के लिए intended एक नया coroutine शुरू करता है, जैसे print करना या state update करना, और उस चल रहे task को represent करने वाला एक Job return करता है, लेकिन कोई computed value नहीं।
- यह उदाहरण kotlinx.coroutines उपयोग करता है, जो साधारण Kotlin standard library का हिस्सा नहीं है।
- इसे compile और run करने के लिए kotlinx-coroutines-core dependency (Gradle/Maven के ज़रिए जोड़ी गई) चाहिए।
उदाहरण: Using launch for Fire-and-Forget Work
import kotlinx.coroutines.*
fun main() = runBlocking {
val job = launch {
println("Doing background work")
}
job.join()
println("Background work complete")
}
Login to try C/C++/Java/PHP code in the editor
Using async for a Result
async { } एक coroutine शुरू करता है जो एक value compute और return करता है, Deferred<T> में wrapped; इस पर .await() call करना result तैयार होने तक suspend करता है और फिर उसे return करता है।
- यह उदाहरण kotlinx.coroutines उपयोग करता है, जो साधारण Kotlin standard library का हिस्सा नहीं है।
- इसे compile और run करने के लिए kotlinx-coroutines-core dependency (Gradle/Maven के ज़रिए जोड़ी गई) चाहिए।
उदाहरण: Using async for a Result
import kotlinx.coroutines.*
fun main() = runBlocking {
val deferred = async {
delay(50)
42
}
println("Result: ${deferred.await()}")
}
Login to try C/C++/Java/PHP code in the editor
Running Multiple async Tasks Concurrently
किसी को भी await करने से पहले कई async calls शुरू करना उन सभी coroutines को concurrently चलने देता है, और total time सभी को जोड़ने के बजाय सबसे लंबे single task के करीब होता है।
- यह उदाहरण kotlinx.coroutines उपयोग करता है, जो साधारण Kotlin standard library का हिस्सा नहीं है।
- इसे compile और run करने के लिए kotlinx-coroutines-core dependency (Gradle/Maven के ज़रिए जोड़ी गई) चाहिए।
उदाहरण: Running Multiple async Tasks Concurrently
import kotlinx.coroutines.*
fun main() = runBlocking {
val first = async { delay(100); 10 }
val second = async { delay(100); 20 }
val total = first.await() + second.await()
println("Total: $total")
}
Login to try C/C++/Java/PHP code in the editor
Choosing Between launch and async
launch का उपयोग करें जब आपको coroutine से result वापस न चाहिए, और async का जब चाहिए -- async का सहारा लेना और कभी .await() call न करना बर्बाद हुई concurrency का एक आम स्रोत है।
- यह उदाहरण kotlinx.coroutines उपयोग करता है, जो साधारण Kotlin standard library का हिस्सा नहीं है।
- इसे compile और run करने के लिए kotlinx-coroutines-core dependency (Gradle/Maven के ज़रिए जोड़ी गई) चाहिए।
उदाहरण: Choosing Between launch and async
import kotlinx.coroutines.*
fun main() = runBlocking {
launch { println("Just logging, no result needed") }
val value = async { 100 }.await()
println("Computed value: $value")
}
Login to try C/C++/Java/PHP code in the editor
asyncसे मिले किसीDeferredपर.await()call करना भूल जाना, यानी आप असल में कभी इसका computed result नहीं निकालते।launchसे काम चल सकता हो तबasyncउपयोग करना, जब असल में कोई return value न चाहिए तब अनावश्यक overhead जोड़ते हुए।- कई
asynccalls शुरू करना लेकिन उन सभी को शुरू करने के बाद के बजाय हर एक पर तुरंत.await()call करना, उन्हें concurrently चलाने का फायदा खोते हुए।
launch { }एक coroutine शुरू करता है जो अपने side effects के लिए चलता है और एकJobreturn करता है, कोई result value नहीं।async { }एक coroutine शुरू करता है जो एक result compute करता है,Deferred<T>के रूप में return किया गया, बाद में.await()से निकाला गया।- किसी को भी await करने से पहले कई
asynccalls शुरू करना उन्हें क्रमवार के बजाय concurrently चलने देता है। launchऔरasyncदोनोंkotlinx.coroutinesद्वारा प्रदान किए coroutine builders हैं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: