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

Suspend Functions क्या हैं

एक suspend function वह है जिसे बीच में रुकने और दूसरा काम होने देने की अनुमति है, फिर वहीं से वापस शुरू होने की जहां इसने छोड़ा था।
Syntax
kotlin
suspend fun functionName(): ReturnType {
    delay(milliseconds)      // suspends without blocking the thread
    return value
}

runBlocking { functionName() }   // call from a coroutine

Declaring a Suspend Function

fun से पहले suspend जोड़ना किसी function को उस function के रूप में चिह्नित करता है जो कुछ points पर execution रोक सकता है और बाद में फिर से शुरू हो सकता है, कभी उस thread को block किए बिना जिस पर यह चलता है।

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

उदाहरण: Declaring a Suspend Function

markup
import kotlinx.coroutines.*

suspend fun fetchGreeting(): String {
    delay(50)
    return "Hello from a suspend function"
}

fun main() = runBlocking {
    println(fetchGreeting())
}

Suspend Functions Can Only Be Called from Coroutines

एक suspend function को साधारण code से सीधे call नहीं किया जा सकता -- इसे किसी दूसरे suspend function से या launch या runBlocking जैसे किसी coroutine builder के अंदर से call किया जाना चाहिए।

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

उदाहरण: Suspend Functions Can Only Be Called from Coroutines

markup
import kotlinx.coroutines.*

suspend fun step1(): String {
    delay(20)
    return "step1 done"
}

suspend fun step2(): String {
    delay(20)
    return "step2 done"
}

fun main() = runBlocking {
    println(step1())
    println(step2())
}

delay() vs Thread.sleep()

delay() coroutine को उसके underlying thread को block किए बिना suspend करता है, उस thread को बीच में दूसरा काम करने देते हुए, Thread.sleep() के विपरीत जो पूरे thread को सीधे block कर देता है।

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

उदाहरण: delay() vs Thread.sleep()

markup
import kotlinx.coroutines.*

fun main() = runBlocking {
    launch {
        delay(100)
        println("Task A finished")
    }
    println("Main coroutine continues immediately")
}

Composing Suspend Functions

Suspend functions सीधे दूसरे suspend functions को call कर सकते हैं, आपको sequential asynchronous logic बनाने देते हुए जो बिल्कुल साधारण, synchronous-दिखने वाले code जैसा पढ़ा जाता है।

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

उदाहरण: Composing Suspend Functions

markup
import kotlinx.coroutines.*

suspend fun fetchUser(): String {
    delay(30)
    return "Ana"
}

suspend fun greetUser(): String {
    val name = fetchUser()
    return "Hello, $name!"
}

fun main() = runBlocking {
    println(greetUser())
}
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. किसी suspend function को साधारण, non-suspending code से बिना किसी coroutine builder या किसी दूसरे suspend function के call करने की कोशिश करना।
  2. यह मान लेना कि suspend किसी function को अपने आप एक background thread पर चलाता है; यह सिर्फ चिह्नित करता है कि यह *suspend कर सकता है*, असली threading उपयोग किए dispatcher पर निर्भर करती है।
  3. यह भूल जाना कि suspension points (जैसे delay) असल में सिर्फ execution को रोकते हैं, वे उस underlying thread को block नहीं करते जिस पर coroutine चल रहा है।
चैप्टर सारांश
  • suspend keyword एक ऐसे function को चिह्नित करता है जो अपना execution रोक सकता है और बिना किसी thread को block किए बाद में फिर शुरू हो सकता है।
  • एक suspend function को सिर्फ किसी दूसरे suspend function से या launch/runBlocking जैसे किसी coroutine builder के अंदर से call किया जा सकता है।
  • delay() एक suspend function है जो underlying thread को block किए बिना किसी coroutine को रोकता है, Thread.sleep() के विपरीत।
  • किसी function को suspend चिह्नित करना खुद अपने आप इसके काम को background thread पर नहीं ले जाता।
🔒

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.