Skip to content

Commit

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

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.async
import kotlinx.coroutines.cancel
import kotlinx.coroutines.cancelChildren
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlin.concurrent.thread
import kotlin.coroutines.ContinuationInterceptor
import kotlin.coroutines.EmptyCoroutineContext

fun main() = runBlocking<Unit> {
val scope = CoroutineScope(Dispatchers.IO)
var innerJob: Job? = null
var innerScope: CoroutineScope? = null
val outerJob = scope.launch(Dispatchers.Default) {
innerJob = coroutineContext[Job]
innerScope = this
launch {

}
}
outerJob.cancel()

scope.async {

}
println("outerJob: $outerJob")
println("innerJob: $innerJob")
println("outerJob === innerJob: ${outerJob === innerJob}")
println("outerJob === innerScope: ${outerJob === innerScope}")


val scope2 = CoroutineScope(EmptyCoroutineContext)
val job = scope2.launch {
println("EmptyCoroutineContext: ${coroutineContext[ContinuationInterceptor]}")
}

job.isActive
job.isCancelled
job.isCompleted
job.parent
job.children

job.join()
job.cancel()
job.cancelChildren()

val job2 = scope2.launch(start = CoroutineStart.LAZY) {
println("Lazy start")
}
job2.start()
}

0 comments on commit 4407119

Please sign in to comment.