← Back to Kotlin Course | Chapter 12: Coroutines | Lesson 2 of 7

launch और async

launch एक background task शुरू करता है जिसका जवाब आपको वापस नहीं चाहिए, जबकि async एक ऐसा शुरू करता है जहां आप बाद में इसका result इकट्ठा करने वापस आएंगे।
Syntax
kotlin
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 नहीं।

Warning:
  • यह उदाहरण kotlinx.coroutines उपयोग करता है, जो साधारण Kotlin standard library का हिस्सा नहीं है।
  • इसे compile और run करने के लिए kotlinx-coroutines-core dependency (Gradle/Maven के ज़रिए जोड़ी गई) चाहिए।

उदाहरण: Using launch for Fire-and-Forget Work

markup
import kotlinx.coroutines.*

fun main() = runBlocking {
    val job = launch {
        println("Doing background work")
    }
    job.join()
    println("Background work complete")
}

Using async for a Result

async { } एक coroutine शुरू करता है जो एक value compute और return करता है, Deferred<T> में wrapped; इस पर .await() call करना result तैयार होने तक suspend करता है और फिर उसे return करता है।

Warning:
  • यह उदाहरण kotlinx.coroutines उपयोग करता है, जो साधारण Kotlin standard library का हिस्सा नहीं है।
  • इसे compile और run करने के लिए kotlinx-coroutines-core dependency (Gradle/Maven के ज़रिए जोड़ी गई) चाहिए।

उदाहरण: Using async for a Result

markup
import kotlinx.coroutines.*

fun main() = runBlocking {
    val deferred = async {
        delay(50)
        42
    }
    println("Result: ${deferred.await()}")
}

Running Multiple async Tasks Concurrently

किसी को भी await करने से पहले कई async calls शुरू करना उन सभी coroutines को concurrently चलने देता है, और total time सभी को जोड़ने के बजाय सबसे लंबे single task के करीब होता है।

Warning:
  • यह उदाहरण kotlinx.coroutines उपयोग करता है, जो साधारण Kotlin standard library का हिस्सा नहीं है।
  • इसे compile और run करने के लिए kotlinx-coroutines-core dependency (Gradle/Maven के ज़रिए जोड़ी गई) चाहिए।

उदाहरण: Running Multiple async Tasks Concurrently

markup
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")
}

Choosing Between launch and async

launch का उपयोग करें जब आपको coroutine से result वापस न चाहिए, और async का जब चाहिए -- async का सहारा लेना और कभी .await() call न करना बर्बाद हुई concurrency का एक आम स्रोत है।

Warning:
  • यह उदाहरण kotlinx.coroutines उपयोग करता है, जो साधारण Kotlin standard library का हिस्सा नहीं है।
  • इसे compile और run करने के लिए kotlinx-coroutines-core dependency (Gradle/Maven के ज़रिए जोड़ी गई) चाहिए।

उदाहरण: Choosing Between launch and async

markup
import kotlinx.coroutines.*

fun main() = runBlocking {
    launch { println("Just logging, no result needed") }
    val value = async { 100 }.await()
    println("Computed value: $value")
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. async से मिले किसी Deferred पर .await() call करना भूल जाना, यानी आप असल में कभी इसका computed result नहीं निकालते।
  2. launch से काम चल सकता हो तब async उपयोग करना, जब असल में कोई return value न चाहिए तब अनावश्यक overhead जोड़ते हुए।
  3. कई async calls शुरू करना लेकिन उन सभी को शुरू करने के बाद के बजाय हर एक पर तुरंत .await() call करना, उन्हें concurrently चलाने का फायदा खोते हुए।
चैप्टर सारांश
  • launch { } एक coroutine शुरू करता है जो अपने side effects के लिए चलता है और एक Job return करता है, कोई result value नहीं।
  • async { } एक coroutine शुरू करता है जो एक result compute करता है, Deferred<T> के रूप में return किया गया, बाद में .await() से निकाला गया।
  • किसी को भी await करने से पहले कई async calls शुरू करना उन्हें क्रमवार के बजाय concurrently चलने देता है।
  • launch और async दोनों kotlinx.coroutines द्वारा प्रदान किए coroutine builders हैं।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.