Skip to content

Canceling the parent and child job

Devrath edited this page Aug 29, 2022 · 8 revisions
Contents
Happy scenario: Completing a job for execution
Canceling a job
Exception handling in a job
Exception handling when 2 Jobs are running under a normal parent job and an exception is thrown in one of the child jobs
Exception handling when 2 Jobs are running under a supervisor parent job and an exception is thrown in one of child jobs

Happy scenario: Completing a job for execution

class MainActivity : AppCompatActivity() {

    val parentScope = CoroutineScope(Job() + Dispatchers.Default)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        Log.d("hello", "Starting point")

        val job1 = parentScope.launch {
            Log.d("hello", "Starting JOB-1")
            delay(2000)
            Log.d("hello", "Ending JOB-1")
        }.invokeOnCompletion {
            if(it is CancellationException){
                Log.d("hello", "Canceled JOB-1")
            }else{
                Log.d("hello", "Completed JOB-1")
            }
        }

    }
}

Output

2022-08-28 21:36:07.783 15604-15604/com.droid.test D/hello: Starting point
2022-08-28 21:36:07.817 15604-15643/com.droid.test D/hello: Starting JOB-1
2022-08-28 21:36:09.831 15604-15643/com.droid.test D/hello: Ending JOB-1
2022-08-28 21:36:09.832 15604-15643/com.droid.test D/hello: Completed JOB-1

Cancelling a job

class MainActivity : AppCompatActivity() {

    val parentScope = CoroutineScope(Job() + Dispatchers.Default)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        Log.d("hello", "Starting point")

        val job1 = parentScope.launch {
            Log.d("hello", "Starting JOB-1")
            delay(2000)
            cancel("Canceling the co-routine")
            delay(2000)
            Log.d("hello", "Ending JOB-1")
        }.invokeOnCompletion {
            if(it is CancellationException){
                Log.d("hello", "Canceled JOB-1: ${it.message}")
            }else{
                Log.d("hello", "Completed JOB-1")
            }
        }

    }
}

Output

2022-08-28 21:39:58.005 15744-15744/com.droid.test D/hello: Starting point
2022-08-28 21:39:58.024 15744-15778/com.droid.test D/hello: Starting JOB-1
2022-08-28 21:40:00.033 15744-15778/com.droid.test D/hello: Canceled JOB-1: Canceling the co-routine

Exception handling in a job

class MainActivity : AppCompatActivity() {

    val exceptionHandler = CoroutineExceptionHandler { coroutineContext, throwable ->
        Log.d("hello", "Caught Exception:-> ${throwable.message}")
    }

    val parentScope = CoroutineScope(Job() + Dispatchers.Default + exceptionHandler)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        Log.d("hello", "Starting point")

        val job1 = parentScope.launch {
            Log.d("hello", "Starting JOB-1")
            delay(2000)
            throw RuntimeException("Custom Runtime Exception")
            Log.d("hello", "Ending JOB-1")
        }.invokeOnCompletion {
            if(it is CancellationException){
                Log.d("hello", "Canceled JOB-1: ${it.message}")
            }else{
                Log.d("hello", "Completed JOB-1")
            }
        }

    }
}

Output

2022-08-28 21:59:48.263 16084-16084/com.droid.test D/hello: Starting point
2022-08-28 21:59:48.277 16084-16118/com.droid.test D/hello: Starting JOB-1
2022-08-28 21:59:50.285 16084-16118/com.droid.test D/hello: Caught Exception:-> Custom Runtime Exception
2022-08-28 21:59:50.285 16084-16118/com.droid.test D/hello: Completed JOB-1

Exception handling when 2 Jobs are running under a normal parent job and exception is thrown in one of child jobs

class MainActivity : AppCompatActivity() {

    val exceptionHandler = CoroutineExceptionHandler { coroutineContext, throwable ->
        Log.d("hello", "Caught Exception:-> ${throwable.message}")
    }

    val parentScope = CoroutineScope(Job() + Dispatchers.Default + exceptionHandler)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        Log.d("hello", "Starting point")

        val job1 = parentScope.launch {
            Log.d("hello", "Starting JOB-1")
            delay(1000)
            throw RuntimeException("Custom Runtime Exception")
            Log.d("hello", "Ending JOB-1")
        }.invokeOnCompletion {
            if(it is CancellationException){
                Log.d("hello", "Canceled JOB-1: ${it.message}")
            }else{
                Log.d("hello", "Completed JOB-1")
            }
        }


        val job2 = parentScope.launch {
            Log.d("hello", "Starting JOB-2")
            delay(2000)
            Log.d("hello", "Ending JOB-2")
        }.invokeOnCompletion {
            if(it is CancellationException){
                Log.d("hello", "Canceled JOB-2: ${it.message}")
            }else{
                Log.d("hello", "Completed JOB-2")
            }
        }

    }
}

Output

2022-08-29 10:02:46.269 18356-18356/com.droid.test D/hello: Starting point
2022-08-29 10:02:46.330 18356-18407/com.droid.test D/hello: Starting JOB-1
2022-08-29 10:02:46.333 18356-18408/com.droid.test D/hello: Starting JOB-2
2022-08-29 10:02:47.350 18356-18407/com.droid.test D/hello: Canceled JOB-2: Parent job is Cancelling
2022-08-29 10:02:47.350 18356-18408/com.droid.test D/hello: Caught Exception:-> Custom Runtime Exception
2022-08-29 10:02:47.350 18356-18408/com.droid.test D/hello: Completed JOB-1

Exception handling when 2 Jobs are running under a supervisor parent job and an exception is thrown in one of child jobs

class MainActivity : AppCompatActivity() {

    val exceptionHandler = CoroutineExceptionHandler { coroutineContext, throwable ->
        Log.d("hello", "Caught Exception:-> ${throwable.message}")
    }

    val parentScope = CoroutineScope(SupervisorJob() + Dispatchers.Default + exceptionHandler)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        Log.d("hello", "Starting point")

        val job1 = parentScope.launch {
            Log.d("hello", "Starting JOB-1")
            delay(1000)
            throw RuntimeException("Custom Runtime Exception")
            Log.d("hello", "Ending JOB-1")
        }.invokeOnCompletion {
            if(it is CancellationException){
                Log.d("hello", "Canceled JOB-1: ${it.message}")
            }else{
                Log.d("hello", "Completed JOB-1")
            }
        }


        val job2 = parentScope.launch {
            Log.d("hello", "Starting JOB-2")
            delay(2000)
            Log.d("hello", "Ending JOB-2")
        }.invokeOnCompletion {
            if(it is CancellationException){
                Log.d("hello", "Canceled JOB-2: ${it.message}")
            }else{
                Log.d("hello", "Completed JOB-2")
            }
        }

    }
}

Output

2022-08-29 10:51:55.088 18709-18709/com.droid.test D/hello: Starting point
2022-08-29 10:51:55.161 18709-18759/com.droid.test D/hello: Starting JOB-1
2022-08-29 10:51:55.174 18709-18760/com.droid.test D/hello: Starting JOB-2
2022-08-29 10:51:56.185 18709-18759/com.droid.test D/hello: Caught Exception:-> Custom Runtime Exception
2022-08-29 10:51:56.185 18709-18759/com.droid.test D/hello: Completed JOB-1
2022-08-29 10:51:57.176 18709-18759/com.droid.test D/hello: Ending JOB-2
2022-08-29 10:51:57.177 18709-18759/com.droid.test D/hello: Completed JOB-2
Clone this wiki locally