-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c03ae22
commit de29809
Showing
2 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
...es/coroutines-crums-group-1/src/main/kotlin/org/jesperancinha/ktd/GlobalScopeCoroutine.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.jesperancinha.ktd | ||
|
||
import kotlinx.coroutines.DelicateCoroutinesApi | ||
import kotlinx.coroutines.GlobalScope | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.coroutines.time.delay | ||
import kotlin.time.Duration.Companion.seconds | ||
import kotlin.time.toJavaDuration | ||
|
||
class GlobalScopeCoroutine { | ||
companion object { | ||
@OptIn(DelicateCoroutinesApi::class) | ||
@JvmStatic | ||
fun main(args: Array<String> = emptyArray()) { | ||
val job = GlobalScope.launch { | ||
delay(1.seconds.toJavaDuration()) | ||
} | ||
println("Global >> Is the Global job cancelled? ${job.isCancelled}") | ||
println("Global >> Is the Global job active? ${job.isActive}") | ||
println("Global >> Is the Global job completed? ${job.isCompleted}") | ||
job.cancel() | ||
println("Global >> Is the Global job cancelled after cancel? ${job.isCancelled}") | ||
runBlocking { | ||
val jobInScope = launch { | ||
delay(1.seconds.toJavaDuration()) | ||
|
||
} | ||
jobInScope.cancel() | ||
println("First Job >> Is this job cancelled? ${jobInScope.isCancelled}") | ||
} | ||
val lastJob = runBlocking { | ||
val jobInScope = launch { | ||
delay(1.seconds.toJavaDuration()) | ||
|
||
} | ||
println("Second Job >> Is this job cancelled? ${jobInScope.isCancelled}") | ||
println("Second Job >> Is this job active? ${jobInScope.isActive}") | ||
println("Second Job >> Is this job completed? ${jobInScope.isCompleted}") | ||
jobInScope | ||
} | ||
println("Second Job After Life-Cycle >> Is this job cancelled? ${lastJob.isCancelled}") | ||
println("Second Job After Life-Cycle >> Is this job active? ${lastJob.isActive}") | ||
println("Second Job After Life-Cycle >> Is this job completed? ${lastJob.isCompleted}") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters