File tree Expand file tree Collapse file tree 3 files changed +69
-0
lines changed
app/src/main/java/com/lukaslechner/coroutineusecasesonandroid/playground/structuredconcurrency Expand file tree Collapse file tree 3 files changed +69
-0
lines changed Original file line number Diff line number Diff line change 1+ package com.lukaslechner.coroutineusecasesonandroid.playground.structuredconcurrency
2+
3+ import kotlinx.coroutines.*
4+
5+ fun main () {
6+
7+ val exceptionHandler = CoroutineExceptionHandler { context, exception ->
8+ println (" Caught exception $exception " )
9+ }
10+
11+ val scope = CoroutineScope (SupervisorJob () + exceptionHandler)
12+
13+ scope.launch {
14+ println (" Coroutine 1 starts" )
15+ delay(50 )
16+ println (" Coroutine 1 fails" )
17+ throw RuntimeException ()
18+ }
19+
20+ scope.launch {
21+ println (" Coroutine 2 starts" )
22+ delay(500 )
23+ println (" Coroutine 2 completed" )
24+ }.invokeOnCompletion { throwable ->
25+ if (throwable is CancellationException ) {
26+ println (" Coroutine 2 got cancelled!" )
27+ }
28+ }
29+
30+ Thread .sleep(1000 )
31+
32+ println (" Scope got cancelled: ${! scope.isActive} " )
33+
34+ }
File renamed without changes.
Original file line number Diff line number Diff line change 1+ package com.lukaslechner.coroutineusecasesonandroid.playground.structuredconcurrency
2+
3+ import kotlinx.coroutines.*
4+
5+ fun main () {
6+
7+ val scope = CoroutineScope (Job ())
8+
9+ scope.launch {
10+
11+ doSomeTasks()
12+
13+ launch {
14+ println (" Starting Task 3" )
15+ delay(300 )
16+ println (" Task 3 completed" )
17+ }
18+ }
19+
20+ Thread .sleep(1000 )
21+ }
22+
23+ suspend fun doSomeTasks () = coroutineScope {
24+ launch {
25+ println (" Starting Task 1" )
26+ delay(100 )
27+ println (" Task 1 completed" )
28+ }
29+
30+ launch {
31+ println (" Starting Task 2" )
32+ delay(200 )
33+ println (" Task 2 completed" )
34+ }
35+ }
You can’t perform that action at this time.
0 commit comments