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

Dispatchers क्या हैं

एक dispatcher तय करता है कि असली worker threads के किस pool पर कोई coroutine असल में अपना काम चलाता है।
Syntax
kotlin
launch(Dispatchers.IO) {
    // blocking work such as files or network
}

val result = withContext(Dispatchers.Default) {
    // switch context, then return here
    value
}

What a Dispatcher Does

एक dispatcher नियंत्रित करता है कि coroutine का code किस thread (या thread pool) पर चलता है; अलग dispatchers अलग तरह के काम के लिए tuned हैं, जैसे CPU computation बनाम blocking I/O।

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

उदाहरण: What a Dispatcher Does

markup
import kotlinx.coroutines.*

fun main() {
    runBlocking {
        launch(Dispatchers.Default) {
            println("Running on Dispatchers.Default: ${Thread.currentThread().name}")
        }
    }
}

Dispatchers.IO for Blocking Operations

Dispatchers.IO एक बड़े thread pool द्वारा backed है जो CPU-focused thread pool को खत्म किए बिना कई concurrent blocking operations, जैसे file या network access, को handle करने के लिए डिज़ाइन किया गया है।

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

उदाहरण: Dispatchers.IO for Blocking Operations

markup
import kotlinx.coroutines.*

fun main() = runBlocking {
    withContext(Dispatchers.IO) {
        println("Simulating a blocking IO operation")
    }
    println("Back on the original context")
}

Switching Context with withContext

withContext(dispatcher) { } current coroutine को suspend करता है, दिए गए block को specified dispatcher पर चलाता है, और फिर block के खत्म होने पर उसके result के साथ वापस resume करता है।

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

उदाहरण: Switching Context with withContext

markup
import kotlinx.coroutines.*

suspend fun computeSum(): Int = withContext(Dispatchers.Default) {
    (1..1000).sum()
}

fun main() = runBlocking {
    println("Sum: ${computeSum()}")
}

Dispatchers.Default for CPU Work

Dispatchers.Default CPU cores की संख्या के आधार पर sized है और computationally intensive काम के लिए है, इसे blocking I/O के लिए dispatchers से अलग रखते हुए।

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

उदाहरण: Dispatchers.Default for CPU Work

markup
import kotlinx.coroutines.*

fun main() = runBlocking {
    val result = withContext(Dispatchers.Default) {
        (1..100).map { it * it }.sum()
    }
    println("Sum of squares: $result")
}
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. Dispatchers.Main का उपयोग किसी ऐसे environment (जैसे Android) के बाहर करना जो असल में एक main/UI thread dispatcher implementation प्रदान करता हो।
  2. Dispatchers.Default पर भारी CPU या I/O काम करना जब Dispatchers.IO specifically blocking I/O operations के लिए डिज़ाइन और sized है।
  3. यह भूल जाना कि withContext block के खत्म होने तक suspend करता है, और यह मान लेना कि यह launch की तरह तुरंत return करता है।
चैप्टर सारांश
  • एक dispatcher तय करता है कि किसी coroutine का code असल में किस thread या thread pool पर execute होता है।
  • Dispatchers.Default CPU-intensive काम के लिए optimized है; Dispatchers.IO blocking I/O operations के लिए optimized है।
  • withContext(dispatcher) { } current coroutine को उस block की अवधि के लिए एक अलग dispatcher में बदल देता है, फिर वापस बदल देता है।
  • सही dispatcher चुनना CPU-heavy और I/O-heavy काम को एक-दूसरे के thread pools को भूखा रखने से बचाता है।
🔒

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.