File tree Expand file tree Collapse file tree 13 files changed +240
-2
lines changed
kotlinx-coroutines-jdk8/test
kotlinx-coroutines-nio/test/examples Expand file tree Collapse file tree 13 files changed +240
-2
lines changed File renamed without changes.
Original file line number Diff line number Diff line change
1
+ /*
2
+ * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3
+ */
4
+
5
+ package kotlinx.coroutines.experimental.examples
6
+
7
+ import kotlinx.coroutines.experimental.delay
8
+ import kotlinx.coroutines.experimental.future.future
9
+
10
+
11
+ fun main (args : Array <String >) {
12
+ val f = future {
13
+ try {
14
+ log(" Started f" )
15
+ delay(500 )
16
+ log(" Slept 500 ms #1" )
17
+ delay(500 )
18
+ log(" Slept 500 ms #2" )
19
+ delay(500 )
20
+ log(" Slept 500 ms #3" )
21
+ delay(500 )
22
+ log(" Slept 500 ms #4" )
23
+ delay(500 )
24
+ log(" Slept 500 ms #5" )
25
+ } catch (e: Exception ) {
26
+ log(" Aborting because of $e " )
27
+ }
28
+ }
29
+ Thread .sleep(1200 )
30
+ f.cancel(false )
31
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3
+ */
4
+
5
+ package kotlinx.coroutines.experimental.examples
6
+
7
+ import kotlinx.coroutines.experimental.Job
8
+ import kotlinx.coroutines.experimental.delay
9
+ import kotlinx.coroutines.experimental.future.future
10
+ import java.util.concurrent.CancellationException
11
+
12
+ fun main (args : Array <String >) {
13
+ val job = Job ()
14
+ log(" Starting futures f && g" )
15
+ val f = future(job) {
16
+ log(" Started f" )
17
+ delay(500 )
18
+ log(" f should not execute this line" )
19
+ }
20
+ val g = future(job) {
21
+ log(" Started g" )
22
+ try {
23
+ delay(500 )
24
+ } finally {
25
+ log(" g is executing finally!" )
26
+ }
27
+ log(" g should not execute this line" )
28
+ }
29
+ log(" Started futures f && g... will not wait -- cancel them!!!" )
30
+ job.cancel(CancellationException (" I don't want it" ))
31
+ check(f.isCancelled)
32
+ check(g.isCancelled)
33
+ log(" f result = ${Try <Unit > { f.get() }} " )
34
+ log(" g result = ${Try <Unit > { g.get() }} " )
35
+ Thread .sleep(1000L )
36
+ log(" Nothing executed!" )
37
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3
+ */
4
+
5
+ package kotlinx.coroutines.experimental.examples
6
+
7
+ import kotlinx.coroutines.experimental.CommonPool
8
+ import kotlinx.coroutines.experimental.async
9
+ import kotlinx.coroutines.experimental.delay
10
+ import kotlinx.coroutines.experimental.future.asCompletableFuture
11
+ import java.util.concurrent.TimeUnit
12
+
13
+ fun main (args : Array <String >) {
14
+ log(" Started" )
15
+ val deferred = async(CommonPool ) {
16
+ log(" Busy..." )
17
+ delay(1 , TimeUnit .SECONDS )
18
+ log(" Done..." )
19
+ 42
20
+ }
21
+ val future = deferred.asCompletableFuture()
22
+ log(" Got ${future.get()} " )
23
+ }
24
+
25
+
Original file line number Diff line number Diff line change
1
+ /*
2
+ * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3
+ */
4
+
5
+ package kotlinx.coroutines.experimental.examples
6
+
7
+ import java.text.*
8
+ import java.util.*
9
+
10
+ public class Try <out T > private constructor(private val _value : Any? ) {
11
+ private class Fail (val exception : Throwable ) {
12
+ override fun toString (): String = " Failure[$exception ]"
13
+ }
14
+ public companion object {
15
+ public operator fun <T > invoke (block : () -> T ): Try <T > =
16
+ try { Success (block()) } catch (e: Throwable ) { Failure <T >(e) }
17
+ public fun <T > Success (value : T ) = Try <T >(value)
18
+ public fun <T > Failure (exception : Throwable ) = Try <T >(Fail (exception))
19
+ }
20
+ @Suppress(" UNCHECKED_CAST" )
21
+ public val value: T get() = if (_value is Fail ) throw _value .exception else _value as T
22
+ public val exception: Throwable ? get() = (_value as ? Fail )?.exception
23
+ override fun toString (): String = _value .toString()
24
+ }
25
+
26
+ fun log (msg : String ) = println (" ${SimpleDateFormat (" yyyyMMdd-HHmmss.sss" ).format(Date ())} [${Thread .currentThread().name} ] $msg " )
Original file line number Diff line number Diff line change
1
+ /*
2
+ * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3
+ */
4
+
5
+ package kotlinx.coroutines.experimental.examples
6
+
7
+ import kotlinx.coroutines.experimental.future.await
8
+ import kotlinx.coroutines.experimental.runBlocking
9
+ import java.util.concurrent.CompletableFuture
10
+
11
+ fun main (args : Array <String >) {
12
+ // Let's assume that we have a future coming from some 3rd party API
13
+ val future: CompletableFuture <Int > = CompletableFuture .supplyAsync {
14
+ Thread .sleep(1000L ) // imitate some long-running computation, actually
15
+ 42
16
+ }
17
+ // now let's launch a coroutine and await for this future inside it
18
+ runBlocking {
19
+ println (" We can do something else, while we are waiting for future..." )
20
+ println (" We've got ${future.await()} from the future!" )
21
+ }
22
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3
+ */
4
+
5
+ package kotlinx.coroutines.experimental.examples
6
+
7
+ import kotlinx.coroutines.experimental.delay
8
+ import kotlinx.coroutines.experimental.future.future
9
+ import java.util.concurrent.CompletableFuture
10
+ import java.util.concurrent.TimeUnit
11
+
12
+ // this function returns a CompletableFuture using Kotlin coroutines
13
+ fun supplyTheAnswerAsync (): CompletableFuture <Int > = future {
14
+ println (" We might be doing some asynchronous IO here or something else..." )
15
+ delay(1 , TimeUnit .SECONDS ) // just do a non-blocking delay
16
+ 42 // The answer!
17
+ }
18
+
19
+ fun main (args : Array <String >) {
20
+ // We can use `supplyTheAnswerAsync` just like any other future-supplier function
21
+ val future = supplyTheAnswerAsync()
22
+ println (" The answer is ${future.get()} " )
23
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3
+ */
4
+
5
+ package kotlinx.coroutines.experimental.examples
6
+
7
+ import kotlinx.coroutines.experimental.future.await
8
+ import kotlinx.coroutines.experimental.future.future
9
+ import java.util.concurrent.CompletableFuture
10
+
11
+ fun main (args : Array <String >) {
12
+ // this example shows how easy it is to perform multiple async operations with coroutines
13
+ val future = future {
14
+ (1 .. 5 ).map { // loops are no problem at all
15
+ startLongAsyncOperation(it).await() // suspend while the long method is running
16
+ }.joinToString(" \n " )
17
+ }
18
+ println (" We have a long-running computation in background, let's wait for its result..." )
19
+ println (future.get())
20
+ }
21
+
22
+ fun startLongAsyncOperation (num : Int ): CompletableFuture <String > =
23
+ CompletableFuture .supplyAsync {
24
+ Thread .sleep(1000L ) // imitate some long-running computation, actually
25
+ " $num " // and return a number converted to string
26
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3
+ */
4
+
5
+ package kotlinx.coroutines.experimental.examples
6
+
7
+ import kotlinx.coroutines.experimental.*
8
+ import kotlinx.coroutines.experimental.future.*
9
+
10
+ fun main (args : Array <String >) {
11
+ fun slow (s : String ) = future {
12
+ delay(500L )
13
+ s
14
+ }
15
+ val f = future<String > {
16
+ log(" Started f" )
17
+ val a = slow(" A" ).await()
18
+ log(" a = $a " )
19
+ withTimeout(1000L ) {
20
+ val b = slow(" B" ).await()
21
+ log(" b = $b " )
22
+ }
23
+ try {
24
+ withTimeout(750L ) {
25
+ val c = slow(" C" ).await()
26
+ log(" c = $c " )
27
+ val d = slow(" D" ).await()
28
+ log(" d = $d " )
29
+ }
30
+ } catch (ex: CancellationException ) {
31
+ log(" timed out with $ex " )
32
+ }
33
+ val e = slow(" E" ).await()
34
+ log(" e = $e " )
35
+ " done"
36
+ }
37
+ log(" f.get() = ${f.get()} " )
38
+ }
File renamed without changes.
You can’t perform that action at this time.
0 commit comments