launch/join और async/await के बीच क्या अंतर है Kotlin coroutines में?
सभी को नमस्ते! मैंने Kotlin में coroutines सीखना शुरू किया और दो approaches में उलझ गया: launch/join और async/await। दोनों ही tasks को parallel में चलाने और उनके पूरा होने का इंतजार करने की अनुमति देते हैं, लेकिन मुझे समझ नहीं आ रहा कि कब क्या use करना चाहिए।
मेरे पास एक सरल example है: दो files download करके उन्हें merge करना है। मैंने दोनों तरीके आजमाए, लेकिन results अलग-अलग आए।
पहला तरीका launch/join के साथ:
```kotlin
fun main() = runBlocking {
var result1 = ""
var result2 = ""
val job1 = launch {
delay(1000)
result1 = "File1 content"
}
val job2 = launch {
delay(1000)
result2 = "File2 content"
}
job1.join()
job2.join()
println("$result1 $result2")
}
```
दूसरा तरीका async/await के साथ:
```kotlin
fun main() = runBlocking {
val deferred1 = async {
delay(1000)
"File1 content"
}
val deferred2 = async {
delay(1000)
"File2 content"
}
println("${deferred1.await()} ${deferred2.await()}")
}
```