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

Coroutine के Basics

एक coroutine एक lightweight task है जिसे Kotlin बाद में रोक और फिर से शुरू कर सकता है, इसके इंतज़ार करते समय आपके program को दूसरी चीज़ें करने देते हुए।
Syntax
kotlin
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 करने देते हुए।

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

उदाहरण: What Is a Coroutine?

markup
import kotlinx.coroutines.*

fun main() = runBlocking {
    println("Starting a coroutine")
    launch {
        println("Hello from a coroutine!")
    }
    println("Coroutine launched")
}

runBlocking as an Entry Point

runBlocking { } एक coroutine scope बनाता है और अंदर की हर चीज़ पूरी होने तक calling thread को block करता है, यही वजह है कि इसे आमतौर पर coroutine examples में main() को wrap करने के लिए उपयोग किया जाता है।

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

उदाहरण: runBlocking as an Entry Point

markup
import kotlinx.coroutines.*

fun main() = runBlocking {
    delay(100)
    println("Finished after a delay, inside runBlocking")
}

Coroutines Are Not Threads

Threads के विपरीत, जो अपेक्षाकृत महंगे operating system resources हैं, coroutines सस्ते और lightweight हैं -- आप उनमें से दसियों हज़ारों को मुट्ठी भर असली threads पर concurrently चला सकते हैं।

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

उदाहरण: Coroutines Are Not Threads

markup
import kotlinx.coroutines.*

fun main() = runBlocking {
    repeat(5) { i ->
        launch {
            println("Coroutine $i running")
        }
    }
}

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 में जोड़ा जाता है।

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

उदाहरण: Why kotlinx.coroutines Is a Library, Not the Standard Library

markup
import kotlinx.coroutines.*

fun main() = runBlocking {
    println("This program depends on kotlinx-coroutines-core")
}
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. किसी coroutine को एक पूरे operating-system thread के साथ confuse करना; हज़ारों coroutines सिर्फ कुछ असली threads पर चल सकते हैं।
  2. यह भूल जाना कि kotlinx.coroutines एक अलग library है, साधारण Kotlin standard library का हिस्सा नहीं, इसलिए इसे एक project dependency के रूप में जोड़ना चाहिए।
  3. examples की आदत से असली application code के अंदर runBlocking call करना, जबकि यह current thread को block करता है और वहां coroutines उपयोग करने का मकसद हरा देता है।
चैप्टर सारांश
  • एक coroutine एक lightweight, suspendable computation है, कोई पूरा operating-system thread नहीं।
  • runBlocking एक coroutine शुरू करता है और इसके पूरा होने तक current thread को block करता है, मुख्यतः main() functions और tests में उपयोग होता है।
  • kotlinx.coroutines library 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:

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.