Skip to content

Instantly share code, notes, and snippets.

@karanahuja-android
Created April 14, 2025 13:08
Show Gist options
  • Save karanahuja-android/d85913434bde84f926fa098b8b788dc5 to your computer and use it in GitHub Desktop.
Save karanahuja-android/d85913434bde84f926fa098b8b788dc5 to your computer and use it in GitHub Desktop.
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