Dispatchers क्या हैं
In this page:
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।
- यह उदाहरण kotlinx.coroutines उपयोग करता है, जो साधारण Kotlin standard library का हिस्सा नहीं है।
- इसे compile और run करने के लिए kotlinx-coroutines-core dependency (Gradle/Maven के ज़रिए जोड़ी गई) चाहिए।
उदाहरण: What a Dispatcher Does
import kotlinx.coroutines.*
fun main() {
runBlocking {
launch(Dispatchers.Default) {
println("Running on Dispatchers.Default: ${Thread.currentThread().name}")
}
}
}
Login to try C/C++/Java/PHP code in the editor
Dispatchers.IO for Blocking Operations
Dispatchers.IO एक बड़े thread pool द्वारा backed है जो CPU-focused thread pool को खत्म किए बिना कई concurrent blocking operations, जैसे file या network access, को handle करने के लिए डिज़ाइन किया गया है।
- यह उदाहरण kotlinx.coroutines उपयोग करता है, जो साधारण Kotlin standard library का हिस्सा नहीं है।
- इसे compile और run करने के लिए kotlinx-coroutines-core dependency (Gradle/Maven के ज़रिए जोड़ी गई) चाहिए।
उदाहरण: Dispatchers.IO for Blocking Operations
import kotlinx.coroutines.*
fun main() = runBlocking {
withContext(Dispatchers.IO) {
println("Simulating a blocking IO operation")
}
println("Back on the original context")
}
Login to try C/C++/Java/PHP code in the editor
Switching Context with withContext
withContext(dispatcher) { } current coroutine को suspend करता है, दिए गए block को specified dispatcher पर चलाता है, और फिर block के खत्म होने पर उसके result के साथ वापस resume करता है।
- यह उदाहरण kotlinx.coroutines उपयोग करता है, जो साधारण Kotlin standard library का हिस्सा नहीं है।
- इसे compile और run करने के लिए kotlinx-coroutines-core dependency (Gradle/Maven के ज़रिए जोड़ी गई) चाहिए।
उदाहरण: Switching Context with withContext
import kotlinx.coroutines.*
suspend fun computeSum(): Int = withContext(Dispatchers.Default) {
(1..1000).sum()
}
fun main() = runBlocking {
println("Sum: ${computeSum()}")
}
Login to try C/C++/Java/PHP code in the editor
Dispatchers.Default for CPU Work
Dispatchers.Default CPU cores की संख्या के आधार पर sized है और computationally intensive काम के लिए है, इसे blocking I/O के लिए dispatchers से अलग रखते हुए।
- यह उदाहरण kotlinx.coroutines उपयोग करता है, जो साधारण Kotlin standard library का हिस्सा नहीं है।
- इसे compile और run करने के लिए kotlinx-coroutines-core dependency (Gradle/Maven के ज़रिए जोड़ी गई) चाहिए।
उदाहरण: Dispatchers.Default for CPU Work
import kotlinx.coroutines.*
fun main() = runBlocking {
val result = withContext(Dispatchers.Default) {
(1..100).map { it * it }.sum()
}
println("Sum of squares: $result")
}
Login to try C/C++/Java/PHP code in the editor
Dispatchers.Mainका उपयोग किसी ऐसे environment (जैसे Android) के बाहर करना जो असल में एक main/UI thread dispatcher implementation प्रदान करता हो।Dispatchers.Defaultपर भारी CPU या I/O काम करना जबDispatchers.IOspecifically blocking I/O operations के लिए डिज़ाइन और sized है।- यह भूल जाना कि
withContextblock के खत्म होने तक suspend करता है, और यह मान लेना कि यहlaunchकी तरह तुरंत return करता है।
- एक dispatcher तय करता है कि किसी coroutine का code असल में किस thread या thread pool पर execute होता है।
Dispatchers.DefaultCPU-intensive काम के लिए optimized है;Dispatchers.IOblocking 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: