File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed
kotlinx-coroutines-jdk8/src/test/kotlin/examples Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change
1
+ package examples
2
+
3
+ import kotlinx.coroutines.experimental.future.await
4
+ import kotlinx.coroutines.experimental.future.future
5
+ import java.util.concurrent.CompletableFuture
6
+
7
+ fun main (args : Array <String >) {
8
+ // this example shows how easy it is to perform multiple async operations with coroutines
9
+ val future = future {
10
+ (1 .. 5 ).map { // loops are no problem at all
11
+ startLongAsyncOperation(it).await() // suspend while the long method is running
12
+ }.joinToString(" \n " )
13
+ }
14
+ println (" We have a long-running computation in background, let's wait for its result..." )
15
+ println (future.get())
16
+ }
17
+
18
+ fun startLongAsyncOperation (num : Int ): CompletableFuture <String > =
19
+ CompletableFuture .supplyAsync {
20
+ Thread .sleep(1000L ) // imitate some long-running computation, actually
21
+ " $num " // and return a number converted to string
22
+ }
You can’t perform that action at this time.
0 commit comments