Skip to content

Commit

Permalink
NonCancellable
Browse files Browse the repository at this point in the history
  • Loading branch information
Louis-XWB committed Sep 18, 2024
1 parent 6e85c8d commit 4c8d0ad
Showing 1 changed file with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.novar.supercorouutine._2_structured_concurrency

import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import kotlin.coroutines.EmptyCoroutineContext

fun main() = runBlocking<Unit> {
val scope = CoroutineScope(EmptyCoroutineContext)
var childJob: Job? = null
var childJob2: Job? = null
val newParent = Job()
val parentJob = scope.launch {
childJob = launch(NonCancellable) {
println("Child started")
delay(1000)
println("Child stopped")
}
println("childJob parent: ${childJob?.parent}")
childJob2 = launch(newParent) {
println("Child started")
writeInfo()
launch(NonCancellable) {
// Log
}
if (!isActive) {
withContext(NonCancellable) {
// Write to database (Room)
delay(1000)
}
throw CancellationException()
}
try {
delay(3000)
} catch (e: CancellationException) {

throw e
}
println("Child 2 started")
delay(3000)
println("Child 2 stopped")
}
println("Parent started")
delay(3000)
println("Parent stopped")
}
delay(1500)
newParent.cancel()
delay(10000)
}

suspend fun writeInfo() = withContext(Dispatchers.IO + NonCancellable) {
// write to file
// read from database (Room)
// write data to file
}

suspend fun uselessSuspendFun() {
Thread.sleep(1000)
}

0 comments on commit 4c8d0ad

Please sign in to comment.