Skip to content

Commit 5c5e97e

Browse files
Add playground files for Structured concurrency
1 parent aa1ac68 commit 5c5e97e

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.lukaslechner.coroutineusecasesonandroid.playground.structuredconcurrency
2+
3+
import kotlinx.coroutines.*
4+
5+
val scope = CoroutineScope(Dispatchers.Default)
6+
7+
fun main() = runBlocking<Unit> {
8+
9+
val job = scope.launch {
10+
delay(100)
11+
println("Coroutine completed")
12+
}
13+
14+
job.invokeOnCompletion { throwable ->
15+
if (throwable is CancellationException) {
16+
println("Coroutine was cancelled")
17+
}
18+
}
19+
20+
delay(50)
21+
onDestroy()
22+
}
23+
24+
fun onDestroy() {
25+
println("life-time of scope ends")
26+
scope.cancel()
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.lukaslechner.coroutineusecasesonandroid.playground.structuredconcurrency
2+
3+
import kotlinx.coroutines.*
4+
5+
fun main() {
6+
7+
val scopeJob = Job()
8+
val scope = CoroutineScope(Dispatchers.Default + scopeJob)
9+
10+
val passedJob = Job()
11+
val coroutineJob = scope.launch(passedJob) {
12+
println("Starting coroutine")
13+
delay(1000)
14+
}
15+
16+
println("passedJob and coroutineJob are references to the same job object: ${passedJob === coroutineJob}")
17+
18+
println("Is coroutineJob a child of scopeJob? =>${scopeJob.children.contains(coroutineJob)}")
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.lukaslechner.coroutineusecasesonandroid.playground.structuredconcurrency
2+
3+
import kotlinx.coroutines.*
4+
5+
fun main() = runBlocking<Unit> {
6+
7+
val scope = CoroutineScope(Dispatchers.Default)
8+
9+
val parentCoroutineJob = scope.launch {
10+
launch {
11+
delay(1000)
12+
println("Child Coroutine 1 has completed!")
13+
}
14+
launch {
15+
delay(1000)
16+
println("Child Coroutine 2 has completed!")
17+
}
18+
}
19+
20+
parentCoroutineJob.join()
21+
println("Parent Coroutine has completed!")
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.lukaslechner.coroutineusecasesonandroid.playground.structuredconcurrency
2+
3+
import kotlinx.coroutines.*
4+
5+
fun main() = runBlocking<Unit> {
6+
7+
val scope = CoroutineScope(Dispatchers.Default)
8+
9+
scope.coroutineContext[Job]!!.invokeOnCompletion { throwable ->
10+
if (throwable is CancellationException) {
11+
println("Parent job was cancelled")
12+
}
13+
}
14+
15+
val childCoroutine1Job = scope.launch {
16+
delay(1000)
17+
println("Coroutine 1 completed")
18+
}
19+
childCoroutine1Job.invokeOnCompletion { throwable ->
20+
if (throwable is CancellationException) {
21+
println("Coroutine 1 was cancelled!")
22+
}
23+
}
24+
25+
scope.launch {
26+
delay(1000)
27+
println("Coroutine 2 completed")
28+
}.invokeOnCompletion { throwable ->
29+
if (throwable is CancellationException) {
30+
println("Coroutine 2 was cancelled!")
31+
}
32+
}
33+
34+
delay(200)
35+
36+
childCoroutine1Job.cancelAndJoin()
37+
38+
// scope.coroutineContext[Job]!!.cancelAndJoin()
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.lukaslechner.coroutineusecasesonandroid.playground.structuredconcurrency
2+
3+
import kotlinx.coroutines.GlobalScope
4+
import kotlinx.coroutines.Job
5+
import kotlinx.coroutines.launch
6+
7+
fun main() {
8+
9+
println("Job of GlobalScope: ${GlobalScope.coroutineContext[Job]}")
10+
11+
GlobalScope.launch {
12+
13+
}
14+
15+
}

0 commit comments

Comments
 (0)