├── .gitignore ├── CHANGES.md ├── README.md ├── benchmarks ├── pom.xml └── src │ └── main │ └── kotlin │ └── benchmarks │ ├── GuideSyncBenchmark.kt │ └── PingPongActorBenchmark.kt ├── coroutines-guide.md ├── knit ├── README.md └── src │ └── Knit.kt ├── kotlinx-coroutines-core ├── README.md ├── pom.xml └── src │ ├── main │ └── kotlin │ │ └── kotlinx │ │ └── coroutines │ │ └── experimental │ │ ├── Builders.kt │ │ ├── CancellableContinuation.kt │ │ ├── CommonPool.kt │ │ ├── CoroutineContext.kt │ │ ├── CoroutineDispatcher.kt │ │ ├── CoroutineExceptionHandler.kt │ │ ├── CoroutineName.kt │ │ ├── CoroutineScope.kt │ │ ├── CoroutineStart.kt │ │ ├── Deferred.kt │ │ ├── Delay.kt │ │ ├── EventLoop.kt │ │ ├── Executors.kt │ │ ├── Job.kt │ │ ├── LazyDeferred.kt │ │ ├── NonCancellable.kt │ │ ├── Scheduled.kt │ │ ├── ThreadPoolDispatcher.kt │ │ ├── Yield.kt │ │ ├── channels │ │ ├── AbstractChannel.kt │ │ ├── Actor.kt │ │ ├── ArrayBroadcastChannel.kt │ │ ├── ArrayChannel.kt │ │ ├── BroadcastChannel.kt │ │ ├── Channel.kt │ │ ├── ChannelCoroutine.kt │ │ ├── Channels.kt │ │ ├── ConflatedBroadcastChannel.kt │ │ ├── ConflatedChannel.kt │ │ ├── LinkedListChannel.kt │ │ ├── Produce.kt │ │ └── RendezvousChannel.kt │ │ ├── internal │ │ ├── Atomic.kt │ │ ├── LockFreeLinkedList.kt │ │ └── Symbol.kt │ │ ├── intrinsics │ │ └── Undispatched.kt │ │ ├── selects │ │ ├── Select.kt │ │ ├── SelectUnbiased.kt │ │ └── WhileSelect.kt │ │ └── sync │ │ └── Mutex.kt │ └── test │ └── kotlin │ ├── guide │ ├── example-basic-01.kt │ ├── example-basic-02.kt │ ├── example-basic-03.kt │ ├── example-basic-04.kt │ ├── example-basic-05.kt │ ├── example-basic-06.kt │ ├── example-cancel-01.kt │ ├── example-cancel-02.kt │ ├── example-cancel-03.kt │ ├── example-cancel-04.kt │ ├── example-cancel-05.kt │ ├── example-cancel-06.kt │ ├── example-channel-01.kt │ ├── example-channel-02.kt │ ├── example-channel-03.kt │ ├── example-channel-04.kt │ ├── example-channel-05.kt │ ├── example-channel-06.kt │ ├── example-channel-07.kt │ ├── example-channel-08.kt │ ├── example-channel-09.kt │ ├── example-compose-01.kt │ ├── example-compose-02.kt │ ├── example-compose-03.kt │ ├── example-compose-04.kt │ ├── example-context-01.kt │ ├── example-context-02.kt │ ├── example-context-03.kt │ ├── example-context-04.kt │ ├── example-context-05.kt │ ├── example-context-06.kt │ ├── example-context-07.kt │ ├── example-context-08.kt │ ├── example-context-09.kt │ ├── example-select-01.kt │ ├── example-select-02.kt │ ├── example-select-03.kt │ ├── example-select-04.kt │ ├── example-select-05.kt │ ├── example-sync-01.kt │ ├── example-sync-02.kt │ ├── example-sync-03.kt │ ├── example-sync-04.kt │ ├── example-sync-05.kt │ ├── example-sync-06.kt │ ├── example-sync-07.kt │ └── test │ │ ├── GuideTest.kt │ │ └── TestUtil.kt │ └── kotlinx │ └── coroutines │ └── experimental │ ├── AsyncLazyTest.kt │ ├── AsyncTest.kt │ ├── CancellableContinuationImplTest.kt │ ├── CoroutinesTest.kt │ ├── ExecutorsTest.kt │ ├── JobTest.kt │ ├── LaunchLazyTest.kt │ ├── RunTest.kt │ ├── TestBase.kt │ ├── Try.kt │ ├── WithTimeoutOrNullTest.kt │ ├── WithTimeoutOrNullThreadDispatchTest.kt │ ├── WithTimeoutTest.kt │ ├── WithTimeoutThreadDispatchTest.kt │ ├── channels │ ├── ActorLazyTest.kt │ ├── ActorTest.kt │ ├── ArrayBroadcastChannelTest.kt │ ├── ArrayChannelTest.kt │ ├── ChannelAtomicCancelStressTest.kt │ ├── ChannelSendReceiveStressTest.kt │ ├── ConflatedBroadcastChannelTest.kt │ ├── ConflatedChannelTest.kt │ ├── RendezvousChannelTest.kt │ ├── SimpleSendReceiveTest.kt │ └── TestChannelKind.kt │ ├── internal │ ├── LockFreeLinkedListAtomicStressTest.kt │ ├── LockFreeLinkedListLongStressTest.kt │ ├── LockFreeLinkedListShortStressTest.kt │ └── LockFreeLinkedListTest.kt │ ├── selects │ ├── SelectArrayChannelTest.kt │ ├── SelectBiasTest.kt │ ├── SelectDeferredTest.kt │ ├── SelectJobTest.kt │ ├── SelectMutexTest.kt │ ├── SelectPhilosophersStressTest.kt │ ├── SelectRendezvousChannelTest.kt │ └── SelectTimeoutTest.kt │ └── sync │ └── MutexTest.kt ├── kotlinx-coroutines-jdk8 ├── README.md ├── pom.xml └── src │ ├── main │ └── kotlin │ │ └── kotlinx │ │ └── coroutines │ │ └── experimental │ │ └── future │ │ └── Future.kt │ └── test │ └── kotlin │ ├── examples │ ├── CancelFuture-example.kt │ ├── ExplicitJob-example.kt │ ├── ToFuture-example.kt │ ├── Try.kt │ ├── log.kt │ ├── simple-example-1.kt │ ├── simple-example-2.kt │ ├── simple-example-3.kt │ └── withTimeout-example.kt │ └── kotlinx │ └── coroutines │ └── experimental │ └── future │ └── FutureTest.kt ├── kotlinx-coroutines-nio ├── README.md ├── pom.xml └── src │ ├── main │ └── kotlin │ │ └── kotlinx │ │ └── coroutines │ │ └── experimental │ │ └── nio │ │ └── Nio.kt │ └── test │ └── kotlin │ ├── examples │ ├── echo-example.kt │ └── log.kt │ └── kotlinx │ └── coroutines │ └── experimental │ └── nio │ └── AsyncIOTest.kt ├── license ├── LICENSE.txt ├── NOTICE.txt └── third_party │ └── minima_LICENSE.txt ├── pom.xml ├── reactive ├── README.md ├── coroutines-guide-reactive.md ├── kotlinx-coroutines-reactive │ ├── README.md │ ├── pom.xml │ └── src │ │ ├── main │ │ └── kotlin │ │ │ └── kotlinx │ │ │ └── coroutines │ │ │ └── experimental │ │ │ └── reactive │ │ │ ├── Await.kt │ │ │ ├── Channel.kt │ │ │ ├── Convert.kt │ │ │ └── Publish.kt │ │ └── test │ │ └── kotlin │ │ └── kotlinx │ │ └── coroutines │ │ └── experimental │ │ └── reactive │ │ ├── IntegrationTest.kt │ │ ├── PublishTest.kt │ │ ├── PublisherBackpressureTest.kt │ │ ├── PublisherCompletionStressTest.kt │ │ └── PublisherMultiTest.kt ├── kotlinx-coroutines-reactor │ ├── README.md │ ├── pom.xml │ └── src │ │ ├── main │ │ └── kotlin │ │ │ └── kotlinx │ │ │ └── coroutines │ │ │ └── experimental │ │ │ └── reactor │ │ │ ├── Convert.kt │ │ │ ├── Flux.kt │ │ │ ├── Mono.kt │ │ │ └── Scheduler.kt │ │ └── test │ │ └── kotlin │ │ └── kotlinx │ │ └── coroutines │ │ └── experimental │ │ └── reactor │ │ ├── Check.kt │ │ ├── ConvertTest.kt │ │ ├── FluxCompletionStressTest.kt │ │ ├── FluxMultiTest.kt │ │ ├── FluxSingleTest.kt │ │ ├── FluxTest.kt │ │ ├── MonoTest.kt │ │ └── SchedulerTest.kt ├── kotlinx-coroutines-rx-example │ ├── pom.xml │ └── src │ │ └── main │ │ └── kotlin │ │ └── main.kt ├── kotlinx-coroutines-rx1 │ ├── README.md │ ├── pom.xml │ └── src │ │ ├── main │ │ └── kotlin │ │ │ └── kotlinx │ │ │ └── coroutines │ │ │ └── experimental │ │ │ └── rx1 │ │ │ ├── RxAwait.kt │ │ │ ├── RxChannel.kt │ │ │ ├── RxCompletable.kt │ │ │ ├── RxConvert.kt │ │ │ ├── RxObservable.kt │ │ │ └── RxSingle.kt │ │ └── test │ │ └── kotlin │ │ └── kotlinx │ │ └── coroutines │ │ └── experimental │ │ └── rx1 │ │ ├── Check.kt │ │ ├── CompletableTest.kt │ │ ├── ConvertTest.kt │ │ ├── IntegrationTest.kt │ │ ├── ObservableBackpressureTest.kt │ │ ├── ObservableCompletionStressTest.kt │ │ ├── ObservableMultiTest.kt │ │ ├── ObservableSingleTest.kt │ │ ├── ObservableTest.kt │ │ └── SingleTest.kt └── kotlinx-coroutines-rx2 │ ├── README.md │ ├── pom.xml │ └── src │ ├── main │ └── kotlin │ │ └── kotlinx │ │ └── coroutines │ │ └── experimental │ │ └── rx2 │ │ ├── RxAwait.kt │ │ ├── RxChannel.kt │ │ ├── RxCompletable.kt │ │ ├── RxConvert.kt │ │ ├── RxFlowable.kt │ │ ├── RxMaybe.kt │ │ ├── RxObservable.kt │ │ ├── RxScheduler.kt │ │ └── RxSingle.kt │ └── test │ └── kotlin │ ├── guide │ ├── example-reactive-basic-01.kt │ ├── example-reactive-basic-02.kt │ ├── example-reactive-basic-03.kt │ ├── example-reactive-basic-04.kt │ ├── example-reactive-basic-05.kt │ ├── example-reactive-basic-06.kt │ ├── example-reactive-basic-07.kt │ ├── example-reactive-basic-08.kt │ ├── example-reactive-basic-09.kt │ ├── example-reactive-context-01.kt │ ├── example-reactive-context-02.kt │ ├── example-reactive-context-03.kt │ ├── example-reactive-context-04.kt │ ├── example-reactive-context-05.kt │ ├── example-reactive-operators-01.kt │ ├── example-reactive-operators-02.kt │ ├── example-reactive-operators-03.kt │ ├── example-reactive-operators-04.kt │ └── test │ │ └── GuideReactiveTest.kt │ └── kotlinx │ └── coroutines │ └── experimental │ └── rx2 │ ├── Check.kt │ ├── CompletableTest.kt │ ├── ConvertTest.kt │ ├── FlowableTest.kt │ ├── IntegrationTest.kt │ ├── MaybeTest.kt │ ├── ObservableCompletionStressTest.kt │ ├── ObservableMultiTest.kt │ ├── ObservableSingleTest.kt │ ├── ObservableTest.kt │ ├── SchedulerTest.kt │ └── SingleTest.kt ├── site ├── README.md ├── build.xml ├── docs │ ├── Gemfile │ ├── Gemfile.lock │ ├── _config.yml │ ├── _includes │ │ ├── footer.html │ │ ├── head.html │ │ └── header.html │ ├── _layouts │ │ └── api.html │ ├── _sass │ │ ├── _api.scss │ │ ├── _base.scss │ │ ├── _layout.scss │ │ └── _minima.scss │ ├── assets │ │ └── main.scss │ └── index.md └── pom.xml └── ui ├── README.md ├── coroutines-guide-ui.md ├── kotlinx-coroutines-android ├── README.md ├── example-app │ ├── .gitignore │ ├── app │ │ ├── build.gradle │ │ ├── proguard-rules.pro │ │ └── src │ │ │ └── main │ │ │ ├── AndroidManifest.xml │ │ │ ├── java │ │ │ └── com │ │ │ │ └── example │ │ │ │ └── app │ │ │ │ └── MainActivity.kt │ │ │ └── res │ │ │ ├── layout │ │ │ ├── activity_main.xml │ │ │ └── content_main.xml │ │ │ ├── menu │ │ │ └── menu_main.xml │ │ │ ├── mipmap-hdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-mdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxxhdpi │ │ │ ├── ic_launcher.png │ │ │ └── ic_launcher_round.png │ │ │ └── values │ │ │ ├── colors.xml │ │ │ ├── dimens.xml │ │ │ ├── strings.xml │ │ │ └── styles.xml │ ├── build.gradle │ ├── gradle.properties │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ └── settings.gradle ├── pom.xml └── src │ └── main │ └── kotlin │ └── kotlinx │ └── coroutines │ └── experimental │ └── android │ └── HandlerContext.kt ├── kotlinx-coroutines-javafx ├── README.md ├── pom.xml └── src │ ├── main │ └── kotlin │ │ └── kotlinx │ │ └── coroutines │ │ └── experimental │ │ └── javafx │ │ └── JavaFx.kt │ └── test │ └── kotlin │ ├── examples │ └── FxExampleApp.kt │ └── guide │ ├── example-ui-actor-01.kt │ ├── example-ui-actor-02.kt │ ├── example-ui-actor-03.kt │ ├── example-ui-advanced-01.kt │ ├── example-ui-advanced-02.kt │ ├── example-ui-basic-01.kt │ ├── example-ui-basic-02.kt │ ├── example-ui-basic-03.kt │ ├── example-ui-blocking-01.kt │ ├── example-ui-blocking-02.kt │ └── example-ui-blocking-03.kt ├── kotlinx-coroutines-swing ├── README.md ├── pom.xml └── src │ ├── main │ └── kotlin │ │ └── kotlinx │ │ └── coroutines │ │ └── experimental │ │ └── swing │ │ └── Swing.kt │ └── test │ └── kotlin │ ├── examples │ ├── SwingExampleApp.kt │ └── swing-example.kt │ └── kotlinx │ └── coroutines │ └── experimental │ └── swing │ └── SwingTest.kt ├── ui-example-android.png └── ui-example-javafx.png /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | *.iml 3 | target -------------------------------------------------------------------------------- /knit/README.md: -------------------------------------------------------------------------------- 1 | # Knit 2 | 3 | This is a very simple tool that produces Kotlin source example files from a markdown document that includes 4 | snippets of Kotlin code in its body. It is used to produce examples for 5 | [coroutines guide](../coroutines-guide.md). 6 | 7 | ## Updating guide 8 | 9 | * In project root directory do: 10 | * Run `mvn clean` 11 | * Run `mvn compile` 12 | * Run `mvn pre-site` (or `mvn site` if you have Jekyll) 13 | * Run `Knit coroutines-guide.md` (from IDEA, mark `knit/src` as source root first) 14 | * Commit updated `coroutines-guide.md` and examples 15 | 16 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/CoroutineName.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kotlinx.coroutines.experimental 18 | 19 | import kotlin.coroutines.experimental.AbstractCoroutineContextElement 20 | import kotlin.coroutines.experimental.CoroutineContext 21 | 22 | /** 23 | * User-specified name of coroutine. This name is used in debugging mode. 24 | * See [newCoroutineContext] for the description of coroutine debugging facilities. 25 | */ 26 | public data class CoroutineName( 27 | /** 28 | * User-defined coroutine name. 29 | */ 30 | val name: String 31 | ) : AbstractCoroutineContextElement(CoroutineName) { 32 | /** 33 | * Key for [CoroutineName] instance in the coroutine context. 34 | */ 35 | public companion object Key : CoroutineContext.Key 36 | 37 | /** 38 | * Returns a string representation of the object. 39 | */ 40 | override fun toString(): String = "CoroutineName($name)" 41 | } 42 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/LazyDeferred.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kotlinx.coroutines.experimental 18 | 19 | import kotlin.coroutines.experimental.CoroutineContext 20 | 21 | /** 22 | * @suppress **Deprecated**: `Deferred` incorporates functionality of `LazyDeferred`. See [Deferred]. 23 | */ 24 | @Deprecated(message = "`Deferred` incorporates functionality of `LazyDeferred`", level = DeprecationLevel.WARNING, 25 | replaceWith = ReplaceWith("Deferred")) 26 | typealias LazyDeferred = Deferred 27 | 28 | /** 29 | * @suppress **Deprecated**: Replace with `async(context, start = false) { ... }`. See [async]. 30 | */ 31 | @Deprecated(message = "This functionality is incorporated into `async", level = DeprecationLevel.WARNING, 32 | replaceWith = ReplaceWith("async(context, start = false, block = block)")) 33 | public fun lazyDefer(context: CoroutineContext, block: suspend CoroutineScope.() -> T) : Deferred = 34 | async(context, start = false, block = block) 35 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/Yield.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kotlinx.coroutines.experimental 18 | 19 | import kotlin.coroutines.experimental.intrinsics.COROUTINE_SUSPENDED 20 | import kotlin.coroutines.experimental.intrinsics.suspendCoroutineOrReturn 21 | 22 | /** 23 | * Yields a thread (or thread pool) of the current coroutine dispatcher to other coroutines to run. 24 | * If the coroutine dispatcher does not have its own thread pool (like [Unconfined] dispatcher) then this 25 | * function does nothing, but checks if the coroutine [Job] was completed. 26 | * This suspending function is cancellable. 27 | * If the [Job] of the current coroutine is completed when this suspending function is invoked or while 28 | * this function is waiting for dispatching, it resumes with [CancellationException]. 29 | */ 30 | suspend fun yield(): Unit = suspendCoroutineOrReturn sc@ { cont -> 31 | val context = cont.context 32 | val job = context[Job] 33 | if (job != null && job.isCompleted) throw job.getCompletionException() 34 | if (cont !is DispatchedContinuation) return@sc Unit 35 | if (!cont.dispatcher.isDispatchNeeded(context)) return@sc Unit 36 | cont.dispatchYield(job, Unit) 37 | COROUTINE_SUSPENDED 38 | } 39 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/channels/BroadcastChannel.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kotlinx.coroutines.experimental.channels 18 | 19 | import java.io.Closeable 20 | 21 | /** 22 | * Broadcast channel is a non-blocking primitive for communication between the sender and multiple receivers 23 | * that subscribe for the elements using [open] function and unsubscribe using [SubscriptionReceiveChannel.close] 24 | * function. 25 | */ 26 | public interface BroadcastChannel : SendChannel { 27 | /** 28 | * Subscribes to this [BroadcastChannel] and returns a channel to receive elements from it. 29 | * The resulting channel shall be [closed][SubscriptionReceiveChannel.close] to unsubscribe from this 30 | * broadcast channel. 31 | */ 32 | public fun open(): SubscriptionReceiveChannel 33 | } 34 | 35 | /** 36 | * Return type for [BroadcastChannel.open] that can be used to [receive] elements from the 37 | * open subscription and to [close] it to unsubscribe. 38 | */ 39 | public interface SubscriptionReceiveChannel : ReceiveChannel, Closeable { 40 | /** 41 | * Closes this subscription. 42 | */ 43 | public override fun close() 44 | } 45 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/channels/ChannelCoroutine.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kotlinx.coroutines.experimental.channels 18 | 19 | import kotlinx.coroutines.experimental.AbstractCoroutine 20 | import kotlinx.coroutines.experimental.JobSupport 21 | import kotlinx.coroutines.experimental.handleCoroutineException 22 | import kotlin.coroutines.experimental.CoroutineContext 23 | 24 | internal open class ChannelCoroutine( 25 | override val parentContext: CoroutineContext, 26 | open val channel: Channel, 27 | active: Boolean 28 | ) : AbstractCoroutine(active), Channel by channel { 29 | override fun afterCompletion(state: Any?, mode: Int) { 30 | val cause = (state as? JobSupport.CompletedExceptionally)?.cause 31 | if (!channel.close(cause) && cause != null) 32 | handleCoroutineException(context, cause) 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/channels/Channels.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kotlinx.coroutines.experimental.channels 18 | 19 | internal const val DEFAULT_CLOSE_MESSAGE = "Channel was closed" 20 | 21 | /** 22 | * Performs the given [action] for each received element. 23 | */ 24 | // :todo: make it inline when this bug is fixed: https://youtrack.jetbrains.com/issue/KT-16448 25 | public suspend fun ReceiveChannel.consumeEach(action: suspend (E) -> Unit) { 26 | for (element in this) action(element) 27 | } 28 | 29 | /** 30 | * Subscribes to this [BroadcastChannel] and performs the specified action for each received element. 31 | */ 32 | // :todo: make it inline when this bug is fixed: https://youtrack.jetbrains.com/issue/KT-16448 33 | public suspend fun BroadcastChannel.consumeEach(action: suspend (E) -> Unit) { 34 | open().use { channel -> 35 | for (x in channel) action(x) 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/channels/RendezvousChannel.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kotlinx.coroutines.experimental.channels 18 | 19 | /** 20 | * Rendezvous channel. This channel does not have any buffer at all. An element is transferred from sender 21 | * to receiver only when [send] and [receive] invocations meet in time (rendezvous), so [send] suspends 22 | * until another coroutine invokes [receive] and [receive] suspends until another coroutine invokes [send]. 23 | * 24 | * This implementation is fully lock-free. 25 | */ 26 | public open class RendezvousChannel : AbstractChannel() { 27 | protected final override val isBufferAlwaysEmpty: Boolean get() = true 28 | protected final override val isBufferEmpty: Boolean get() = true 29 | protected final override val isBufferAlwaysFull: Boolean get() = true 30 | protected final override val isBufferFull: Boolean get() = true 31 | } 32 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/internal/Symbol.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kotlinx.coroutines.experimental.internal 18 | 19 | /** @suppress **This is unstable API and it is subject to change.** */ 20 | public class Symbol(val symbol: String) { 21 | override fun toString(): String = symbol 22 | } 23 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/intrinsics/Undispatched.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kotlinx.coroutines.experimental.intrinsics 18 | 19 | import kotlin.coroutines.experimental.Continuation 20 | import kotlin.coroutines.experimental.intrinsics.* 21 | import kotlin.coroutines.experimental.suspendCoroutine 22 | 23 | /** 24 | * Use this function to restart coroutine directly from inside of [suspendCoroutine]. 25 | * 26 | * @suppress **This is unstable API and it is subject to change.** 27 | */ 28 | @Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "UNCHECKED_CAST") 29 | internal fun (suspend () -> R).startCoroutineUndispatched(completion: Continuation) { 30 | val value = try { 31 | startCoroutineUninterceptedOrReturn(completion) 32 | } catch (e: Throwable) { 33 | completion.resumeWithException(e) 34 | return 35 | } 36 | if (value !== COROUTINE_SUSPENDED) 37 | completion.resume(value as R) 38 | } 39 | 40 | /** 41 | * Use this function to restart coroutine directly from inside of [suspendCoroutine]. 42 | * 43 | * @suppress **This is unstable API and it is subject to change.** 44 | */ 45 | @Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "UNCHECKED_CAST") 46 | internal fun (suspend (E) -> R).startCoroutineUndispatched(element: E, completion: Continuation) { 47 | val value = try { 48 | startCoroutineUninterceptedOrReturn(element, completion) 49 | } catch (e: Throwable) { 50 | completion.resumeWithException(e) 51 | return 52 | } 53 | if (value !== COROUTINE_SUSPENDED) 54 | completion.resume(value as R) 55 | } 56 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/selects/WhileSelect.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kotlinx.coroutines.experimental.selects 18 | 19 | /** 20 | * Loops while [select] expression returns `true`. 21 | * 22 | * The statement of the form: 23 | * 24 | * ``` 25 | * whileSelect { 26 | * /*body*/ 27 | * } 28 | * ``` 29 | * 30 | * is a shortcut for: 31 | * 32 | * ``` 33 | * while(select { 34 | * /*body*/ 35 | * }) {} 36 | */ 37 | suspend fun whileSelect(builder: SelectBuilder.() -> Unit) { 38 | while(select(builder)) {} 39 | } 40 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-basic-01.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.basic.example01 19 | 20 | import kotlinx.coroutines.experimental.* 21 | 22 | fun main(args: Array) { 23 | launch(CommonPool) { // create new coroutine in common thread pool 24 | delay(1000L) // non-blocking delay for 1 second (default time unit is ms) 25 | println("World!") // print after delay 26 | } 27 | println("Hello,") // main function continues while coroutine is delayed 28 | Thread.sleep(2000L) // block main thread for 2 seconds to keep JVM alive 29 | } 30 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-basic-02.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.basic.example02 19 | 20 | import kotlinx.coroutines.experimental.* 21 | 22 | fun main(args: Array) = runBlocking { // start main coroutine 23 | launch(CommonPool) { // create new coroutine in common thread pool 24 | delay(1000L) 25 | println("World!") 26 | } 27 | println("Hello,") // main coroutine continues while child is delayed 28 | delay(2000L) // non-blocking delay for 2 seconds to keep JVM alive 29 | } 30 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-basic-03.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.basic.example03 19 | 20 | import kotlinx.coroutines.experimental.* 21 | 22 | fun main(args: Array) = runBlocking { 23 | val job = launch(CommonPool) { // create new coroutine and keep a reference to its Job 24 | delay(1000L) 25 | println("World!") 26 | } 27 | println("Hello,") 28 | job.join() // wait until child coroutine completes 29 | } 30 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-basic-04.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.basic.example04 19 | 20 | import kotlinx.coroutines.experimental.* 21 | 22 | fun main(args: Array) = runBlocking { 23 | val job = launch(CommonPool) { doWorld() } 24 | println("Hello,") 25 | job.join() 26 | } 27 | 28 | // this is your first suspending function 29 | suspend fun doWorld() { 30 | delay(1000L) 31 | println("World!") 32 | } 33 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-basic-05.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.basic.example05 19 | 20 | import kotlinx.coroutines.experimental.* 21 | 22 | fun main(args: Array) = runBlocking { 23 | val jobs = List(100_000) { // create a lot of coroutines and list their jobs 24 | launch(CommonPool) { 25 | delay(1000L) 26 | print(".") 27 | } 28 | } 29 | jobs.forEach { it.join() } // wait for all jobs to complete 30 | } 31 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-basic-06.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.basic.example06 19 | 20 | import kotlinx.coroutines.experimental.* 21 | 22 | fun main(args: Array) = runBlocking { 23 | launch(CommonPool) { 24 | repeat(1000) { i -> 25 | println("I'm sleeping $i ...") 26 | delay(500L) 27 | } 28 | } 29 | delay(1300L) // just quit after delay 30 | } 31 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-cancel-01.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.cancel.example01 19 | 20 | import kotlinx.coroutines.experimental.* 21 | 22 | fun main(args: Array) = runBlocking { 23 | val job = launch(CommonPool) { 24 | repeat(1000) { i -> 25 | println("I'm sleeping $i ...") 26 | delay(500L) 27 | } 28 | } 29 | delay(1300L) // delay a bit 30 | println("main: I'm tired of waiting!") 31 | job.cancel() // cancels the job 32 | delay(1300L) // delay a bit to ensure it was cancelled indeed 33 | println("main: Now I can quit.") 34 | } 35 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-cancel-02.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.cancel.example02 19 | 20 | import kotlinx.coroutines.experimental.* 21 | 22 | fun main(args: Array) = runBlocking { 23 | val job = launch(CommonPool) { 24 | var nextPrintTime = 0L 25 | var i = 0 26 | while (i < 10) { // computation loop 27 | val currentTime = System.currentTimeMillis() 28 | if (currentTime >= nextPrintTime) { 29 | println("I'm sleeping ${i++} ...") 30 | nextPrintTime = currentTime + 500L 31 | } 32 | } 33 | } 34 | delay(1300L) // delay a bit 35 | println("main: I'm tired of waiting!") 36 | job.cancel() // cancels the job 37 | delay(1300L) // delay a bit to see if it was cancelled.... 38 | println("main: Now I can quit.") 39 | } 40 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-cancel-03.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.cancel.example03 19 | 20 | import kotlinx.coroutines.experimental.* 21 | 22 | fun main(args: Array) = runBlocking { 23 | val job = launch(CommonPool) { 24 | var nextPrintTime = 0L 25 | var i = 0 26 | while (isActive) { // cancellable computation loop 27 | val currentTime = System.currentTimeMillis() 28 | if (currentTime >= nextPrintTime) { 29 | println("I'm sleeping ${i++} ...") 30 | nextPrintTime = currentTime + 500L 31 | } 32 | } 33 | } 34 | delay(1300L) // delay a bit 35 | println("main: I'm tired of waiting!") 36 | job.cancel() // cancels the job 37 | delay(1300L) // delay a bit to see if it was cancelled.... 38 | println("main: Now I can quit.") 39 | } 40 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-cancel-04.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.cancel.example04 19 | 20 | import kotlinx.coroutines.experimental.* 21 | 22 | fun main(args: Array) = runBlocking { 23 | val job = launch(CommonPool) { 24 | try { 25 | repeat(1000) { i -> 26 | println("I'm sleeping $i ...") 27 | delay(500L) 28 | } 29 | } finally { 30 | println("I'm running finally") 31 | } 32 | } 33 | delay(1300L) // delay a bit 34 | println("main: I'm tired of waiting!") 35 | job.cancel() // cancels the job 36 | delay(1300L) // delay a bit to ensure it was cancelled indeed 37 | println("main: Now I can quit.") 38 | } 39 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-cancel-05.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.cancel.example05 19 | 20 | import kotlinx.coroutines.experimental.* 21 | 22 | fun main(args: Array) = runBlocking { 23 | val job = launch(CommonPool) { 24 | try { 25 | repeat(1000) { i -> 26 | println("I'm sleeping $i ...") 27 | delay(500L) 28 | } 29 | } finally { 30 | run(NonCancellable) { 31 | println("I'm running finally") 32 | delay(1000L) 33 | println("And I've just delayed for 1 sec because I'm non-cancellable") 34 | } 35 | } 36 | } 37 | delay(1300L) // delay a bit 38 | println("main: I'm tired of waiting!") 39 | job.cancel() // cancels the job 40 | delay(1300L) // delay a bit to ensure it was cancelled indeed 41 | println("main: Now I can quit.") 42 | } 43 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-cancel-06.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.cancel.example06 19 | 20 | import kotlinx.coroutines.experimental.* 21 | 22 | fun main(args: Array) = runBlocking { 23 | withTimeout(1300L) { 24 | repeat(1000) { i -> 25 | println("I'm sleeping $i ...") 26 | delay(500L) 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-channel-01.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.channel.example01 19 | 20 | import kotlinx.coroutines.experimental.* 21 | import kotlinx.coroutines.experimental.channels.* 22 | 23 | fun main(args: Array) = runBlocking { 24 | val channel = Channel() 25 | launch(CommonPool) { 26 | // this might be heavy CPU-consuming computation or async logic, we'll just send five squares 27 | for (x in 1..5) channel.send(x * x) 28 | } 29 | // here we print five received integers: 30 | repeat(5) { println(channel.receive()) } 31 | println("Done!") 32 | } 33 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-channel-02.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.channel.example02 19 | 20 | import kotlinx.coroutines.experimental.* 21 | import kotlinx.coroutines.experimental.channels.* 22 | 23 | fun main(args: Array) = runBlocking { 24 | val channel = Channel() 25 | launch(CommonPool) { 26 | for (x in 1..5) channel.send(x * x) 27 | channel.close() // we're done sending 28 | } 29 | // here we print received values using `for` loop (until the channel is closed) 30 | for (y in channel) println(y) 31 | println("Done!") 32 | } 33 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-channel-03.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.channel.example03 19 | 20 | import kotlinx.coroutines.experimental.* 21 | import kotlinx.coroutines.experimental.channels.* 22 | 23 | fun produceSquares() = produce(CommonPool) { 24 | for (x in 1..5) send(x * x) 25 | } 26 | 27 | fun main(args: Array) = runBlocking { 28 | val squares = produceSquares() 29 | squares.consumeEach { println(it) } 30 | println("Done!") 31 | } 32 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-channel-04.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.channel.example04 19 | 20 | import kotlinx.coroutines.experimental.* 21 | import kotlinx.coroutines.experimental.channels.* 22 | 23 | fun produceNumbers() = produce(CommonPool) { 24 | var x = 1 25 | while (true) send(x++) // infinite stream of integers starting from 1 26 | } 27 | 28 | fun square(numbers: ReceiveChannel) = produce(CommonPool) { 29 | for (x in numbers) send(x * x) 30 | } 31 | 32 | fun main(args: Array) = runBlocking { 33 | val numbers = produceNumbers() // produces integers from 1 and on 34 | val squares = square(numbers) // squares integers 35 | for (i in 1..5) println(squares.receive()) // print first five 36 | println("Done!") // we are done 37 | squares.cancel() // need to cancel these coroutines in a larger app 38 | numbers.cancel() 39 | } 40 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-channel-05.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.channel.example05 19 | 20 | import kotlinx.coroutines.experimental.* 21 | import kotlinx.coroutines.experimental.channels.* 22 | import kotlin.coroutines.experimental.CoroutineContext 23 | 24 | fun numbersFrom(context: CoroutineContext, start: Int) = produce(context) { 25 | var x = start 26 | while (true) send(x++) // infinite stream of integers from start 27 | } 28 | 29 | fun filter(context: CoroutineContext, numbers: ReceiveChannel, prime: Int) = produce(context) { 30 | for (x in numbers) if (x % prime != 0) send(x) 31 | } 32 | 33 | fun main(args: Array) = runBlocking { 34 | var cur = numbersFrom(context, 2) 35 | for (i in 1..10) { 36 | val prime = cur.receive() 37 | println(prime) 38 | cur = filter(context, cur, prime) 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-channel-06.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.channel.example06 19 | 20 | import kotlinx.coroutines.experimental.* 21 | import kotlinx.coroutines.experimental.channels.* 22 | 23 | fun produceNumbers() = produce(CommonPool) { 24 | var x = 1 // start from 1 25 | while (true) { 26 | send(x++) // produce next 27 | delay(100) // wait 0.1s 28 | } 29 | } 30 | 31 | fun launchProcessor(id: Int, channel: ReceiveChannel) = launch(CommonPool) { 32 | channel.consumeEach { 33 | println("Processor #$id received $it") 34 | } 35 | } 36 | 37 | fun main(args: Array) = runBlocking { 38 | val producer = produceNumbers() 39 | repeat(5) { launchProcessor(it, producer) } 40 | delay(1000) 41 | producer.cancel() // cancel producer coroutine and thus kill them all 42 | } 43 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-channel-07.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.channel.example07 19 | 20 | import kotlinx.coroutines.experimental.* 21 | import kotlinx.coroutines.experimental.channels.* 22 | 23 | suspend fun sendString(channel: SendChannel, s: String, time: Long) { 24 | while (true) { 25 | delay(time) 26 | channel.send(s) 27 | } 28 | } 29 | 30 | fun main(args: Array) = runBlocking { 31 | val channel = Channel() 32 | launch(context) { sendString(channel, "foo", 200L) } 33 | launch(context) { sendString(channel, "BAR!", 500L) } 34 | repeat(6) { // receive first six 35 | println(channel.receive()) 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-channel-08.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.channel.example08 19 | 20 | import kotlinx.coroutines.experimental.* 21 | import kotlinx.coroutines.experimental.channels.* 22 | 23 | fun main(args: Array) = runBlocking { 24 | val channel = Channel(4) // create buffered channel 25 | launch(context) { // launch sender coroutine 26 | repeat(10) { 27 | println("Sending $it") // print before sending each element 28 | channel.send(it) // will suspend when buffer is full 29 | } 30 | } 31 | // don't receive anything... just wait.... 32 | delay(1000) 33 | } 34 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-channel-09.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.channel.example09 19 | 20 | import kotlinx.coroutines.experimental.* 21 | import kotlinx.coroutines.experimental.channels.* 22 | 23 | data class Ball(var hits: Int) 24 | 25 | fun main(args: Array) = runBlocking { 26 | val table = Channel() // a shared table 27 | launch(context) { player("ping", table) } 28 | launch(context) { player("pong", table) } 29 | table.send(Ball(0)) // serve the ball 30 | delay(1000) // delay 1 second 31 | table.receive() // game over, grab the ball 32 | } 33 | 34 | suspend fun player(name: String, table: Channel) { 35 | for (ball in table) { // receive the ball in a loop 36 | ball.hits++ 37 | println("$name $ball") 38 | delay(300) // wait a bit 39 | table.send(ball) // send the ball back 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-compose-01.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.compose.example01 19 | 20 | import kotlinx.coroutines.experimental.* 21 | import kotlin.system.measureTimeMillis 22 | 23 | suspend fun doSomethingUsefulOne(): Int { 24 | delay(1000L) // pretend we are doing something useful here 25 | return 13 26 | } 27 | 28 | suspend fun doSomethingUsefulTwo(): Int { 29 | delay(1000L) // pretend we are doing something useful here, too 30 | return 29 31 | } 32 | 33 | fun main(args: Array) = runBlocking { 34 | val time = measureTimeMillis { 35 | val one = doSomethingUsefulOne() 36 | val two = doSomethingUsefulTwo() 37 | println("The answer is ${one + two}") 38 | } 39 | println("Completed in $time ms") 40 | } 41 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-compose-02.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.compose.example02 19 | 20 | import kotlinx.coroutines.experimental.* 21 | import kotlin.system.measureTimeMillis 22 | 23 | suspend fun doSomethingUsefulOne(): Int { 24 | delay(1000L) // pretend we are doing something useful here 25 | return 13 26 | } 27 | 28 | suspend fun doSomethingUsefulTwo(): Int { 29 | delay(1000L) // pretend we are doing something useful here, too 30 | return 29 31 | } 32 | 33 | fun main(args: Array) = runBlocking { 34 | val time = measureTimeMillis { 35 | val one = async(CommonPool) { doSomethingUsefulOne() } 36 | val two = async(CommonPool) { doSomethingUsefulTwo() } 37 | println("The answer is ${one.await() + two.await()}") 38 | } 39 | println("Completed in $time ms") 40 | } 41 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-compose-03.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.compose.example03 19 | 20 | import kotlinx.coroutines.experimental.* 21 | import kotlin.system.measureTimeMillis 22 | 23 | suspend fun doSomethingUsefulOne(): Int { 24 | delay(1000L) // pretend we are doing something useful here 25 | return 13 26 | } 27 | 28 | suspend fun doSomethingUsefulTwo(): Int { 29 | delay(1000L) // pretend we are doing something useful here, too 30 | return 29 31 | } 32 | 33 | fun main(args: Array) = runBlocking { 34 | val time = measureTimeMillis { 35 | val one = async(CommonPool, CoroutineStart.LAZY) { doSomethingUsefulOne() } 36 | val two = async(CommonPool, CoroutineStart.LAZY) { doSomethingUsefulTwo() } 37 | println("The answer is ${one.await() + two.await()}") 38 | } 39 | println("Completed in $time ms") 40 | } 41 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-compose-04.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.compose.example04 19 | 20 | import kotlinx.coroutines.experimental.* 21 | import kotlin.system.measureTimeMillis 22 | 23 | suspend fun doSomethingUsefulOne(): Int { 24 | delay(1000L) // pretend we are doing something useful here 25 | return 13 26 | } 27 | 28 | suspend fun doSomethingUsefulTwo(): Int { 29 | delay(1000L) // pretend we are doing something useful here, too 30 | return 29 31 | } 32 | 33 | // The result type of asyncSomethingUsefulOne is Deferred 34 | fun asyncSomethingUsefulOne() = async(CommonPool) { 35 | doSomethingUsefulOne() 36 | } 37 | 38 | // The result type of asyncSomethingUsefulTwo is Deferred 39 | fun asyncSomethingUsefulTwo() = async(CommonPool) { 40 | doSomethingUsefulTwo() 41 | } 42 | 43 | // note, that we don't have `runBlocking` to the right of `main` in this example 44 | fun main(args: Array) { 45 | val time = measureTimeMillis { 46 | // we can initiate async actions outside of a coroutine 47 | val one = asyncSomethingUsefulOne() 48 | val two = asyncSomethingUsefulTwo() 49 | // but waiting for a result must involve either suspending or blocking. 50 | // here we use `runBlocking { ... }` to block the main thread while waiting for the result 51 | runBlocking { 52 | println("The answer is ${one.await() + two.await()}") 53 | } 54 | } 55 | println("Completed in $time ms") 56 | } 57 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-context-01.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.context.example01 19 | 20 | import kotlinx.coroutines.experimental.* 21 | 22 | fun main(args: Array) = runBlocking { 23 | val jobs = arrayListOf() 24 | jobs += launch(Unconfined) { // not confined -- will work with main thread 25 | println(" 'Unconfined': I'm working in thread ${Thread.currentThread().name}") 26 | } 27 | jobs += launch(context) { // context of the parent, runBlocking coroutine 28 | println(" 'context': I'm working in thread ${Thread.currentThread().name}") 29 | } 30 | jobs += launch(CommonPool) { // will get dispatched to ForkJoinPool.commonPool (or equivalent) 31 | println(" 'CommonPool': I'm working in thread ${Thread.currentThread().name}") 32 | } 33 | jobs += launch(newSingleThreadContext("MyOwnThread")) { // will get its own new thread 34 | println(" 'newSTC': I'm working in thread ${Thread.currentThread().name}") 35 | } 36 | jobs.forEach { it.join() } 37 | } 38 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-context-02.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.context.example02 19 | 20 | import kotlinx.coroutines.experimental.* 21 | 22 | fun main(args: Array) = runBlocking { 23 | val jobs = arrayListOf() 24 | jobs += launch(Unconfined) { // not confined -- will work with main thread 25 | println(" 'Unconfined': I'm working in thread ${Thread.currentThread().name}") 26 | delay(500) 27 | println(" 'Unconfined': After delay in thread ${Thread.currentThread().name}") 28 | } 29 | jobs += launch(context) { // context of the parent, runBlocking coroutine 30 | println(" 'context': I'm working in thread ${Thread.currentThread().name}") 31 | delay(1000) 32 | println(" 'context': After delay in thread ${Thread.currentThread().name}") 33 | } 34 | jobs.forEach { it.join() } 35 | } 36 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-context-03.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.context.example03 19 | 20 | import kotlinx.coroutines.experimental.* 21 | 22 | fun log(msg: String) = println("[${Thread.currentThread().name}] $msg") 23 | 24 | fun main(args: Array) = runBlocking { 25 | val a = async(context) { 26 | log("I'm computing a piece of the answer") 27 | 6 28 | } 29 | val b = async(context) { 30 | log("I'm computing another piece of the answer") 31 | 7 32 | } 33 | log("The answer is ${a.await() * b.await()}") 34 | } 35 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-context-04.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.context.example04 19 | 20 | import kotlinx.coroutines.experimental.* 21 | 22 | fun log(msg: String) = println("[${Thread.currentThread().name}] $msg") 23 | 24 | fun main(args: Array) { 25 | val ctx1 = newSingleThreadContext("Ctx1") 26 | val ctx2 = newSingleThreadContext("Ctx2") 27 | runBlocking(ctx1) { 28 | log("Started in ctx1") 29 | run(ctx2) { 30 | log("Working in ctx2") 31 | } 32 | log("Back to ctx1") 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-context-05.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.context.example05 19 | 20 | import kotlinx.coroutines.experimental.* 21 | 22 | fun main(args: Array) = runBlocking { 23 | println("My job is ${context[Job]}") 24 | } 25 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-context-06.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.context.example06 19 | 20 | import kotlinx.coroutines.experimental.* 21 | 22 | fun main(args: Array) = runBlocking { 23 | // start a coroutine to process some kind of incoming request 24 | val request = launch(CommonPool) { 25 | // it spawns two other jobs, one with its separate context 26 | val job1 = launch(CommonPool) { 27 | println("job1: I have my own context and execute independently!") 28 | delay(1000) 29 | println("job1: I am not affected by cancellation of the request") 30 | } 31 | // and the other inherits the parent context 32 | val job2 = launch(context) { 33 | println("job2: I am a child of the request coroutine") 34 | delay(1000) 35 | println("job2: I will not execute this line if my parent request is cancelled") 36 | } 37 | // request completes when both its sub-jobs complete: 38 | job1.join() 39 | job2.join() 40 | } 41 | delay(500) 42 | request.cancel() // cancel processing of the request 43 | delay(1000) // delay a second to see what happens 44 | println("main: Who has survived request cancellation?") 45 | } 46 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-context-07.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.context.example07 19 | 20 | import kotlinx.coroutines.experimental.* 21 | 22 | fun main(args: Array) = runBlocking { 23 | // start a coroutine to process some kind of incoming request 24 | val request = launch(context) { // use the context of `runBlocking` 25 | // spawns CPU-intensive child job in CommonPool !!! 26 | val job = launch(context + CommonPool) { 27 | println("job: I am a child of the request coroutine, but with a different dispatcher") 28 | delay(1000) 29 | println("job: I will not execute this line if my parent request is cancelled") 30 | } 31 | job.join() // request completes when its sub-job completes 32 | } 33 | delay(500) 34 | request.cancel() // cancel processing of the request 35 | delay(1000) // delay a second to see what happens 36 | println("main: Who has survived request cancellation?") 37 | } 38 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-context-08.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.context.example08 19 | 20 | import kotlinx.coroutines.experimental.* 21 | 22 | fun log(msg: String) = println("[${Thread.currentThread().name}] $msg") 23 | 24 | fun main(args: Array) = runBlocking(CoroutineName("main")) { 25 | log("Started main coroutine") 26 | // run two background value computations 27 | val v1 = async(CommonPool + CoroutineName("v1coroutine")) { 28 | log("Computing v1") 29 | delay(500) 30 | 252 31 | } 32 | val v2 = async(CommonPool + CoroutineName("v2coroutine")) { 33 | log("Computing v2") 34 | delay(1000) 35 | 6 36 | } 37 | log("The answer for v1 / v2 = ${v1.await() / v2.await()}") 38 | } 39 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-context-09.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.context.example09 19 | 20 | import kotlinx.coroutines.experimental.* 21 | 22 | fun main(args: Array) = runBlocking { 23 | val job = Job() // create a job object to manage our lifecycle 24 | // now launch ten coroutines for a demo, each working for a different time 25 | val coroutines = List(10) { i -> 26 | // they are all children of our job object 27 | launch(context + job) { // we use the context of main runBlocking thread, but with our own job object 28 | delay(i * 200L) // variable delay 0ms, 200ms, 400ms, ... etc 29 | println("Coroutine $i is done") 30 | } 31 | } 32 | println("Launched ${coroutines.size} coroutines") 33 | delay(500L) // delay for half a second 34 | println("Cancelling job!") 35 | job.cancel() // cancel our job.. !!! 36 | delay(1000L) // delay for more to see if our coroutines are still working 37 | } 38 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-select-01.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.select.example01 19 | 20 | import kotlinx.coroutines.experimental.* 21 | import kotlinx.coroutines.experimental.channels.* 22 | import kotlinx.coroutines.experimental.selects.* 23 | import kotlin.coroutines.experimental.CoroutineContext 24 | 25 | fun fizz(context: CoroutineContext) = produce(context) { 26 | while (true) { // sends "Fizz" every 300 ms 27 | delay(300) 28 | send("Fizz") 29 | } 30 | } 31 | 32 | fun buzz(context: CoroutineContext) = produce(context) { 33 | while (true) { // sends "Buzz!" every 500 ms 34 | delay(500) 35 | send("Buzz!") 36 | } 37 | } 38 | 39 | suspend fun selectFizzBuzz(fizz: ReceiveChannel, buzz: ReceiveChannel) { 40 | select { // means that this select expression does not produce any result 41 | fizz.onReceive { value -> // this is the first select clause 42 | println("fizz -> '$value'") 43 | } 44 | buzz.onReceive { value -> // this is the second select clause 45 | println("buzz -> '$value'") 46 | } 47 | } 48 | } 49 | 50 | fun main(args: Array) = runBlocking { 51 | val fizz = fizz(context) 52 | val buzz = buzz(context) 53 | repeat(7) { 54 | selectFizzBuzz(fizz, buzz) 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-select-02.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.select.example02 19 | 20 | import kotlinx.coroutines.experimental.* 21 | import kotlinx.coroutines.experimental.channels.* 22 | import kotlinx.coroutines.experimental.selects.* 23 | 24 | suspend fun selectAorB(a: ReceiveChannel, b: ReceiveChannel): String = 25 | select { 26 | a.onReceiveOrNull { value -> 27 | if (value == null) 28 | "Channel 'a' is closed" 29 | else 30 | "a -> '$value'" 31 | } 32 | b.onReceiveOrNull { value -> 33 | if (value == null) 34 | "Channel 'b' is closed" 35 | else 36 | "b -> '$value'" 37 | } 38 | } 39 | 40 | fun main(args: Array) = runBlocking { 41 | // we are using the context of the main thread in this example for predictability ... 42 | val a = produce(context) { 43 | repeat(4) { send("Hello $it") } 44 | } 45 | val b = produce(context) { 46 | repeat(4) { send("World $it") } 47 | } 48 | repeat(8) { // print first eight results 49 | println(selectAorB(a, b)) 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-select-03.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.select.example03 19 | 20 | import kotlinx.coroutines.experimental.* 21 | import kotlinx.coroutines.experimental.channels.* 22 | import kotlinx.coroutines.experimental.selects.* 23 | 24 | fun produceNumbers(side: SendChannel) = produce(CommonPool) { 25 | for (num in 1..10) { // produce 10 numbers from 1 to 10 26 | delay(100) // every 100 ms 27 | select { 28 | onSend(num) {} // Send to the primary channel 29 | side.onSend(num) {} // or to the side channel 30 | } 31 | } 32 | } 33 | 34 | fun main(args: Array) = runBlocking { 35 | val side = Channel() // allocate side channel 36 | launch(context) { // this is a very fast consumer for the side channel 37 | side.consumeEach { println("Side channel has $it") } 38 | } 39 | produceNumbers(side).consumeEach { 40 | println("Consuming $it") 41 | delay(250) // let us digest the consumed number properly, do not hurry 42 | } 43 | println("Done consuming") 44 | } 45 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-select-04.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.select.example04 19 | 20 | import kotlinx.coroutines.experimental.* 21 | import kotlinx.coroutines.experimental.channels.* 22 | import kotlinx.coroutines.experimental.selects.* 23 | import java.util.* 24 | 25 | fun asyncString(time: Int) = async(CommonPool) { 26 | delay(time.toLong()) 27 | "Waited for $time ms" 28 | } 29 | 30 | fun asyncStringsList(): List> { 31 | val random = Random(3) 32 | return List(12) { asyncString(random.nextInt(1000)) } 33 | } 34 | 35 | fun main(args: Array) = runBlocking { 36 | val list = asyncStringsList() 37 | val result = select { 38 | list.withIndex().forEach { (index, deferred) -> 39 | deferred.onAwait { answer -> 40 | "Deferred $index produced answer '$answer'" 41 | } 42 | } 43 | } 44 | println(result) 45 | val countActive = list.count { it.isActive } 46 | println("$countActive coroutines are still active") 47 | } 48 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-sync-01.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.sync.example01 19 | 20 | import kotlinx.coroutines.experimental.* 21 | import kotlin.coroutines.experimental.CoroutineContext 22 | import kotlin.system.measureTimeMillis 23 | 24 | suspend fun massiveRun(context: CoroutineContext, action: suspend () -> Unit) { 25 | val n = 1000 // number of coroutines to launch 26 | val k = 1000 // times an action is repeated by each coroutine 27 | val time = measureTimeMillis { 28 | val jobs = List(n) { 29 | launch(context) { 30 | repeat(k) { action() } 31 | } 32 | } 33 | jobs.forEach { it.join() } 34 | } 35 | println("Completed ${n * k} actions in $time ms") 36 | } 37 | 38 | var counter = 0 39 | 40 | fun main(args: Array) = runBlocking { 41 | massiveRun(CommonPool) { 42 | counter++ 43 | } 44 | println("Counter = $counter") 45 | } 46 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-sync-02.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.sync.example02 19 | 20 | import kotlinx.coroutines.experimental.* 21 | import kotlin.coroutines.experimental.CoroutineContext 22 | import kotlin.system.measureTimeMillis 23 | 24 | suspend fun massiveRun(context: CoroutineContext, action: suspend () -> Unit) { 25 | val n = 1000 // number of coroutines to launch 26 | val k = 1000 // times an action is repeated by each coroutine 27 | val time = measureTimeMillis { 28 | val jobs = List(n) { 29 | launch(context) { 30 | repeat(k) { action() } 31 | } 32 | } 33 | jobs.forEach { it.join() } 34 | } 35 | println("Completed ${n * k} actions in $time ms") 36 | } 37 | 38 | @Volatile // in Kotlin `volatile` is an annotation 39 | var counter = 0 40 | 41 | fun main(args: Array) = runBlocking { 42 | massiveRun(CommonPool) { 43 | counter++ 44 | } 45 | println("Counter = $counter") 46 | } 47 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-sync-03.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.sync.example03 19 | 20 | import kotlinx.coroutines.experimental.* 21 | import kotlin.coroutines.experimental.CoroutineContext 22 | import kotlin.system.measureTimeMillis 23 | import java.util.concurrent.atomic.AtomicInteger 24 | 25 | suspend fun massiveRun(context: CoroutineContext, action: suspend () -> Unit) { 26 | val n = 1000 // number of coroutines to launch 27 | val k = 1000 // times an action is repeated by each coroutine 28 | val time = measureTimeMillis { 29 | val jobs = List(n) { 30 | launch(context) { 31 | repeat(k) { action() } 32 | } 33 | } 34 | jobs.forEach { it.join() } 35 | } 36 | println("Completed ${n * k} actions in $time ms") 37 | } 38 | 39 | var counter = AtomicInteger() 40 | 41 | fun main(args: Array) = runBlocking { 42 | massiveRun(CommonPool) { 43 | counter.incrementAndGet() 44 | } 45 | println("Counter = ${counter.get()}") 46 | } 47 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-sync-04.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.sync.example04 19 | 20 | import kotlinx.coroutines.experimental.* 21 | import kotlin.coroutines.experimental.CoroutineContext 22 | import kotlin.system.measureTimeMillis 23 | 24 | suspend fun massiveRun(context: CoroutineContext, action: suspend () -> Unit) { 25 | val n = 1000 // number of coroutines to launch 26 | val k = 1000 // times an action is repeated by each coroutine 27 | val time = measureTimeMillis { 28 | val jobs = List(n) { 29 | launch(context) { 30 | repeat(k) { action() } 31 | } 32 | } 33 | jobs.forEach { it.join() } 34 | } 35 | println("Completed ${n * k} actions in $time ms") 36 | } 37 | 38 | val counterContext = newSingleThreadContext("CounterContext") 39 | var counter = 0 40 | 41 | fun main(args: Array) = runBlocking { 42 | massiveRun(CommonPool) { // run each coroutine in CommonPool 43 | run(counterContext) { // but confine each increment to the single-threaded context 44 | counter++ 45 | } 46 | } 47 | println("Counter = $counter") 48 | } 49 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-sync-05.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.sync.example05 19 | 20 | import kotlinx.coroutines.experimental.* 21 | import kotlin.coroutines.experimental.CoroutineContext 22 | import kotlin.system.measureTimeMillis 23 | 24 | suspend fun massiveRun(context: CoroutineContext, action: suspend () -> Unit) { 25 | val n = 1000 // number of coroutines to launch 26 | val k = 1000 // times an action is repeated by each coroutine 27 | val time = measureTimeMillis { 28 | val jobs = List(n) { 29 | launch(context) { 30 | repeat(k) { action() } 31 | } 32 | } 33 | jobs.forEach { it.join() } 34 | } 35 | println("Completed ${n * k} actions in $time ms") 36 | } 37 | 38 | val counterContext = newSingleThreadContext("CounterContext") 39 | var counter = 0 40 | 41 | fun main(args: Array) = runBlocking { 42 | massiveRun(counterContext) { // run each coroutine in the single-threaded context 43 | counter++ 44 | } 45 | println("Counter = $counter") 46 | } 47 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/guide/example-sync-06.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. 18 | package guide.sync.example06 19 | 20 | import kotlinx.coroutines.experimental.* 21 | import kotlin.coroutines.experimental.CoroutineContext 22 | import kotlin.system.measureTimeMillis 23 | import kotlinx.coroutines.experimental.sync.Mutex 24 | 25 | suspend fun massiveRun(context: CoroutineContext, action: suspend () -> Unit) { 26 | val n = 1000 // number of coroutines to launch 27 | val k = 1000 // times an action is repeated by each coroutine 28 | val time = measureTimeMillis { 29 | val jobs = List(n) { 30 | launch(context) { 31 | repeat(k) { action() } 32 | } 33 | } 34 | jobs.forEach { it.join() } 35 | } 36 | println("Completed ${n * k} actions in $time ms") 37 | } 38 | 39 | val mutex = Mutex() 40 | var counter = 0 41 | 42 | fun main(args: Array) = runBlocking { 43 | massiveRun(CommonPool) { 44 | mutex.lock() 45 | try { counter++ } 46 | finally { mutex.unlock() } 47 | } 48 | println("Counter = $counter") 49 | } 50 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/LaunchLazyTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kotlinx.coroutines.experimental 18 | 19 | import org.junit.Test 20 | 21 | class LaunchLazyTest : TestBase() { 22 | @Test 23 | fun testLaunchAndYieldJoin() = runBlocking { 24 | expect(1) 25 | val job = launch(context, CoroutineStart.LAZY) { 26 | expect(4) 27 | yield() // does nothing -- main waits 28 | expect(5) 29 | } 30 | expect(2) 31 | yield() // does nothing, was not started yet 32 | expect(3) 33 | check(!job.isActive && !job.isCompleted) 34 | job.join() 35 | check(!job.isActive && job.isCompleted) 36 | finish(6) 37 | } 38 | 39 | @Test 40 | fun testStart() = runBlocking { 41 | expect(1) 42 | val job = launch(context, CoroutineStart.LAZY) { 43 | expect(5) 44 | yield() // yields back to main 45 | expect(7) 46 | } 47 | expect(2) 48 | yield() // does nothing, was not started yet 49 | expect(3) 50 | check(!job.isActive && !job.isCompleted) 51 | check(job.start()) 52 | check(job.isActive && !job.isCompleted) 53 | check(!job.start()) // start again -- does nothing 54 | check(job.isActive && !job.isCompleted) 55 | expect(4) 56 | yield() // now yield to started coroutine 57 | expect(6) 58 | check(job.isActive && !job.isCompleted) 59 | yield() // yield again 60 | check(!job.isActive && job.isCompleted) // it completes this time 61 | expect(8) 62 | job.join() // immediately returns 63 | finish(9) 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/TestBase.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kotlinx.coroutines.experimental 18 | 19 | import org.junit.After 20 | import java.util.concurrent.atomic.AtomicBoolean 21 | import java.util.concurrent.atomic.AtomicInteger 22 | import java.util.concurrent.atomic.AtomicReference 23 | 24 | open class TestBase { 25 | val isStressTest = System.getProperty("stressTest") != null 26 | val stressTestMultiplier = if (isStressTest) 30 else 1 27 | 28 | var actionIndex = AtomicInteger() 29 | var finished = AtomicBoolean() 30 | var error = AtomicReference() 31 | 32 | public fun error(message: Any): Nothing { 33 | val exception = IllegalStateException(message.toString()) 34 | error.compareAndSet(null, exception) 35 | throw exception 36 | } 37 | 38 | public inline fun check(value: Boolean, lazyMessage: () -> Any): Unit { 39 | if (!value) error(lazyMessage()) 40 | } 41 | 42 | fun expect(index: Int) { 43 | val wasIndex = actionIndex.incrementAndGet() 44 | check(index == wasIndex) { "Expecting action index $index but it is actually $wasIndex" } 45 | } 46 | 47 | fun expectUnreached() { 48 | error("Should not be reached") 49 | } 50 | 51 | fun finish(index: Int) { 52 | expect(index) 53 | check(!finished.getAndSet(true)) { "Should call 'finish(...)' at most once" } 54 | } 55 | 56 | @After 57 | fun onCompletion() { 58 | error.get()?.let { throw it } 59 | check(actionIndex.get() == 0 || finished.get()) { "Expecting that 'finish(...)' was invoked, but it was not" } 60 | } 61 | 62 | } -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/Try.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kotlinx.coroutines.experimental 18 | 19 | public class Try private constructor(private val _value: Any?) { 20 | private class Fail(val exception: Throwable) { 21 | override fun toString(): String = "Failure[$exception]" 22 | } 23 | 24 | public companion object { 25 | public operator fun invoke(block: () -> T): Try = 26 | try { Success(block()) } catch(e: Throwable) { Failure(e) } 27 | public fun Success(value: T) = Try(value) 28 | public fun Failure(exception: Throwable) = Try(Fail(exception)) 29 | } 30 | 31 | @Suppress("UNCHECKED_CAST") 32 | public val value: T get() = if (_value is Fail) throw _value.exception else _value as T 33 | 34 | public val exception: Throwable? get() = (_value as? Fail)?.exception 35 | 36 | override fun toString(): String = _value.toString() 37 | } 38 | -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/selects/SelectBiasTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kotlinx.coroutines.experimental.selects 18 | 19 | import kotlinx.coroutines.experimental.async 20 | import kotlinx.coroutines.experimental.runBlocking 21 | import org.junit.Assert.assertEquals 22 | import org.junit.Assert.assertTrue 23 | import org.junit.Test 24 | 25 | class SelectBiasTest { 26 | val n = 10_000 27 | 28 | @Test 29 | fun testBiased() = runBlocking { 30 | val d0 = async(context) { 0 } 31 | val d1 = async(context) { 1 } 32 | val counter = IntArray(2) 33 | repeat(n) { 34 | val selected = select { 35 | d0.onAwait { 0 } 36 | d1.onAwait { 1 } 37 | } 38 | counter[selected]++ 39 | } 40 | assertEquals(n, counter[0]) 41 | assertEquals(0, counter[1]) 42 | } 43 | 44 | @Test 45 | fun testUnbiased() = runBlocking { 46 | val d0 = async(context) { 0 } 47 | val d1 = async(context) { 1 } 48 | val counter = IntArray(2) 49 | repeat(n) { 50 | val selected = selectUnbiased { 51 | d0.onAwait { 0 } 52 | d1.onAwait { 1 } 53 | } 54 | counter[selected]++ 55 | } 56 | assertTrue(counter[0] >= n / 4) 57 | assertTrue(counter[1] >= n / 4) 58 | } 59 | } -------------------------------------------------------------------------------- /kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/selects/SelectTimeoutTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kotlinx.coroutines.experimental.selects 18 | 19 | import kotlinx.coroutines.experimental.TestBase 20 | import kotlinx.coroutines.experimental.runBlocking 21 | import org.hamcrest.MatcherAssert.assertThat 22 | import org.hamcrest.core.IsEqual 23 | import org.junit.Test 24 | 25 | class SelectTimeoutTest : TestBase() { 26 | @Test 27 | fun testBasic() = runBlocking { 28 | expect(1) 29 | val result = select { 30 | onTimeout(1000) { 31 | expectUnreached() 32 | "FAIL" 33 | } 34 | onTimeout(100) { 35 | expect(2) 36 | "OK" 37 | } 38 | onTimeout(500) { 39 | expectUnreached() 40 | "FAIL" 41 | } 42 | } 43 | assertThat(result, IsEqual("OK")) 44 | finish(3) 45 | } 46 | } -------------------------------------------------------------------------------- /kotlinx-coroutines-jdk8/README.md: -------------------------------------------------------------------------------- 1 | # Module kotlinx-coroutines-jdk8 2 | 3 | Additional libraries for JDK8 (or Android API level 24). 4 | 5 | Coroutine builders: 6 | 7 | | **Name** | **Result** | **Scope** | **Description** 8 | | -------- | ---------- | ---------- | --------------- 9 | | [future] | [CompletableFuture][java.util.concurrent.CompletableFuture] | [CoroutineScope] | Returns a single value with the future result 10 | 11 | Extension functions: 12 | 13 | | **Name** | **Description** 14 | | -------- | --------------- 15 | | [CompletableFuture.await][java.util.concurrent.CompletableFuture.await] | Awaits for completion of the future 16 | | [Deferred.asCompletableFuture][kotlinx.coroutines.experimental.Deferred.asCompletableFuture] | Converts a deferred value to the future 17 | 18 | # Package kotlinx.coroutines.experimental.future 19 | 20 | Additional libraries for JDK8 [CompletableFuture][java.util.concurrent.CompletableFuture]. 21 | 22 | 23 | 24 | 25 | [CoroutineScope]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-scope/index.html 26 | 27 | 28 | 29 | [future]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-jdk8/kotlinx.coroutines.experimental.future/future.html 30 | [java.util.concurrent.CompletableFuture]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-jdk8/kotlinx.coroutines.experimental.future/java.util.concurrent.-completable-future/index.html 31 | [java.util.concurrent.CompletableFuture.await]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-jdk8/kotlinx.coroutines.experimental.future/java.util.concurrent.-completable-future/await.html 32 | [kotlinx.coroutines.experimental.Deferred.asCompletableFuture]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-jdk8/kotlinx.coroutines.experimental.future/kotlinx.coroutines.experimental.-deferred/as-completable-future.html 33 | 34 | -------------------------------------------------------------------------------- /kotlinx-coroutines-jdk8/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 19 | 20 | 4.0.0 21 | 22 | 23 | org.jetbrains.kotlinx 24 | kotlinx-coroutines 25 | 0.15-SNAPSHOT 26 | 27 | 28 | kotlinx-coroutines-jdk8 29 | jar 30 | 31 | 32 | src/main/kotlin 33 | src/test/kotlin 34 | 35 | 36 | 37 | 38 | org.jetbrains.kotlinx 39 | kotlinx-coroutines-core 40 | ${project.version} 41 | compile 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /kotlinx-coroutines-jdk8/src/test/kotlin/examples/CancelFuture-example.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package examples 18 | 19 | import kotlinx.coroutines.experimental.delay 20 | import kotlinx.coroutines.experimental.future.future 21 | 22 | fun main(args: Array) { 23 | val f = future { 24 | try { 25 | log("Started f") 26 | delay(500) 27 | log("Slept 500 ms #1") 28 | delay(500) 29 | log("Slept 500 ms #2") 30 | delay(500) 31 | log("Slept 500 ms #3") 32 | delay(500) 33 | log("Slept 500 ms #4") 34 | delay(500) 35 | log("Slept 500 ms #5") 36 | } catch(e: Exception) { 37 | log("Aborting because of $e") 38 | } 39 | } 40 | Thread.sleep(1200) 41 | f.cancel(false) 42 | } 43 | -------------------------------------------------------------------------------- /kotlinx-coroutines-jdk8/src/test/kotlin/examples/ExplicitJob-example.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package examples 18 | 19 | import kotlinx.coroutines.experimental.Job 20 | import kotlinx.coroutines.experimental.delay 21 | import kotlinx.coroutines.experimental.future.future 22 | import java.util.concurrent.CancellationException 23 | 24 | fun main(args: Array) { 25 | val job = Job() 26 | log("Starting futures f && g") 27 | val f = future(job) { 28 | log("Started f") 29 | delay(500) 30 | log("f should not execute this line") 31 | } 32 | val g = future(job) { 33 | log("Started g") 34 | try { 35 | delay(500) 36 | } finally { 37 | log("g is executing finally!") 38 | } 39 | log("g should not execute this line") 40 | } 41 | log("Started futures f && g... will not wait -- cancel them!!!") 42 | job.cancel(CancellationException("I don't want it")) 43 | check(f.isCancelled) 44 | check(g.isCancelled) 45 | log("f result = ${Try { f.get() }}") 46 | log("g result = ${Try { g.get() }}") 47 | Thread.sleep(1000L) 48 | log("Nothing executed!") 49 | } 50 | -------------------------------------------------------------------------------- /kotlinx-coroutines-jdk8/src/test/kotlin/examples/ToFuture-example.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package examples 18 | 19 | import kotlinx.coroutines.experimental.CommonPool 20 | import kotlinx.coroutines.experimental.async 21 | import kotlinx.coroutines.experimental.delay 22 | import kotlinx.coroutines.experimental.future.asCompletableFuture 23 | import java.util.concurrent.TimeUnit 24 | 25 | fun main(args: Array) { 26 | log("Started") 27 | val deferred = async(CommonPool) { 28 | log("Busy...") 29 | delay(1, TimeUnit.SECONDS) 30 | log("Done...") 31 | 42 32 | } 33 | val future = deferred.asCompletableFuture() 34 | log("Got ${future.get()}") 35 | } -------------------------------------------------------------------------------- /kotlinx-coroutines-jdk8/src/test/kotlin/examples/Try.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package examples 18 | 19 | public class Try private constructor(private val _value: Any?) { 20 | private class Fail(val exception: Throwable) { 21 | override fun toString(): String = "Failure[$exception]" 22 | } 23 | 24 | public companion object { 25 | public operator fun invoke(block: () -> T): Try = 26 | try { Success(block()) } catch(e: Throwable) { Failure(e) } 27 | public fun Success(value: T) = Try(value) 28 | public fun Failure(exception: Throwable) = Try(Fail(exception)) 29 | } 30 | 31 | @Suppress("UNCHECKED_CAST") 32 | public val value: T get() = if (_value is Fail) throw _value.exception else _value as T 33 | 34 | public val exception: Throwable? get() = (_value as? Fail)?.exception 35 | 36 | override fun toString(): String = _value.toString() 37 | } 38 | -------------------------------------------------------------------------------- /kotlinx-coroutines-jdk8/src/test/kotlin/examples/log.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package examples 18 | 19 | import java.text.SimpleDateFormat 20 | import java.util.* 21 | 22 | fun log(msg: String) = println("${SimpleDateFormat("yyyyMMdd-HHmmss.sss").format(Date())} [${Thread.currentThread().name}] $msg") -------------------------------------------------------------------------------- /kotlinx-coroutines-jdk8/src/test/kotlin/examples/simple-example-1.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package examples 18 | 19 | import kotlinx.coroutines.experimental.future.await 20 | import kotlinx.coroutines.experimental.runBlocking 21 | import java.util.concurrent.CompletableFuture 22 | 23 | fun main(args: Array) { 24 | // Let's assume that we have a future coming from some 3rd party API 25 | val future: CompletableFuture = CompletableFuture.supplyAsync { 26 | Thread.sleep(1000L) // imitate some long-running computation, actually 27 | 42 28 | } 29 | // now let's launch a coroutine and await for this future inside it 30 | runBlocking { 31 | println("We can do something else, while we are waiting for future...") 32 | println("We've got ${future.await()} from the future!") 33 | } 34 | } -------------------------------------------------------------------------------- /kotlinx-coroutines-jdk8/src/test/kotlin/examples/simple-example-2.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package examples 18 | 19 | import kotlinx.coroutines.experimental.delay 20 | import kotlinx.coroutines.experimental.future.future 21 | import java.util.concurrent.CompletableFuture 22 | import java.util.concurrent.TimeUnit 23 | 24 | // this function returns a CompletableFuture using Kotlin coroutines 25 | fun supplyTheAnswerAsync(): CompletableFuture = future { 26 | println("We might be doing some asynchronous IO here or something else...") 27 | delay(1, TimeUnit.SECONDS) // just do a non-blocking delay 28 | 42 // The answer! 29 | } 30 | 31 | fun main(args: Array) { 32 | // We can use `supplyTheAnswerAsync` just like any other future-supplier function 33 | val future = supplyTheAnswerAsync() 34 | println("The answer is ${future.get()}") 35 | } -------------------------------------------------------------------------------- /kotlinx-coroutines-jdk8/src/test/kotlin/examples/simple-example-3.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package examples 18 | 19 | import kotlinx.coroutines.experimental.future.await 20 | import kotlinx.coroutines.experimental.future.future 21 | import java.util.concurrent.CompletableFuture 22 | 23 | fun main(args: Array) { 24 | // this example shows how easy it is to perform multiple async operations with coroutines 25 | val future = future { 26 | (1..5).map { // loops are no problem at all 27 | startLongAsyncOperation(it).await() // suspend while the long method is running 28 | }.joinToString("\n") 29 | } 30 | println("We have a long-running computation in background, let's wait for its result...") 31 | println(future.get()) 32 | } 33 | 34 | fun startLongAsyncOperation(num: Int): CompletableFuture = 35 | CompletableFuture.supplyAsync { 36 | Thread.sleep(1000L) // imitate some long-running computation, actually 37 | "$num" // and return a number converted to string 38 | } 39 | -------------------------------------------------------------------------------- /kotlinx-coroutines-jdk8/src/test/kotlin/examples/withTimeout-example.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package examples 18 | 19 | import kotlinx.coroutines.experimental.CancellationException 20 | import kotlinx.coroutines.experimental.delay 21 | import kotlinx.coroutines.experimental.future.await 22 | import kotlinx.coroutines.experimental.future.future 23 | import kotlinx.coroutines.experimental.withTimeout 24 | 25 | fun main(args: Array) { 26 | fun slow(s: String) = future { 27 | delay(500L) 28 | s 29 | } 30 | 31 | val f = future { 32 | log("Started f") 33 | val a = slow("A").await() 34 | log("a = $a") 35 | withTimeout(1000L) { 36 | val b = slow("B").await() 37 | log("b = $b") 38 | } 39 | try { 40 | withTimeout(750L) { 41 | val c = slow("C").await() 42 | log("c = $c") 43 | val d = slow("D").await() 44 | log("d = $d") 45 | } 46 | } catch (ex: CancellationException) { 47 | log("timed out with $ex") 48 | } 49 | val e = slow("E").await() 50 | log("e = $e") 51 | "done" 52 | } 53 | log("f.get() = ${f.get()}") 54 | } 55 | -------------------------------------------------------------------------------- /kotlinx-coroutines-nio/README.md: -------------------------------------------------------------------------------- 1 | # Module kotlinx-coroutines-nio 2 | 3 | Extensions for asynchronous IO on JDK7+. 4 | 5 | # Package kotlinx.coroutines.experimental.nio 6 | 7 | Extensions for asynchronous IO on JDK7+. 8 | 9 | * `AsynchronousFileChannel` extensions `aLock`, `aRead`, and `aWrite`. 10 | * `AsynchronousServerSocketChannel` extension `aAccept`. 11 | * `AsynchronousSocketChannel` extensions `aConnect`, `aRead`, and `aWrite`. 12 | -------------------------------------------------------------------------------- /kotlinx-coroutines-nio/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 19 | 20 | 4.0.0 21 | 22 | 23 | org.jetbrains.kotlinx 24 | kotlinx-coroutines 25 | 0.15-SNAPSHOT 26 | 27 | 28 | kotlinx-coroutines-nio 29 | jar 30 | 31 | 32 | src/main/kotlin 33 | src/test/kotlin 34 | 35 | 36 | 37 | 38 | org.jetbrains.kotlinx 39 | kotlinx-coroutines-core 40 | ${project.version} 41 | compile 42 | 43 | 44 | org.apache.commons 45 | commons-io 46 | 1.3.2 47 | test 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /kotlinx-coroutines-nio/src/test/kotlin/examples/log.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package examples 18 | 19 | import java.text.SimpleDateFormat 20 | import java.util.* 21 | 22 | fun log(msg: String) = println("${SimpleDateFormat("yyyyMMdd-HHmmss.sss").format(Date())} [${Thread.currentThread().name}] $msg") -------------------------------------------------------------------------------- /license/LICENSE.txt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | -------------------------------------------------------------------------------- /license/NOTICE.txt: -------------------------------------------------------------------------------- 1 | ========================================================================= 2 | == NOTICE file corresponding to the section 4 d of == 3 | == the Apache License, Version 2.0, == 4 | == in this case for the kotlix.coroutines library. == 5 | ========================================================================= 6 | 7 | kotlinx.coroutines library. 8 | Copyright 2016-2017 JetBrains s.r.o and respective authors and developers 9 | -------------------------------------------------------------------------------- /license/third_party/minima_LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Parker Moore 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /reactive/README.md: -------------------------------------------------------------------------------- 1 | # Coroutines for reactive streams 2 | 3 | This directory contains modules with utilities for various reactive stream libraries: 4 | 5 | ## Modules 6 | 7 | * [kotlinx-coroutines-reactive](kotlinx-coroutines-reactive) -- utilities for [Reactive Streams](http://www.reactive-streams.org) 8 | * [kotlinx-coroutines-reactor](kotlinx-coroutines-reactor) -- utilities for [Reactor](https://projectreactor.io) 9 | * [kotlinx-coroutines-rx1](kotlinx-coroutines-rx1) -- utilities for [RxJava 1.x](https://github.com/ReactiveX/RxJava/tree/1.x) 10 | * [kotlinx-coroutines-rx2](kotlinx-coroutines-rx2) -- utilities for [RxJava 2.x](https://github.com/ReactiveX/RxJava) 11 | -------------------------------------------------------------------------------- /reactive/kotlinx-coroutines-reactive/src/main/kotlin/kotlinx/coroutines/experimental/reactive/Convert.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kotlinx.coroutines.experimental.reactive 18 | 19 | import kotlinx.coroutines.experimental.channels.ReceiveChannel 20 | import org.reactivestreams.Publisher 21 | import kotlin.coroutines.experimental.CoroutineContext 22 | 23 | /** 24 | * Converts a stream of elements received from the channel to the hot reactive publisher. 25 | * 26 | * Every subscriber receives values from this channel in **fan-out** fashion. If the are multiple subscribers, 27 | * they'll receive values in round-robin way. 28 | * 29 | * @param context -- the coroutine context from which the resulting observable is going to be signalled 30 | */ 31 | public fun ReceiveChannel.asPublisher(context: CoroutineContext): Publisher = publish(context) { 32 | for (t in this@asPublisher) 33 | send(t) 34 | } 35 | 36 | /** 37 | * @suppress **Deprecated**: Renamed to [asPublisher] 38 | */ 39 | @Deprecated(message = "Renamed to `asPublisher`", 40 | replaceWith = ReplaceWith("asPublisher(context)")) 41 | public fun ReceiveChannel.toPublisher(context: CoroutineContext): Publisher = asPublisher(context) -------------------------------------------------------------------------------- /reactive/kotlinx-coroutines-reactive/src/test/kotlin/kotlinx/coroutines/experimental/reactive/PublisherCompletionStressTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kotlinx.coroutines.experimental.reactive 18 | 19 | import kotlinx.coroutines.experimental.CommonPool 20 | import kotlinx.coroutines.experimental.TestBase 21 | import kotlinx.coroutines.experimental.runBlocking 22 | import kotlinx.coroutines.experimental.withTimeout 23 | import org.junit.Test 24 | import java.util.* 25 | import kotlin.coroutines.experimental.CoroutineContext 26 | 27 | class PublisherCompletionStressTest : TestBase() { 28 | val N_REPEATS = 10_000 * stressTestMultiplier 29 | 30 | fun range(context: CoroutineContext, start: Int, count: Int) = publish(context) { 31 | for (x in start until start + count) send(x) 32 | } 33 | 34 | @Test 35 | fun testCompletion() { 36 | val rnd = Random() 37 | repeat(N_REPEATS) { 38 | val count = rnd.nextInt(5) 39 | runBlocking { 40 | withTimeout(5000) { 41 | var received = 0 42 | range(CommonPool, 1, count).consumeEach { x -> 43 | received++ 44 | if (x != received) error("$x != $received") 45 | } 46 | if (received != count) error("$received != $count") 47 | } 48 | } 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /reactive/kotlinx-coroutines-reactive/src/test/kotlin/kotlinx/coroutines/experimental/reactive/PublisherMultiTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kotlinx.coroutines.experimental.reactive 18 | 19 | import kotlinx.coroutines.experimental.CommonPool 20 | import kotlinx.coroutines.experimental.TestBase 21 | import kotlinx.coroutines.experimental.launch 22 | import kotlinx.coroutines.experimental.runBlocking 23 | import org.hamcrest.core.IsEqual 24 | import org.junit.Assert.assertThat 25 | import org.junit.Assert.assertTrue 26 | import org.junit.Test 27 | 28 | /** 29 | * Test emitting multiple values with [publish]. 30 | */ 31 | class PublisherMultiTest : TestBase() { 32 | @Test 33 | fun testConcurrentStress() = runBlocking { 34 | val n = 10_000 * stressTestMultiplier 35 | val observable = publish(CommonPool) { 36 | // concurrent emitters (many coroutines) 37 | val jobs = List(n) { 38 | // launch 39 | launch(CommonPool) { 40 | send(it) 41 | } 42 | } 43 | jobs.forEach { it.join() } 44 | } 45 | val resultSet = mutableSetOf() 46 | observable.consumeEach { 47 | assertTrue(resultSet.add(it)) 48 | } 49 | assertThat(resultSet.size, IsEqual(n)) 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /reactive/kotlinx-coroutines-reactor/src/main/kotlin/kotlinx/coroutines/experimental/reactor/Convert.kt: -------------------------------------------------------------------------------- 1 | package kotlinx.coroutines.experimental.reactor 2 | 3 | import kotlinx.coroutines.experimental.Deferred 4 | import kotlinx.coroutines.experimental.Job 5 | import kotlinx.coroutines.experimental.channels.ReceiveChannel 6 | import reactor.core.publisher.Flux 7 | import reactor.core.publisher.Mono 8 | import kotlin.coroutines.experimental.CoroutineContext 9 | 10 | /** 11 | * Converts this job to the hot reactive mono that signals 12 | * with [success][MonoSink.success] when the corresponding job completes. 13 | * 14 | * Every subscriber gets the signal at the same time. 15 | * Unsubscribing from the resulting mono **does not** affect the original job in any way. 16 | * 17 | * @param context -- the coroutine context from which the resulting mono is going to be signalled 18 | */ 19 | public fun Job.asMono(context: CoroutineContext): Mono = mono(context) { this@asMono.join() } 20 | 21 | /** 22 | * Converts this deferred value to the hot reactive mono that signals 23 | * [success][MonoSink.success] or [error][MonoSink.error]. 24 | * 25 | * Every subscriber gets the same completion value. 26 | * Unsubscribing from the resulting mono **does not** affect the original deferred value in any way. 27 | * 28 | * @param context -- the coroutine context from which the resulting mono is going to be signalled 29 | */ 30 | public fun Deferred.asMono(context: CoroutineContext): Mono = mono(context) { this@asMono.await() } 31 | 32 | /** 33 | * Converts a stream of elements received from the channel to the hot reactive flux. 34 | * 35 | * Every subscriber receives values from this channel in **fan-out** fashion. If the are multiple subscribers, 36 | * they'll receive values in round-robin way. 37 | * 38 | * @param context -- the coroutine context from which the resulting flux is going to be signalled 39 | */ 40 | public fun ReceiveChannel.asFlux(context: CoroutineContext): Flux = flux(context) { 41 | for (t in this@asFlux) 42 | send(t) 43 | } -------------------------------------------------------------------------------- /reactive/kotlinx-coroutines-reactor/src/main/kotlin/kotlinx/coroutines/experimental/reactor/Flux.kt: -------------------------------------------------------------------------------- 1 | package kotlinx.coroutines.experimental.reactor 2 | 3 | import kotlinx.coroutines.experimental.channels.ProducerScope 4 | import kotlinx.coroutines.experimental.reactive.publish 5 | import reactor.core.publisher.Flux 6 | import kotlin.coroutines.experimental.CoroutineContext 7 | 8 | /** 9 | * Creates cold reactive [Flux] that runs a given [block] in a coroutine. 10 | * Every time the returned flux is subscribed, it starts a new coroutine in the specified [context]. 11 | * Coroutine emits items with `send`. Unsubscribing cancels running coroutine. 12 | * 13 | * Invocations of `send` are suspended appropriately when subscribers apply back-pressure and to ensure that 14 | * `onNext` is not invoked concurrently. 15 | * 16 | * | **Coroutine action** | **Signal to subscriber** 17 | * | -------------------------------------------- | ------------------------ 18 | * | `send` | `onNext` 19 | * | Normal completion or `close` without cause | `onComplete` 20 | * | Failure with exception or `close` with cause | `onError` 21 | */ 22 | fun flux( 23 | context: CoroutineContext, 24 | block: suspend ProducerScope.() -> Unit 25 | ): Flux = Flux.from(publish(context, block)) 26 | -------------------------------------------------------------------------------- /reactive/kotlinx-coroutines-reactor/src/test/kotlin/kotlinx/coroutines/experimental/reactor/Check.kt: -------------------------------------------------------------------------------- 1 | package kotlinx.coroutines.experimental.reactor 2 | 3 | import reactor.core.publisher.Flux 4 | import reactor.core.publisher.Mono 5 | 6 | fun checkMonoValue( 7 | mono: Mono, 8 | checker: (T) -> Unit 9 | ) { 10 | val monoValue = mono.block() 11 | checker(monoValue) 12 | } 13 | 14 | fun checkErroneous( 15 | mono: Mono<*>, 16 | checker: (Throwable) -> Unit 17 | ) { 18 | try { 19 | mono.block() 20 | error("Should have failed") 21 | } catch (e: Throwable) { 22 | checker(e) 23 | } 24 | } 25 | 26 | fun checkSingleValue( 27 | flux: Flux, 28 | checker: (T) -> Unit 29 | ) { 30 | val singleValue = flux.toIterable().single() 31 | checker(singleValue) 32 | } 33 | 34 | fun checkErroneous( 35 | flux: Flux<*>, 36 | checker: (Throwable) -> Unit 37 | ) { 38 | val singleNotification = flux.materialize().toIterable().single() 39 | checker(singleNotification.throwable) 40 | } 41 | -------------------------------------------------------------------------------- /reactive/kotlinx-coroutines-reactor/src/test/kotlin/kotlinx/coroutines/experimental/reactor/FluxCompletionStressTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kotlinx.coroutines.experimental.reactor 18 | 19 | import kotlinx.coroutines.experimental.CommonPool 20 | import kotlinx.coroutines.experimental.TestBase 21 | import kotlinx.coroutines.experimental.reactive.consumeEach 22 | import kotlinx.coroutines.experimental.runBlocking 23 | import kotlinx.coroutines.experimental.withTimeout 24 | import org.junit.Test 25 | import java.util.Random 26 | import kotlin.coroutines.experimental.CoroutineContext 27 | 28 | class FluxCompletionStressTest : TestBase() { 29 | val N_REPEATS = 10_000 * stressTestMultiplier 30 | 31 | fun range(context: CoroutineContext, start: Int, count: Int) = flux(context) { 32 | for (x in start until start + count) send(x) 33 | } 34 | 35 | @Test 36 | fun testCompletion() { 37 | val rnd = Random() 38 | repeat(N_REPEATS) { 39 | val count = rnd.nextInt(5) 40 | runBlocking { 41 | withTimeout(5000) { 42 | var received = 0 43 | range(CommonPool, 1, count).consumeEach { x -> 44 | received++ 45 | if (x != received) error("$x != $received") 46 | } 47 | if (received != count) error("$received != $count") 48 | } 49 | } 50 | } 51 | } 52 | } -------------------------------------------------------------------------------- /reactive/kotlinx-coroutines-reactor/src/test/kotlin/kotlinx/coroutines/experimental/reactor/SchedulerTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kotlinx.coroutines.experimental.reactor 18 | 19 | import kotlinx.coroutines.experimental.TestBase 20 | import kotlinx.coroutines.experimental.delay 21 | import kotlinx.coroutines.experimental.run 22 | import kotlinx.coroutines.experimental.runBlocking 23 | import org.hamcrest.core.IsEqual 24 | import org.hamcrest.core.IsNot 25 | import org.junit.Assert.assertThat 26 | import org.junit.Test 27 | import reactor.core.scheduler.Schedulers 28 | 29 | class SchedulerTest : TestBase() { 30 | @Test 31 | fun testSingleScheduler(): Unit = runBlocking { 32 | expect(1) 33 | val mainThread = Thread.currentThread() 34 | run(Schedulers.single().asCoroutineDispatcher()) { 35 | val t1 = Thread.currentThread() 36 | println(t1) 37 | assertThat(t1, IsNot(IsEqual(mainThread))) 38 | expect(2) 39 | delay(100) 40 | val t2 = Thread.currentThread() 41 | println(t2) 42 | assertThat(t2, IsNot(IsEqual(mainThread))) 43 | expect(3) 44 | } 45 | finish(4) 46 | } 47 | } -------------------------------------------------------------------------------- /reactive/kotlinx-coroutines-rx1/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 19 | 20 | 4.0.0 21 | 22 | 23 | org.jetbrains.kotlinx 24 | kotlinx-coroutines 25 | 0.15-SNAPSHOT 26 | ../../pom.xml 27 | 28 | 29 | kotlinx-coroutines-rx1 30 | jar 31 | 32 | 33 | src/main/kotlin 34 | src/test/kotlin 35 | 36 | 37 | 38 | 39 | io.reactivex 40 | rxjava 41 | 1.2.6 42 | 43 | 44 | org.jetbrains.kotlinx 45 | kotlinx-coroutines-core 46 | ${project.version} 47 | compile 48 | 49 | 50 | org.jetbrains.kotlinx 51 | kotlinx-coroutines-core 52 | ${project.version} 53 | tests 54 | test 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /reactive/kotlinx-coroutines-rx1/src/test/kotlin/kotlinx/coroutines/experimental/rx1/Check.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kotlinx.coroutines.experimental.rx1 18 | 19 | import rx.Observable 20 | import rx.Single 21 | 22 | fun checkSingleValue( 23 | observable: Observable, 24 | checker: (T) -> Unit 25 | ) { 26 | val singleValue = observable.toBlocking().single() 27 | checker(singleValue) 28 | } 29 | 30 | fun checkErroneous( 31 | observable: Observable<*>, 32 | checker: (Throwable) -> Unit 33 | ) { 34 | val singleNotification = observable.materialize().toBlocking().single() 35 | checker(singleNotification.throwable) 36 | } 37 | 38 | fun checkSingleValue( 39 | single: Single, 40 | checker: (T) -> Unit 41 | ) { 42 | val singleValue = single.toBlocking().value() 43 | checker(singleValue) 44 | } 45 | 46 | fun checkErroneous( 47 | single: Single<*>, 48 | checker: (Throwable) -> Unit 49 | ) { 50 | try { 51 | single.toBlocking().value() 52 | error("Should have failed") 53 | } catch (e: Throwable) { 54 | checker(e) 55 | } 56 | } 57 | 58 | -------------------------------------------------------------------------------- /reactive/kotlinx-coroutines-rx1/src/test/kotlin/kotlinx/coroutines/experimental/rx1/ObservableCompletionStressTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kotlinx.coroutines.experimental.rx1 18 | 19 | import kotlinx.coroutines.experimental.CommonPool 20 | import kotlinx.coroutines.experimental.TestBase 21 | import kotlinx.coroutines.experimental.runBlocking 22 | import kotlinx.coroutines.experimental.withTimeout 23 | import org.junit.Test 24 | import java.util.* 25 | import kotlin.coroutines.experimental.CoroutineContext 26 | 27 | class ObservableCompletionStressTest : TestBase() { 28 | val N_REPEATS = 10_000 * stressTestMultiplier 29 | 30 | fun range(context: CoroutineContext, start: Int, count: Int) = rxObservable(context) { 31 | for (x in start until start + count) send(x) 32 | } 33 | 34 | @Test 35 | fun testCompletion() { 36 | val rnd = Random() 37 | repeat(N_REPEATS) { 38 | val count = rnd.nextInt(5) 39 | runBlocking { 40 | withTimeout(5000) { 41 | var received = 0 42 | range(CommonPool, 1, count).consumeEach { x -> 43 | received++ 44 | if (x != received) error("$x != $received") 45 | } 46 | if (received != count) error("$received != $count") 47 | } 48 | } 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /reactive/kotlinx-coroutines-rx2/src/main/kotlin/kotlinx/coroutines/experimental/rx2/RxFlowable.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kotlinx.coroutines.experimental.rx2 18 | 19 | import io.reactivex.Flowable 20 | import kotlinx.coroutines.experimental.channels.ProducerScope 21 | import kotlinx.coroutines.experimental.reactive.publish 22 | import kotlin.coroutines.experimental.CoroutineContext 23 | 24 | /** 25 | * Creates cold [flowable][Flowable] that will run a given [block] in a coroutine. 26 | * Every time the returned flowable is subscribed, it starts a new coroutine in the specified [context]. 27 | * Coroutine emits items with `send`. Unsubscribing cancels running coroutine. 28 | * 29 | * Invocations of `send` are suspended appropriately when subscribers apply back-pressure and to ensure that 30 | * `onNext` is not invoked concurrently. 31 | * 32 | * | **Coroutine action** | **Signal to subscriber** 33 | * | -------------------------------------------- | ------------------------ 34 | * | `send` | `onNext` 35 | * | Normal completion or `close` without cause | `onComplete` 36 | * | Failure with exception or `close` with cause | `onError` 37 | */ 38 | public fun rxFlowable( 39 | context: CoroutineContext, 40 | block: suspend ProducerScope.() -> Unit 41 | ): Flowable = Flowable.fromPublisher(publish(context, block)) 42 | -------------------------------------------------------------------------------- /reactive/kotlinx-coroutines-rx2/src/test/kotlin/guide/example-reactive-basic-01.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide-reactive.md by Knit tool. Do not edit. 18 | package guide.reactive.basic.example01 19 | 20 | import kotlinx.coroutines.experimental.* 21 | import kotlinx.coroutines.experimental.channels.* 22 | 23 | fun main(args: Array) = runBlocking { 24 | // create a channel that produces numbers from 1 to 3 with 200ms delays between them 25 | val source = produce(context) { 26 | println("Begin") // mark the beginning of this coroutine in output 27 | for (x in 1..3) { 28 | delay(200) // wait for 200ms 29 | send(x) // send number x to the channel 30 | } 31 | } 32 | // print elements from the source 33 | println("Elements:") 34 | source.consumeEach { // consume elements from it 35 | println(it) 36 | } 37 | // print elements from the source AGAIN 38 | println("Again:") 39 | source.consumeEach { // consume elements from it 40 | println(it) 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /reactive/kotlinx-coroutines-rx2/src/test/kotlin/guide/example-reactive-basic-02.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide-reactive.md by Knit tool. Do not edit. 18 | package guide.reactive.basic.example02 19 | 20 | import kotlinx.coroutines.experimental.* 21 | import kotlinx.coroutines.experimental.reactive.* 22 | 23 | fun main(args: Array) = runBlocking { 24 | // create a publisher that produces numbers from 1 to 3 with 200ms delays between them 25 | val source = publish(context) { 26 | // ^^^^^^^ <--- Difference from the previous examples is here 27 | println("Begin") // mark the beginning of this coroutine in output 28 | for (x in 1..3) { 29 | delay(200) // wait for 200ms 30 | send(x) // send number x to the channel 31 | } 32 | } 33 | // print elements from the source 34 | println("Elements:") 35 | source.consumeEach { // consume elements from it 36 | println(it) 37 | } 38 | // print elements from the source AGAIN 39 | println("Again:") 40 | source.consumeEach { // consume elements from it 41 | println(it) 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /reactive/kotlinx-coroutines-rx2/src/test/kotlin/guide/example-reactive-basic-03.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide-reactive.md by Knit tool. Do not edit. 18 | package guide.reactive.basic.example03 19 | 20 | import io.reactivex.* 21 | import kotlinx.coroutines.experimental.* 22 | import kotlinx.coroutines.experimental.reactive.* 23 | 24 | fun main(args: Array) = runBlocking { 25 | val source = Flowable.range(1, 5) // a range of five numbers 26 | .doOnSubscribe { println("OnSubscribe") } // provide some insight 27 | .doFinally { println("Finally") } // ... into what's going on 28 | var cnt = 0 29 | source.open().use { channel -> // open channel to the source 30 | for (x in channel) { // iterate over the channel to receive elements from it 31 | println(x) 32 | if (++cnt >= 3) break // break when 3 elements are printed 33 | } 34 | // `use` will close the channel when this block of code is complete 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /reactive/kotlinx-coroutines-rx2/src/test/kotlin/guide/example-reactive-basic-04.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide-reactive.md by Knit tool. Do not edit. 18 | package guide.reactive.basic.example04 19 | 20 | import io.reactivex.* 21 | import kotlinx.coroutines.experimental.* 22 | import kotlinx.coroutines.experimental.reactive.* 23 | 24 | fun main(args: Array) = runBlocking { 25 | val source = Flowable.range(1, 5) // a range of five numbers 26 | .doOnSubscribe { println("OnSubscribe") } // provide some insight 27 | .doFinally { println("Finally") } // ... into what's going on 28 | // iterate over the source fully 29 | source.consumeEach { println(it) } 30 | } 31 | -------------------------------------------------------------------------------- /reactive/kotlinx-coroutines-rx2/src/test/kotlin/guide/example-reactive-basic-05.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide-reactive.md by Knit tool. Do not edit. 18 | package guide.reactive.basic.example05 19 | 20 | import kotlinx.coroutines.experimental.* 21 | import kotlinx.coroutines.experimental.rx2.rxFlowable 22 | import io.reactivex.schedulers.Schedulers 23 | 24 | fun main(args: Array) = runBlocking { 25 | // coroutine -- fast producer of elements in the context of the main thread 26 | val source = rxFlowable(context) { 27 | for (x in 1..3) { 28 | send(x) // this is a suspending function 29 | println("Sent $x") // print after successfully sent item 30 | } 31 | } 32 | // subscribe on another thread with a slow subscriber using Rx 33 | source 34 | .observeOn(Schedulers.io(), false, 1) // specify buffer size of 1 item 35 | .doOnComplete { println("Complete") } 36 | .subscribe { x -> 37 | Thread.sleep(500) // 500ms to process each item 38 | println("Processed $x") 39 | } 40 | delay(2000) // suspend the main thread for a few seconds 41 | } 42 | -------------------------------------------------------------------------------- /reactive/kotlinx-coroutines-rx2/src/test/kotlin/guide/example-reactive-basic-06.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide-reactive.md by Knit tool. Do not edit. 18 | package guide.reactive.basic.example06 19 | 20 | import io.reactivex.subjects.BehaviorSubject 21 | 22 | fun main(args: Array) { 23 | val subject = BehaviorSubject.create() 24 | subject.onNext("one") 25 | subject.onNext("two") // updates the state of BehaviorSubject, "one" value is lost 26 | // now subscribe to this subject and print everything 27 | subject.subscribe(System.out::println) 28 | subject.onNext("three") 29 | subject.onNext("four") 30 | } 31 | -------------------------------------------------------------------------------- /reactive/kotlinx-coroutines-rx2/src/test/kotlin/guide/example-reactive-basic-07.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide-reactive.md by Knit tool. Do not edit. 18 | package guide.reactive.basic.example07 19 | 20 | import io.reactivex.subjects.BehaviorSubject 21 | import kotlinx.coroutines.experimental.launch 22 | import kotlinx.coroutines.experimental.runBlocking 23 | import kotlinx.coroutines.experimental.rx2.consumeEach 24 | 25 | fun main(args: Array) = runBlocking { 26 | val subject = BehaviorSubject.create() 27 | subject.onNext("one") 28 | subject.onNext("two") 29 | // now launch a coroutine to print everything 30 | launch(context) { // use the context of the main thread for a coroutine 31 | subject.consumeEach { println(it) } 32 | } 33 | subject.onNext("three") 34 | subject.onNext("four") 35 | } -------------------------------------------------------------------------------- /reactive/kotlinx-coroutines-rx2/src/test/kotlin/guide/example-reactive-basic-08.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide-reactive.md by Knit tool. Do not edit. 18 | package guide.reactive.basic.example08 19 | 20 | import io.reactivex.subjects.BehaviorSubject 21 | import kotlinx.coroutines.experimental.launch 22 | import kotlinx.coroutines.experimental.runBlocking 23 | import kotlinx.coroutines.experimental.rx2.consumeEach 24 | import kotlinx.coroutines.experimental.yield 25 | 26 | fun main(args: Array) = runBlocking { 27 | val subject = BehaviorSubject.create() 28 | subject.onNext("one") 29 | subject.onNext("two") 30 | // now launch a coroutine to print everything 31 | launch(context) { // use the context of the main thread for a coroutine 32 | subject.consumeEach { println(it) } 33 | } 34 | subject.onNext("three") 35 | yield() // yield the main thread to the launched coroutine <--- HERE 36 | subject.onNext("four") 37 | } 38 | -------------------------------------------------------------------------------- /reactive/kotlinx-coroutines-rx2/src/test/kotlin/guide/example-reactive-basic-09.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide-reactive.md by Knit tool. Do not edit. 18 | package guide.reactive.basic.example09 19 | 20 | import kotlinx.coroutines.experimental.channels.ConflatedBroadcastChannel 21 | import kotlinx.coroutines.experimental.channels.consumeEach 22 | import kotlinx.coroutines.experimental.launch 23 | import kotlinx.coroutines.experimental.runBlocking 24 | import kotlinx.coroutines.experimental.yield 25 | 26 | fun main(args: Array) = runBlocking { 27 | val broadcast = ConflatedBroadcastChannel() 28 | broadcast.offer("one") 29 | broadcast.offer("two") 30 | // now launch a coroutine to print everything 31 | launch(context) { // use the context of the main thread for a coroutine 32 | broadcast.consumeEach { println(it) } 33 | } 34 | broadcast.offer("three") 35 | yield() // yield the main thread to the launched coroutine 36 | broadcast.offer("four") 37 | } 38 | -------------------------------------------------------------------------------- /reactive/kotlinx-coroutines-rx2/src/test/kotlin/guide/example-reactive-context-01.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide-reactive.md by Knit tool. Do not edit. 18 | package guide.reactive.context.example01 19 | 20 | import io.reactivex.* 21 | import io.reactivex.functions.BiFunction 22 | import io.reactivex.schedulers.Schedulers 23 | import java.util.concurrent.TimeUnit 24 | 25 | fun rangeWithIntervalRx(scheduler: Scheduler, time: Long, start: Int, count: Int): Flowable = 26 | Flowable.zip( 27 | Flowable.range(start, count), 28 | Flowable.interval(time, TimeUnit.MILLISECONDS, scheduler), 29 | BiFunction { x, _ -> x }) 30 | 31 | fun main(args: Array) { 32 | rangeWithIntervalRx(Schedulers.computation(), 100, 1, 3) 33 | .subscribe { println("$it on thread ${Thread.currentThread().name}") } 34 | Thread.sleep(1000) 35 | } 36 | -------------------------------------------------------------------------------- /reactive/kotlinx-coroutines-rx2/src/test/kotlin/guide/example-reactive-context-02.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide-reactive.md by Knit tool. Do not edit. 18 | package guide.reactive.context.example02 19 | 20 | import io.reactivex.* 21 | import kotlinx.coroutines.experimental.* 22 | import kotlinx.coroutines.experimental.reactive.* 23 | import kotlin.coroutines.experimental.CoroutineContext 24 | 25 | fun rangeWithInterval(context: CoroutineContext, time: Long, start: Int, count: Int) = publish(context) { 26 | for (x in start until start + count) { 27 | delay(time) // wait before sending each number 28 | send(x) 29 | } 30 | } 31 | 32 | fun main(args: Array) { 33 | Flowable.fromPublisher(rangeWithInterval(CommonPool, 100, 1, 3)) 34 | .subscribe { println("$it on thread ${Thread.currentThread().name}") } 35 | Thread.sleep(1000) 36 | } 37 | -------------------------------------------------------------------------------- /reactive/kotlinx-coroutines-rx2/src/test/kotlin/guide/example-reactive-context-03.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide-reactive.md by Knit tool. Do not edit. 18 | package guide.reactive.context.example03 19 | 20 | import io.reactivex.* 21 | import kotlinx.coroutines.experimental.* 22 | import kotlinx.coroutines.experimental.reactive.* 23 | import io.reactivex.schedulers.Schedulers 24 | import kotlin.coroutines.experimental.CoroutineContext 25 | 26 | fun rangeWithInterval(context: CoroutineContext, time: Long, start: Int, count: Int) = publish(context) { 27 | for (x in start until start + count) { 28 | delay(time) // wait before sending each number 29 | send(x) 30 | } 31 | } 32 | 33 | fun main(args: Array) { 34 | Flowable.fromPublisher(rangeWithInterval(CommonPool, 100, 1, 3)) 35 | .observeOn(Schedulers.computation()) // <-- THIS LINE IS ADDED 36 | .subscribe { println("$it on thread ${Thread.currentThread().name}") } 37 | Thread.sleep(1000) 38 | } 39 | -------------------------------------------------------------------------------- /reactive/kotlinx-coroutines-rx2/src/test/kotlin/guide/example-reactive-context-04.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide-reactive.md by Knit tool. Do not edit. 18 | package guide.reactive.context.example04 19 | 20 | import io.reactivex.* 21 | import kotlinx.coroutines.experimental.* 22 | import kotlinx.coroutines.experimental.reactive.* 23 | import io.reactivex.functions.BiFunction 24 | import io.reactivex.schedulers.Schedulers 25 | import java.util.concurrent.TimeUnit 26 | 27 | fun rangeWithIntervalRx(scheduler: Scheduler, time: Long, start: Int, count: Int): Flowable = 28 | Flowable.zip( 29 | Flowable.range(start, count), 30 | Flowable.interval(time, TimeUnit.MILLISECONDS, scheduler), 31 | BiFunction { x, _ -> x }) 32 | 33 | fun main(args: Array) = runBlocking { 34 | rangeWithIntervalRx(Schedulers.computation(), 100, 1, 3) 35 | .consumeEach { println("$it on thread ${Thread.currentThread().name}") } 36 | } 37 | -------------------------------------------------------------------------------- /reactive/kotlinx-coroutines-rx2/src/test/kotlin/guide/example-reactive-context-05.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide-reactive.md by Knit tool. Do not edit. 18 | package guide.reactive.context.example05 19 | 20 | import io.reactivex.* 21 | import kotlinx.coroutines.experimental.* 22 | import kotlinx.coroutines.experimental.reactive.* 23 | import io.reactivex.functions.BiFunction 24 | import io.reactivex.schedulers.Schedulers 25 | import java.util.concurrent.TimeUnit 26 | 27 | fun rangeWithIntervalRx(scheduler: Scheduler, time: Long, start: Int, count: Int): Flowable = 28 | Flowable.zip( 29 | Flowable.range(start, count), 30 | Flowable.interval(time, TimeUnit.MILLISECONDS, scheduler), 31 | BiFunction { x, _ -> x }) 32 | 33 | fun main(args: Array) = runBlocking { 34 | val job = launch(Unconfined) { // launch new coroutine in Unconfined context (without its own thread pool) 35 | rangeWithIntervalRx(Schedulers.computation(), 100, 1, 3) 36 | .consumeEach { println("$it on thread ${Thread.currentThread().name}") } 37 | } 38 | job.join() // wait for our coroutine to complete 39 | } 40 | -------------------------------------------------------------------------------- /reactive/kotlinx-coroutines-rx2/src/test/kotlin/guide/example-reactive-operators-01.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide-reactive.md by Knit tool. Do not edit. 18 | package guide.reactive.operators.example01 19 | 20 | import kotlinx.coroutines.experimental.* 21 | import kotlinx.coroutines.experimental.reactive.* 22 | import kotlin.coroutines.experimental.CoroutineContext 23 | 24 | fun range(context: CoroutineContext, start: Int, count: Int) = publish(context) { 25 | for (x in start until start + count) send(x) 26 | } 27 | 28 | fun main(args: Array) = runBlocking { 29 | range(CommonPool, 1, 5).consumeEach { println(it) } 30 | } 31 | -------------------------------------------------------------------------------- /reactive/kotlinx-coroutines-rx2/src/test/kotlin/guide/example-reactive-operators-02.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide-reactive.md by Knit tool. Do not edit. 18 | package guide.reactive.operators.example02 19 | 20 | import kotlinx.coroutines.experimental.* 21 | import kotlinx.coroutines.experimental.reactive.* 22 | import org.reactivestreams.Publisher 23 | import kotlin.coroutines.experimental.CoroutineContext 24 | 25 | fun Publisher.fusedFilterMap( 26 | context: CoroutineContext, // the context to execute this coroutine in 27 | predicate: (T) -> Boolean, // the filter predicate 28 | mapper: (T) -> R // the mapper function 29 | ) = publish(context) { 30 | consumeEach { // consume the source stream 31 | if (predicate(it)) // filter part 32 | send(mapper(it)) // map part 33 | } 34 | } 35 | 36 | fun range(context: CoroutineContext, start: Int, count: Int) = publish(context) { 37 | for (x in start until start + count) send(x) 38 | } 39 | 40 | fun main(args: Array) = runBlocking { 41 | range(context, 1, 5) 42 | .fusedFilterMap(context, { it % 2 == 0}, { "$it is even" }) 43 | .consumeEach { println(it) } // print all the resulting strings 44 | } 45 | -------------------------------------------------------------------------------- /reactive/kotlinx-coroutines-rx2/src/test/kotlin/guide/example-reactive-operators-04.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | // This file was automatically generated from coroutines-guide-reactive.md by Knit tool. Do not edit. 18 | package guide.reactive.operators.example04 19 | 20 | import kotlinx.coroutines.experimental.* 21 | import kotlinx.coroutines.experimental.reactive.* 22 | import org.reactivestreams.Publisher 23 | import kotlin.coroutines.experimental.CoroutineContext 24 | 25 | fun Publisher>.merge(context: CoroutineContext) = publish(context) { 26 | consumeEach { pub -> // for each publisher received on the source channel 27 | launch(this.context) { // launch a child coroutine 28 | pub.consumeEach { send(it) } // resend all element from this publisher 29 | } 30 | } 31 | } 32 | 33 | fun rangeWithInterval(context: CoroutineContext, time: Long, start: Int, count: Int) = publish(context) { 34 | for (x in start until start + count) { 35 | delay(time) // wait before sending each number 36 | send(x) 37 | } 38 | } 39 | 40 | fun testPub(context: CoroutineContext) = publish>(context) { 41 | send(rangeWithInterval(context, 250, 1, 4)) // number 1 at 250ms, 2 at 500ms, 3 at 750ms, 4 at 1000ms 42 | delay(100) // wait for 100 ms 43 | send(rangeWithInterval(context, 500, 11, 3)) // number 11 at 600ms, 12 at 1100ms, 13 at 1600ms 44 | delay(1100) // wait for 1.1s - done in 1.2 sec after start 45 | } 46 | 47 | fun main(args: Array) = runBlocking { 48 | testPub(context).merge(context).consumeEach { println(it) } // print the whole stream 49 | } 50 | -------------------------------------------------------------------------------- /reactive/kotlinx-coroutines-rx2/src/test/kotlin/kotlinx/coroutines/experimental/rx2/Check.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kotlinx.coroutines.experimental.rx2 18 | 19 | import io.reactivex.Maybe 20 | import io.reactivex.Observable 21 | import io.reactivex.Single 22 | 23 | fun checkSingleValue( 24 | observable: Observable, 25 | checker: (T) -> Unit 26 | ) { 27 | val singleValue = observable.blockingSingle() 28 | checker(singleValue) 29 | } 30 | 31 | fun checkErroneous( 32 | observable: Observable<*>, 33 | checker: (Throwable) -> Unit 34 | ) { 35 | val singleNotification = observable.materialize().blockingSingle() 36 | checker(singleNotification.error) 37 | } 38 | 39 | fun checkSingleValue( 40 | single: Single, 41 | checker: (T) -> Unit 42 | ) { 43 | val singleValue = single.blockingGet() 44 | checker(singleValue) 45 | } 46 | 47 | fun checkErroneous( 48 | single: Single<*>, 49 | checker: (Throwable) -> Unit 50 | ) { 51 | try { 52 | single.blockingGet() 53 | error("Should have failed") 54 | } catch (e: Throwable) { 55 | checker(e) 56 | } 57 | } 58 | 59 | fun checkMaybeValue( 60 | maybe: Maybe, 61 | checker: (T?) -> Unit 62 | ) { 63 | val maybeValue = maybe.toFlowable().blockingIterable().firstOrNull() 64 | checker(maybeValue) 65 | } 66 | 67 | fun checkErroneous( 68 | maybe: Maybe<*>, 69 | checker: (Throwable) -> Unit 70 | ) { 71 | try { 72 | (maybe as Maybe).blockingGet() 73 | error("Should have failed") 74 | } catch (e: Throwable) { 75 | checker(e) 76 | } 77 | } 78 | 79 | -------------------------------------------------------------------------------- /reactive/kotlinx-coroutines-rx2/src/test/kotlin/kotlinx/coroutines/experimental/rx2/ObservableCompletionStressTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kotlinx.coroutines.experimental.rx2 18 | 19 | import kotlinx.coroutines.experimental.CommonPool 20 | import kotlinx.coroutines.experimental.TestBase 21 | import kotlinx.coroutines.experimental.runBlocking 22 | import kotlinx.coroutines.experimental.withTimeout 23 | import org.junit.Test 24 | import java.util.* 25 | import kotlin.coroutines.experimental.CoroutineContext 26 | 27 | class ObservableCompletionStressTest : TestBase() { 28 | val N_REPEATS = 10_000 * stressTestMultiplier 29 | 30 | fun range(context: CoroutineContext, start: Int, count: Int) = rxObservable(context) { 31 | for (x in start until start + count) send(x) 32 | } 33 | 34 | @Test 35 | fun testCompletion() { 36 | val rnd = Random() 37 | repeat(N_REPEATS) { 38 | val count = rnd.nextInt(5) 39 | runBlocking { 40 | withTimeout(5000) { 41 | var received = 0 42 | range(CommonPool, 1, count).consumeEach { x -> 43 | received++ 44 | if (x != received) error("$x != $received") 45 | } 46 | if (received != count) error("$received != $count") 47 | } 48 | } 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /reactive/kotlinx-coroutines-rx2/src/test/kotlin/kotlinx/coroutines/experimental/rx2/SchedulerTest.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-2017 JetBrains s.r.o. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kotlinx.coroutines.experimental.rx2 18 | 19 | import io.reactivex.schedulers.Schedulers 20 | import kotlinx.coroutines.experimental.* 21 | import org.hamcrest.core.IsEqual 22 | import org.hamcrest.core.IsNot 23 | import org.junit.Assert.assertThat 24 | import org.junit.Test 25 | 26 | class SchedulerTest : TestBase() { 27 | @Test 28 | fun testIoScheduler(): Unit = runBlocking { 29 | expect(1) 30 | val mainThread = Thread.currentThread() 31 | run(Schedulers.io().asCoroutineDispatcher()) { 32 | val t1 = Thread.currentThread() 33 | println(t1) 34 | assertThat(t1, IsNot(IsEqual(mainThread))) 35 | expect(2) 36 | delay(100) 37 | val t2 = Thread.currentThread() 38 | println(t2) 39 | assertThat(t2, IsNot(IsEqual(mainThread))) 40 | expect(3) 41 | } 42 | finish(4) 43 | } 44 | } -------------------------------------------------------------------------------- /site/README.md: -------------------------------------------------------------------------------- 1 | # Reference documentation site 2 | 3 | This module builds references documentation. 4 | 5 | ## Building 6 | 7 | * Install [Jekyll](http://jekyllrb.com) 8 | * In project root directory do: 9 | * Run `mvn clean` 10 | * Run `mvn compile` 11 | * Run `mvn site` 12 | * The result is in `target/_site` 13 | * Upload it to github pages (`gh-pages` branch) 14 | -------------------------------------------------------------------------------- /site/docs/Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | ruby RUBY_VERSION 3 | 4 | # Hello! This is where you manage which Jekyll version is used to run. 5 | # When you want to use a different version, change it below, save the 6 | # file and run `bundle install`. Run Jekyll with `bundle exec`, like so: 7 | # 8 | # bundle exec jekyll serve 9 | # 10 | # This will help ensure the proper Jekyll version is running. 11 | # Happy Jekylling! 12 | 13 | gem "jekyll", "3.4.0" 14 | 15 | # Windows does not include zoneinfo files, so bundle the tzinfo-data gem 16 | gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] 17 | -------------------------------------------------------------------------------- /site/docs/Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | addressable (2.5.0) 5 | public_suffix (~> 2.0, >= 2.0.2) 6 | colorator (1.1.0) 7 | ffi (1.9.17) 8 | ffi (1.9.17-x64-mingw32) 9 | forwardable-extended (2.6.0) 10 | jekyll (3.4.0) 11 | addressable (~> 2.4) 12 | colorator (~> 1.0) 13 | jekyll-sass-converter (~> 1.0) 14 | jekyll-watch (~> 1.1) 15 | kramdown (~> 1.3) 16 | liquid (~> 3.0) 17 | mercenary (~> 0.3.3) 18 | pathutil (~> 0.9) 19 | rouge (~> 1.7) 20 | safe_yaml (~> 1.0) 21 | jekyll-sass-converter (1.5.0) 22 | sass (~> 3.4) 23 | jekyll-watch (1.5.0) 24 | listen (~> 3.0, < 3.1) 25 | kramdown (1.13.2) 26 | liquid (3.0.6) 27 | listen (3.0.8) 28 | rb-fsevent (~> 0.9, >= 0.9.4) 29 | rb-inotify (~> 0.9, >= 0.9.7) 30 | mercenary (0.3.6) 31 | pathutil (0.14.0) 32 | forwardable-extended (~> 2.6) 33 | public_suffix (2.0.5) 34 | rb-fsevent (0.9.8) 35 | rb-inotify (0.9.8) 36 | ffi (>= 0.5.0) 37 | rouge (1.11.1) 38 | safe_yaml (1.0.4) 39 | sass (3.4.23) 40 | thread_safe (0.3.5) 41 | tzinfo (1.2.2) 42 | thread_safe (~> 0.1) 43 | tzinfo-data (1.2016.10) 44 | tzinfo (>= 1.0.0) 45 | 46 | PLATFORMS 47 | ruby 48 | x64-mingw32 49 | 50 | DEPENDENCIES 51 | jekyll (= 3.4.0) 52 | tzinfo-data 53 | 54 | RUBY VERSION 55 | ruby 2.2.6p396 56 | 57 | BUNDLED WITH 58 | 1.14.3 59 | -------------------------------------------------------------------------------- /site/docs/_config.yml: -------------------------------------------------------------------------------- 1 | # Jekyll configuration file 2 | title: kotlinx.coroutines 3 | description: Library support for kotlin coroutines 4 | baseurl: "/kotlinx.coroutines" 5 | url: "https://kotlin.github.io" 6 | 7 | # Build settings 8 | markdown: kramdown 9 | exclude: 10 | - Gemfile 11 | - Gemfile.lock 12 | -------------------------------------------------------------------------------- /site/docs/_includes/footer.html: -------------------------------------------------------------------------------- 1 |
2 | 3 |
4 | -------------------------------------------------------------------------------- /site/docs/_includes/head.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {% if page.title %}{{ page.title | escape }}{% else %}{{ site.title | escape }}{% endif %} 6 | 7 | 8 | 9 | 17 | 18 | -------------------------------------------------------------------------------- /site/docs/_includes/header.html: -------------------------------------------------------------------------------- 1 | 6 | -------------------------------------------------------------------------------- /site/docs/_layouts/api.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | {% include head.html %} 4 | 5 | {% include header.html %} 6 |
7 |
8 | {{ content }} 9 |
10 |
11 | {% include footer.html %} 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /site/docs/_sass/_layout.scss: -------------------------------------------------------------------------------- 1 | // Bits and pieces from Minima Jekyll Layout 2 | // The MIT License (MIT) Copyright (c) 2016 Parker Moor 3 | 4 | // Site header 5 | .site-header { 6 | border-top: 5px solid $grey-color-dark; 7 | border-bottom: 1px solid $grey-color-light; 8 | min-height: 56px; 9 | 10 | // Positioning context for the mobile navigation icon 11 | position: relative; 12 | } 13 | 14 | .site-title { 15 | font-size: 26px; 16 | font-weight: 300; 17 | line-height: 56px; 18 | letter-spacing: -1px; 19 | margin-bottom: 0; 20 | float: left; 21 | 22 | &, 23 | &:visited { 24 | color: $grey-color-dark; 25 | } 26 | } 27 | // Site footer 28 | .site-footer { 29 | border-top: 1px solid $grey-color-light; 30 | padding: $spacing-unit 0; 31 | } 32 | 33 | // Page content 34 | .page-content { 35 | padding: $spacing-unit 0; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /site/docs/_sass/_minima.scss: -------------------------------------------------------------------------------- 1 | // Bits and pieces from Minima Jekyll Layout 2 | // The MIT License (MIT) Copyright (c) 2016 Parker Moor 3 | 4 | // Define defaults for each variable. 5 | 6 | $base-font-family: "Helvetica Neue", Helvetica, Arial, sans-serif !default; 7 | $base-font-size: 16px !default; 8 | $base-font-weight: 400 !default; 9 | $small-font-size: $base-font-size * 0.875 !default; 10 | $base-line-height: 1.5 !default; 11 | 12 | $spacing-unit: 30px !default; 13 | 14 | $text-color: #111 !default; 15 | $background-color: #fdfdfd !default; 16 | $brand-color: #2a7ae2 !default; 17 | 18 | $grey-color: #828282 !default; 19 | $grey-color-light: lighten($grey-color, 40%) !default; 20 | $grey-color-dark: darken($grey-color, 25%) !default; 21 | 22 | // Width of the content area 23 | $content-width: 800px !default; 24 | 25 | $on-palm: 600px !default; 26 | $on-laptop: 800px !default; 27 | 28 | @mixin media-query($device) { 29 | @media screen and (max-width: $device) { 30 | @content; 31 | } 32 | } 33 | 34 | // Import partials. 35 | @import "base", "layout"; 36 | -------------------------------------------------------------------------------- /site/docs/assets/main.scss: -------------------------------------------------------------------------------- 1 | --- 2 | # Only the main Sass file needs front matter (the dashes are enough) 3 | --- 4 | @charset "utf-8"; 5 | 6 | // Sans Serif 7 | @import url('//fonts.googleapis.com/css?family=Open+Sans:300,300italic,400italic,700italic,400,700'); 8 | 9 | // Our variables 10 | $base-font-family: "Open Sans", Helvetica, Arial, sans-serif; 11 | $base-font-size: 14px; 12 | $base-font-weight: 400; 13 | $small-font-size: $base-font-size * 0.875; 14 | $base-line-height: 20px; 15 | 16 | $spacing-unit: 30px; 17 | 18 | $text-color: #111; 19 | $background-color: #fdfdfd; 20 | $brand-color: #2a7ae2; 21 | 22 | $grey-color: #828282; 23 | $grey-color-light: lighten($grey-color, 40%); 24 | $grey-color-dark: darken($grey-color, 25%); 25 | 26 | // Width of the content area 27 | $content-width: 800px; 28 | 29 | $on-palm: 600px; 30 | $on-laptop: 800px; 31 | 32 | // Import partials from the `minima` theme. 33 | @import "minima"; 34 | 35 | // Import api reference styles 36 | @import "api"; 37 | -------------------------------------------------------------------------------- /site/docs/index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: kotlinx-coroutines 3 | layout: api 4 | --- 5 | 6 | # kotlinx.coroutines reference documentation 7 | 8 | Library support for Kotlin coroutines. This reference is a companion to 9 | [Guide to kotlinx.coroutines by example](https://github.com/Kotlin/kotlinx.coroutines/blob/master/coroutines-guide.md). 10 | 11 | ## Modules 12 | 13 | [kotlinx-coroutines-core](kotlinx-coroutines-core) | Core primitives to work with coroutines 14 | [kotlinx-coroutines-jdk8](kotlinx-coroutines-jdk8) | Additional libraries for JDK8 (or Android API level 24) 15 | [kotlinx-coroutines-nio](kotlinx-coroutines-nio) | Extensions for asynchronous IO on JDK7+ 16 | [kotlinx-coroutines-reactive](kotlinx-coroutines-reactive) | Utilities for [Reactive Streams](http://www.reactive-streams.org) 17 | [kotlinx-coroutines-rx1](kotlinx-coroutines-rx1) | Utilities for [RxJava 1.x](https://github.com/ReactiveX/RxJava/tree/1.x) 18 | [kotlinx-coroutines-rx2](kotlinx-coroutines-rx2) | Utilities for [RxJava 2.x](https://github.com/ReactiveX/RxJava) 19 | [kotlinx-coroutines-android](kotlinx-coroutines-android) | `UI` context for Android applications 20 | [kotlinx-coroutines-javafx](kotlinx-coroutines-javafx) | `JavaFx` context for JavaFX UI applications 21 | [kotlinx-coroutines-swing](kotlinx-coroutines-swing) | `Swing` context for Swing UI applications 22 | -------------------------------------------------------------------------------- /ui/README.md: -------------------------------------------------------------------------------- 1 | # Coroutines for UI 2 | 3 | This directory contains modules for coroutine programming with various single-threaded UI libraries: 4 | 5 | ## Modules 6 | 7 | * [kotlinx-coroutines-android](kotlinx-coroutines-android) -- `UI` context for Android applications. 8 | * [kotlinx-coroutines-javafx](kotlinx-coroutines-javafx) -- `JavaFx` context for JavaFX UI applications. 9 | * [kotlinx-coroutines-swing](kotlinx-coroutines-swing) -- `Swing` context for Swing UI applications. 10 | -------------------------------------------------------------------------------- /ui/kotlinx-coroutines-android/README.md: -------------------------------------------------------------------------------- 1 | # Module kotlinx-coroutines-android 2 | 3 | Provides `UI` context for Android applications. 4 | 5 | Read [Guide to UI programming with coroutines](https://github.com/Kotlin/kotlinx.coroutines/blob/master/ui/coroutines-guide-ui.md) 6 | for tutorial on this module. 7 | 8 | # Package kotlinx.coroutines.experimental.android 9 | 10 | Provides `UI` context for Android applications. 11 | -------------------------------------------------------------------------------- /ui/kotlinx-coroutines-android/example-app/.gitignore: -------------------------------------------------------------------------------- 1 | local.properties 2 | .gradle 3 | .idea 4 | build 5 | example-app.iml 6 | app/build 7 | app/app.iml 8 | -------------------------------------------------------------------------------- /ui/kotlinx-coroutines-android/example-app/app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | 5 | android { 6 | compileSdkVersion 25 7 | buildToolsVersion "25.0.2" 8 | defaultConfig { 9 | applicationId "com.example.app" 10 | minSdkVersion 9 11 | targetSdkVersion 25 12 | versionCode 1 13 | versionName "1.0" 14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 15 | } 16 | buildTypes { 17 | release { 18 | minifyEnabled false 19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 20 | } 21 | } 22 | } 23 | 24 | repositories { 25 | mavenLocal() 26 | mavenCentral() 27 | } 28 | 29 | dependencies { 30 | compile fileTree(dir: 'libs', include: ['*.jar']) 31 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 32 | exclude group: 'com.android.support', module: 'support-annotations' 33 | }) 34 | compile 'com.android.support:appcompat-v7:25.2.0' 35 | compile 'com.android.support.constraint:constraint-layout:1.0.1' 36 | compile 'com.android.support:design:25.2.0' 37 | testCompile 'junit:junit:4.12' 38 | compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" 39 | compile "org.jetbrains.kotlinx:kotlinx-coroutines-android:0.15" 40 | } 41 | 42 | kotlin { 43 | experimental { 44 | coroutines "enable" 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /ui/kotlinx-coroutines-android/example-app/app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in C:\Users\roman\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /ui/kotlinx-coroutines-android/example-app/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /ui/kotlinx-coroutines-android/example-app/app/src/main/java/com/example/app/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.example.app 2 | 3 | import android.os.Bundle 4 | import android.support.design.widget.FloatingActionButton 5 | import android.support.v7.app.AppCompatActivity 6 | import android.view.Menu 7 | import android.view.MenuItem 8 | import android.widget.TextView 9 | import kotlinx.android.synthetic.main.activity_main.* 10 | import kotlinx.android.synthetic.main.content_main.* 11 | 12 | class MainActivity : AppCompatActivity() { 13 | override fun onCreate(savedInstanceState: Bundle?) { 14 | super.onCreate(savedInstanceState) 15 | setContentView(R.layout.activity_main) 16 | setSupportActionBar(toolbar) 17 | setup(hello, fab) 18 | } 19 | 20 | override fun onCreateOptionsMenu(menu: Menu): Boolean { 21 | // Inflate the menu; this adds items to the action bar if it is present. 22 | menuInflater.inflate(R.menu.menu_main, menu) 23 | return true 24 | } 25 | 26 | override fun onOptionsItemSelected(item: MenuItem): Boolean { 27 | // Handle action bar item clicks here. The action bar will 28 | // automatically handle clicks on the Home/Up button, so long 29 | // as you specify a parent activity in AndroidManifest.xml. 30 | val id = item.itemId 31 | if (id == R.id.action_settings) return true 32 | return super.onOptionsItemSelected(item) 33 | } 34 | } 35 | 36 | fun setup(hello: TextView, fab: FloatingActionButton) { 37 | // placeholder 38 | } 39 | -------------------------------------------------------------------------------- /ui/kotlinx-coroutines-android/example-app/app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 13 | 14 | 20 | 21 | 22 | 23 | 24 | 25 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /ui/kotlinx-coroutines-android/example-app/app/src/main/res/layout/content_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /ui/kotlinx-coroutines-android/example-app/app/src/main/res/menu/menu_main.xml: -------------------------------------------------------------------------------- 1 | 5 | 10 | 11 | -------------------------------------------------------------------------------- /ui/kotlinx-coroutines-android/example-app/app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pljp/kotlinx.coroutines/52e15006d31e779aaeadba053dcda1afd0198240/ui/kotlinx-coroutines-android/example-app/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /ui/kotlinx-coroutines-android/example-app/app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pljp/kotlinx.coroutines/52e15006d31e779aaeadba053dcda1afd0198240/ui/kotlinx-coroutines-android/example-app/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /ui/kotlinx-coroutines-android/example-app/app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pljp/kotlinx.coroutines/52e15006d31e779aaeadba053dcda1afd0198240/ui/kotlinx-coroutines-android/example-app/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /ui/kotlinx-coroutines-android/example-app/app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pljp/kotlinx.coroutines/52e15006d31e779aaeadba053dcda1afd0198240/ui/kotlinx-coroutines-android/example-app/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /ui/kotlinx-coroutines-android/example-app/app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pljp/kotlinx.coroutines/52e15006d31e779aaeadba053dcda1afd0198240/ui/kotlinx-coroutines-android/example-app/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /ui/kotlinx-coroutines-android/example-app/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pljp/kotlinx.coroutines/52e15006d31e779aaeadba053dcda1afd0198240/ui/kotlinx-coroutines-android/example-app/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /ui/kotlinx-coroutines-android/example-app/app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pljp/kotlinx.coroutines/52e15006d31e779aaeadba053dcda1afd0198240/ui/kotlinx-coroutines-android/example-app/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /ui/kotlinx-coroutines-android/example-app/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pljp/kotlinx.coroutines/52e15006d31e779aaeadba053dcda1afd0198240/ui/kotlinx-coroutines-android/example-app/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /ui/kotlinx-coroutines-android/example-app/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pljp/kotlinx.coroutines/52e15006d31e779aaeadba053dcda1afd0198240/ui/kotlinx-coroutines-android/example-app/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /ui/kotlinx-coroutines-android/example-app/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pljp/kotlinx.coroutines/52e15006d31e779aaeadba053dcda1afd0198240/ui/kotlinx-coroutines-android/example-app/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /ui/kotlinx-coroutines-android/example-app/app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /ui/kotlinx-coroutines-android/example-app/app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 16dp 3 | 4 | -------------------------------------------------------------------------------- /ui/kotlinx-coroutines-android/example-app/app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | ExampleApp 3 | Settings 4 | 5 | -------------------------------------------------------------------------------- /ui/kotlinx-coroutines-android/example-app/app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 15 | 16 |