Coroutine के Basics
In this page:
import kotlinx.coroutines.*
fun main() = runBlocking {
// coroutine code runs here
}
What Is a Coroutine?
एक coroutine concurrent काम की एक lightweight unit है जिसे किसी underlying operating system thread को block किए बिना रोका और फिर से शुरू किया जा सकता है, कई coroutines को असली threads के एक छोटे pool को share करने देते हुए।
- यह उदाहरण kotlinx.coroutines उपयोग करता है, जो साधारण Kotlin standard library का हिस्सा नहीं है।
- इसे compile और run करने के लिए kotlinx-coroutines-core dependency (Gradle/Maven के ज़रिए जोड़ी गई) चाहिए।
उदाहरण: What Is a Coroutine?
import kotlinx.coroutines.*
fun main() = runBlocking {
println("Starting a coroutine")
launch {
println("Hello from a coroutine!")
}
println("Coroutine launched")
}
Login to try C/C++/Java/PHP code in the editor
runBlocking as an Entry Point
runBlocking { } एक coroutine scope बनाता है और अंदर की हर चीज़ पूरी होने तक calling thread को block करता है, यही वजह है कि इसे आमतौर पर coroutine examples में main() को wrap करने के लिए उपयोग किया जाता है।
runBlocking से बचें; यह main() functions और tests के लिए सबसे उपयुक्त है।- यह उदाहरण kotlinx.coroutines उपयोग करता है, जो साधारण Kotlin standard library का हिस्सा नहीं है।
- इसे compile और run करने के लिए kotlinx-coroutines-core dependency (Gradle/Maven के ज़रिए जोड़ी गई) चाहिए।
उदाहरण: runBlocking as an Entry Point
import kotlinx.coroutines.*
fun main() = runBlocking {
delay(100)
println("Finished after a delay, inside runBlocking")
}
Login to try C/C++/Java/PHP code in the editor
Coroutines Are Not Threads
Threads के विपरीत, जो अपेक्षाकृत महंगे operating system resources हैं, coroutines सस्ते और lightweight हैं -- आप उनमें से दसियों हज़ारों को मुट्ठी भर असली threads पर concurrently चला सकते हैं।
- यह उदाहरण kotlinx.coroutines उपयोग करता है, जो साधारण Kotlin standard library का हिस्सा नहीं है।
- इसे compile और run करने के लिए kotlinx-coroutines-core dependency (Gradle/Maven के ज़रिए जोड़ी गई) चाहिए।
उदाहरण: Coroutines Are Not Threads
import kotlinx.coroutines.*
fun main() = runBlocking {
repeat(5) { i ->
launch {
println("Coroutine $i running")
}
}
}
Login to try C/C++/Java/PHP code in the editor
Why kotlinx.coroutines Is a Library, Not the Standard Library
core coroutine building blocks (suspend functions) Kotlin language का हिस्सा हैं, लेकिन launch, async, और Dispatchers जैसे practical tools अलग kotlinx.coroutines library से आते हैं, जिसे Gradle या Maven के ज़रिए किसी project में जोड़ा जाता है।
- यह उदाहरण kotlinx.coroutines उपयोग करता है, जो साधारण Kotlin standard library का हिस्सा नहीं है।
- इसे compile और run करने के लिए kotlinx-coroutines-core dependency (Gradle/Maven के ज़रिए जोड़ी गई) चाहिए।
उदाहरण: Why kotlinx.coroutines Is a Library, Not the Standard Library
import kotlinx.coroutines.*
fun main() = runBlocking {
println("This program depends on kotlinx-coroutines-core")
}
Login to try C/C++/Java/PHP code in the editor
- किसी coroutine को एक पूरे operating-system thread के साथ confuse करना; हज़ारों coroutines सिर्फ कुछ असली threads पर चल सकते हैं।
- यह भूल जाना कि
kotlinx.coroutinesएक अलग library है, साधारण Kotlin standard library का हिस्सा नहीं, इसलिए इसे एक project dependency के रूप में जोड़ना चाहिए। - examples की आदत से असली application code के अंदर
runBlockingcall करना, जबकि यह current thread को block करता है और वहां coroutines उपयोग करने का मकसद हरा देता है।
- एक coroutine एक lightweight, suspendable computation है, कोई पूरा operating-system thread नहीं।
runBlockingएक coroutine शुरू करता है और इसके पूरा होने तक current thread को block करता है, मुख्यतःmain()functions और tests में उपयोग होता है।kotlinx.coroutineslibrary coroutine support प्रदान करती है और इसे एक dependency के रूप में जोड़ना चाहिए; यह Kotlin की standard library का हिस्सा नहीं है।- Coroutines concurrent code को sequential जैसा दिखाते हैं, गहराई से nested callbacks से बचते हुए।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: