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

Structured Concurrency क्या है

Structured concurrency का मतलब है कि हर background task एक parent scope से बंधा है, ताकि parent के खत्म होने पर कुछ भी गलती से चलता हुआ न छूटे।
Syntax
kotlin
runBlocking {
    launch { /* child 1 */ }
    launch { /* child 2 */ }
    coroutineScope {
        launch { /* grouped work */ }
    }   // parent waits for all children
}

Parents Wait for Their Children

एक coroutine scope, जैसे runBlocking से बनाया गया, तब तक पूरा नहीं होता जब तक इसके अंदर launch किया हर coroutine भी खत्म न हो जाए, child task lifetimes को उनके parent से बांधते हुए।

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

उदाहरण: Parents Wait for Their Children

markup
import kotlinx.coroutines.*

fun main() = runBlocking {
    launch {
        delay(50)
        println("Child coroutine finished")
    }
    println("Parent reached its last line, but will wait for the child")
}

coroutineScope Groups Related Work

coroutineScope { } coroutines शुरू करने के लिए एक नया scope बनाता है जो calling coroutine के context से बंधा है, और यह तब तक suspend करता है जब तक इसके अंदर शुरू किया हर coroutine पूरा न हो जाए।

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

उदाहरण: coroutineScope Groups Related Work

markup
import kotlinx.coroutines.*

suspend fun loadData() = coroutineScope {
    launch {
        delay(30)
        println("Loaded part 1")
    }
    launch {
        delay(30)
        println("Loaded part 2")
    }
}

fun main() = runBlocking {
    loadData()
    println("All data loaded")
}

Cancellation Propagates to Children

किसी parent job को cancel करना अपने आप इसके तहत launch किया हर child coroutine को cancel कर देता है, यह सुनिश्चित करते हुए कि संबंधित काम parent के result की परवाह करना बंद करने के बाद बेवजह चलता नहीं रहता।

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

उदाहरण: Cancellation Propagates to Children

markup
import kotlinx.coroutines.*

fun main() = runBlocking {
    val job = launch {
        launch {
            delay(1000)
            println("This should not print")
        }
        delay(20)
    }
    delay(50)
    job.cancelAndJoin()
    println("Parent and child both cancelled")
}

Why Structured Concurrency Matters

हर coroutine के lifetime को एक अच्छी तरह परिभाषित scope से बांधकर, structured concurrency उस classic समस्या से बचता है जहां background tasks चुपचाप उस operation से ज़्यादा जीते हैं जिसने उन्हें शुरू किया, जो unstructured async code में सूक्ष्म bugs का एक आम स्रोत है।

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

उदाहरण: Why Structured Concurrency Matters

markup
import kotlinx.coroutines.*

suspend fun fetchBoth(): Pair<Int, Int> = coroutineScope {
    val a = async { delay(20); 10 }
    val b = async { delay(20); 20 }
    Pair(a.await(), b.await())
}

fun main() = runBlocking {
    val (x, y) = fetchBoth()
    println("x=$x, y=$y")
}
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. आदत से coroutines को एक global, unscoped context (जैसे GlobalScope) से launch करना, structured concurrency द्वारा दिए automatic cancellation और lifecycle management को खोते हुए।
  2. यह मान लेना कि कोई parent coroutine अपनी body की आखिरी line चलते ही खत्म हो जाता है, यह भूलते हुए कि यह असल में अपने सभी child coroutines के पहले पूरा होने का इंतज़ार करता है।
  3. exceptions को व्यापक रूप से catch करते समय CancellationException को specially handle न करना, जो गलती से एक legitimate coroutine cancellation को निगल सकता है।
चैप्टर सारांश
  • Structured concurrency में, हर coroutine एक scope के अंदर launch होता है, और कोई parent coroutine तब तक पूरा नहीं होता जब तक इसके सभी children भी न हो जाएं।
  • किसी parent coroutine को cancel करना अपने आप इसके सभी child coroutines को cancel कर देता है।
  • coroutineScope { } अपने calling context से बंधा एक नया scope बनाता है, इसके अंदर launch किए सभी coroutines के खत्म होने का इंतज़ार करते हुए।
  • Structured concurrency उन leaked coroutines को रोकता है जो अपने logical parent के पहले ही खत्म होने के बाद भी अप्रत्याशित रूप से चलते रहते हैं।
🔒

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.