-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scopes.kt
58 lines (45 loc) · 2.5 KB
/
Scopes.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package coroutines
import kotlinx.coroutines.*
import kotlin.coroutines.CoroutineContext
fun main(args: Array<String>) {
val scoped = ScopedCoroutine()
// Run it in a ScopedCoroutine (scoped) --> Coroutine scope active: true
val job1 = runBlocking { scoped.doIt() }
println("Coroutine scope active (job1): ${job1.isActive}")
Thread.sleep(500)
// Run it in a GlobalScope (!= scoped context) --> Global scope active: true
val job2 = runBlocking { scoped.doItGlobally() }
println("Global scope active (job2): ${job2.isActive}")
Thread.sleep(500)
// Run it in a ScopedCoroutine (!= scoped context) --> Other coroutine scope active: true
val job3 = runBlocking { scoped.doItInOtherScope() }
println("Other coroutine scope active (job3): ${job3.isActive}")
Thread.sleep(500)
// Run it in a ScopedCoroutine(scoped context) --> Another(but same!) coroutine scope active: true
val job4 = CoroutineScope(scoped.coroutineContext).launch { doItInAnyScope() }
println("Any(but same!) coroutine scope active (job4): ${job4.isActive}")
Thread.sleep(500)
scoped.coroutineContext.cancel()
println("== Canceling ==")
Thread.sleep(500)
// The ScopedCoroutine scoped is canceled so does job1 --> Coroutine scope active: false
println("Coroutine scope active (job1): ${job1.isActive}")
Thread.sleep(500)
// The ScopedCoroutine scoped is canceled but job2 is not because is a GlobalScope coroutine(!=scoped context) --> Global scope active: true
println("Global scope active (job2): ${job2.isActive}")
Thread.sleep(500)
// The ScopedCoroutine scoped is canceled but job3 is not because is a ScopedCoroutine(!=scoped context) --> Other coroutine scope active: true
println("Other coroutine scope active (job3): ${job3.isActive}")
Thread.sleep(500)
// The ScopedCoroutine scoped is canceled so does job4 because is a ScopedCoroutine(scoped context) --> Another(but same!) coroutine scope active: false
println("Any(but same!) coroutine scope active (job4): ${job4.isActive}")
}
class ScopedCoroutine(): CoroutineScope {
var scopedJob = Job()
override val coroutineContext: CoroutineContext
get() = scopedJob
fun doIt() = launch(Dispatchers.IO) { delay(100000) }
fun doItGlobally() = GlobalScope.launch(Dispatchers.IO) { delay(100000) }
fun doItInOtherScope() = CoroutineScope(Dispatchers.IO).launch { delay(100000) }
}
private suspend fun doItInAnyScope() = coroutineScope { launch(Dispatchers.IO) { delay(100000) } }