SupervisorJob for independent failure
📖 Scenario: You are building a Kotlin program that launches two independent tasks using coroutines. You want to make sure that if one task fails, the other task continues running without being cancelled.
🎯 Goal: Create a Kotlin program that uses SupervisorJob to launch two coroutines. One coroutine will throw an exception, and the other will complete successfully. The program should show that the failure of one coroutine does not cancel the other.
📋 What You'll Learn
Create a
SupervisorJob instance called supervisorCreate a
CoroutineScope called scope using supervisorLaunch two coroutines inside
scope named job1 and job2job1 should throw an exception after a short delayjob2 should print a success message after a longer delayUse
join() to wait for both coroutines to finishPrint messages to show the independent failure behavior
💡 Why This Matters
🌍 Real World
In real apps, you often want some tasks to continue even if others fail, like loading multiple independent data sources.
💼 Career
Understanding <code>SupervisorJob</code> is important for Kotlin developers working with coroutines to build resilient and fault-tolerant applications.
Progress0 / 4 steps