Created
April 14, 2025 13:08
-
-
Save karanahuja-android/d85913434bde84f926fa098b8b788dc5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlinx.coroutines.* | |
fun main() = runBlocking { | |
val job = launch { | |
try { | |
repeat(1000) { i -> | |
println("Job: I'm working $i...") | |
delay(500L) | |
println("at end of try") | |
} | |
} finally { | |
println("Job: I'm running cleanup tasks") | |
} | |
} | |
delay(1300L) // Let the coroutine work for a while | |
println("Main: I'm tired of waiting!") | |
job.cancel() // Cancels the job | |
job.join() // Waits for job's completion | |
println("Main: Now I can quit.") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment