File tree Expand file tree Collapse file tree 16 files changed +451
-44
lines changed
kotlinx-coroutines-core/src/test/kotlin/examples Expand file tree Collapse file tree 16 files changed +451
-44
lines changed Load Diff Large diffs are not rendered by default.
Original file line number Diff line number Diff line change 1
- package examples
1
+ package example11
2
2
3
3
import kotlinx.coroutines.experimental.CommonPool
4
4
import kotlinx.coroutines.experimental.delay
Original file line number Diff line number Diff line change 1
- package examples
1
+ package example12
2
2
3
3
import kotlinx.coroutines.experimental.CommonPool
4
4
import kotlinx.coroutines.experimental.delay
Original file line number Diff line number Diff line change 1
- package examples
1
+ package example13
2
2
3
3
import kotlinx.coroutines.experimental.*
4
4
Original file line number Diff line number Diff line change 1
- package examples
1
+ package example14
2
2
3
3
import kotlinx.coroutines.experimental.*
4
4
Original file line number Diff line number Diff line change 1
- package examples
1
+ package example15
2
2
3
3
import kotlinx.coroutines.experimental.*
4
4
Original file line number Diff line number Diff line change 1
- package examples
1
+ package example16
2
2
3
3
import kotlinx.coroutines.experimental.CommonPool
4
4
import kotlinx.coroutines.experimental.delay
Original file line number Diff line number Diff line change 1
- package examples
1
+ package example21
2
2
3
3
import kotlinx.coroutines.experimental.CommonPool
4
4
import kotlinx.coroutines.experimental.delay
@@ -13,8 +13,8 @@ fun main(args: Array<String>) = runBlocking<Unit> {
13
13
}
14
14
}
15
15
delay(1300L ) // delay a bit
16
- println (" I'm tired of waiting!" )
16
+ println (" main: I'm tired of waiting!" )
17
17
job.cancel() // cancels the job
18
18
delay(1300L ) // delay a bit to ensure it was cancelled indeed
19
- println (" Now I can quit." )
19
+ println (" main: Now I can quit." )
20
20
}
Original file line number Diff line number Diff line change 1
- package examples
1
+ package example22
2
2
3
3
import kotlinx.coroutines.experimental.CommonPool
4
4
import kotlinx.coroutines.experimental.delay
@@ -18,8 +18,8 @@ fun main(args: Array<String>) = runBlocking<Unit> {
18
18
}
19
19
}
20
20
delay(1300L ) // delay a bit
21
- println (" I'm tired of waiting!" )
21
+ println (" main: I'm tired of waiting!" )
22
22
job.cancel() // cancels the job
23
23
delay(1300L ) // delay a bit to see if it was cancelled....
24
- println (" Now I can quit." )
24
+ println (" main: Now I can quit." )
25
25
}
Original file line number Diff line number Diff line change 1
- package examples
1
+ package example23
2
2
3
3
import kotlinx.coroutines.experimental.CommonPool
4
4
import kotlinx.coroutines.experimental.delay
@@ -18,8 +18,8 @@ fun main(args: Array<String>) = runBlocking<Unit> {
18
18
}
19
19
}
20
20
delay(1300L ) // delay a bit
21
- println (" I'm tired of waiting!" )
21
+ println (" main: I'm tired of waiting!" )
22
22
job.cancel() // cancels the job
23
23
delay(1300L ) // delay a bit to see if it was cancelled....
24
- println (" Now I can quit." )
24
+ println (" main: Now I can quit." )
25
25
}
Original file line number Diff line number Diff line change
1
+ package example24
2
+
3
+ import kotlinx.coroutines.experimental.CommonPool
4
+ import kotlinx.coroutines.experimental.delay
5
+ import kotlinx.coroutines.experimental.launch
6
+ import kotlinx.coroutines.experimental.runBlocking
7
+
8
+ fun main (args : Array <String >) = runBlocking<Unit > {
9
+ val job = launch(CommonPool ) {
10
+ try {
11
+ repeat(1000 ) { i ->
12
+ println (" I'm sleeping $i ..." )
13
+ delay(500L )
14
+ }
15
+ } finally {
16
+ println (" I'm running finally" )
17
+ }
18
+ }
19
+ delay(1300L ) // delay a bit
20
+ println (" main: I'm tired of waiting!" )
21
+ job.cancel() // cancels the job
22
+ delay(1300L ) // delay a bit to ensure it was cancelled indeed
23
+ println (" main: Now I can quit." )
24
+ }
Original file line number Diff line number Diff line change
1
+ package example25
2
+
3
+ import kotlinx.coroutines.experimental.*
4
+
5
+ fun main (args : Array <String >) = runBlocking<Unit > {
6
+ val job = launch(CommonPool ) {
7
+ try {
8
+ repeat(1000 ) { i ->
9
+ println (" I'm sleeping $i ..." )
10
+ delay(500L )
11
+ }
12
+ } finally {
13
+ run (NonCancellable ) {
14
+ println (" I'm running finally" )
15
+ delay(1000L )
16
+ println (" And I've just delayed for 1 sec because I'm non-cancellable" )
17
+ }
18
+ }
19
+ }
20
+ delay(1300L ) // delay a bit
21
+ println (" main: I'm tired of waiting!" )
22
+ job.cancel() // cancels the job
23
+ delay(1300L ) // delay a bit to ensure it was cancelled indeed
24
+ println (" main: Now I can quit." )
25
+ }
Original file line number Diff line number Diff line change
1
+ package example26
2
+
3
+ import kotlinx.coroutines.experimental.delay
4
+ import kotlinx.coroutines.experimental.runBlocking
5
+ import kotlinx.coroutines.experimental.withTimeout
6
+
7
+ fun main (args : Array <String >) = runBlocking<Unit > {
8
+ withTimeout(1300L ) {
9
+ repeat(1000 ) { i ->
10
+ println (" I'm sleeping $i ..." )
11
+ delay(500L )
12
+ }
13
+ }
14
+ }
Original file line number Diff line number Diff line change
1
+ package example31
2
+
3
+ import kotlinx.coroutines.experimental.delay
4
+ import kotlinx.coroutines.experimental.runBlocking
5
+ import kotlin.system.measureTimeMillis
6
+
7
+ suspend fun doSomethingUsefulOne (): Int {
8
+ delay(1000L ) // pretend we are doing something useful here
9
+ return 13
10
+ }
11
+
12
+ suspend fun doSomethingUsefulTwo (): Int {
13
+ delay(1000L ) // pretend we are doing something useful here, too
14
+ return 29
15
+ }
16
+
17
+ fun main (args : Array <String >) = runBlocking<Unit > {
18
+ val time = measureTimeMillis {
19
+ val one = doSomethingUsefulOne()
20
+ val two = doSomethingUsefulTwo()
21
+ println (" The answer is ${one + two} " )
22
+ }
23
+ println (" Completed in $time ms" )
24
+ }
Original file line number Diff line number Diff line change
1
+ package example32
2
+
3
+ import kotlinx.coroutines.experimental.CommonPool
4
+ import kotlinx.coroutines.experimental.defer
5
+ import kotlinx.coroutines.experimental.delay
6
+ import kotlinx.coroutines.experimental.runBlocking
7
+ import kotlin.system.measureTimeMillis
8
+
9
+ suspend fun doSomethingUsefulOne (): Int {
10
+ delay(1000L ) // pretend we are doing something useful here
11
+ return 13
12
+ }
13
+
14
+ suspend fun doSomethingUsefulTwo (): Int {
15
+ delay(1000L ) // pretend we are doing something useful here, too
16
+ return 29
17
+ }
18
+
19
+ fun main (args : Array <String >) = runBlocking<Unit > {
20
+ val time = measureTimeMillis {
21
+ val one = defer(CommonPool ) { doSomethingUsefulOne() }
22
+ val two = defer(CommonPool ) { doSomethingUsefulTwo() }
23
+ println (" The answer is ${one.await() + two.await()} " )
24
+ }
25
+ println (" Completed in $time ms" )
26
+ }
Original file line number Diff line number Diff line change
1
+ package example33
2
+
3
+ import kotlinx.coroutines.experimental.CommonPool
4
+ import kotlinx.coroutines.experimental.delay
5
+ import kotlinx.coroutines.experimental.lazyDefer
6
+ import kotlinx.coroutines.experimental.runBlocking
7
+ import kotlin.system.measureTimeMillis
8
+
9
+ suspend fun doSomethingUsefulOne (): Int {
10
+ delay(1000L ) // pretend we are doing something useful here
11
+ return 13
12
+ }
13
+
14
+ suspend fun doSomethingUsefulTwo (): Int {
15
+ delay(1000L ) // pretend we are doing something useful here, too
16
+ return 29
17
+ }
18
+
19
+ fun main (args : Array <String >) = runBlocking<Unit > {
20
+ val time = measureTimeMillis {
21
+ val one = lazyDefer(CommonPool ) { doSomethingUsefulOne() }
22
+ val two = lazyDefer(CommonPool ) { doSomethingUsefulTwo() }
23
+ println (" The answer is ${one.await() + two.await()} " )
24
+ }
25
+ println (" Completed in $time ms" )
26
+ }
You can’t perform that action at this time.
0 commit comments