commit | d8dc69f1a0281761b957b06a81878fba8d189081 | [log] [tgz] |
---|---|---|
author | Denis Zharkov <denis.zharkov@jetbrains.com> | Wed Dec 21 17:24:19 2016 +0300 |
committer | Denis Zharkov <denis.zharkov@jetbrains.com> | Wed Dec 21 17:24:19 2016 +0300 |
tree | 88ce4c70acf7994a2e4a69310b9bb28f7caddb2a | |
parent | e469d5f185639aac038b787ef95b82a84c157c9c [diff] |
Use 1.1-M04 as kotlin.version Also put resulting artifacts into kotlin-eap-1.1
Three libraries built upon Kotlin coroutines:
kotlinx-coroutines-async
with convenient interfaces/wrappers to commonly used asynchronous API shipped with standard JDK, namely promise-like CompletableFuture
and asynchronous channels from java.nio
packagekotlinx-coroutines-generate
provides ability to create Sequence
objects generated by coroutine body containing yield
suspension pointskotlinx-coroutines-rx
allows to use Observable
objects from RxJava inside a coroutine body to suspend on themimport kotlinx.coroutines.async import java.util.concurrent.CompletableFuture private fun startLongAsyncOperation(v: Int) = CompletableFuture.supplyAsync { Thread.sleep(1000) "Result: $v" } fun main(args: Array<String>) { val future = async { (1..5).map { await(startLongAsyncOperation(it)) }.joinToString("\n") } println(future.get()) }
Bear in mind that async
library actively uses CompletableFuture
from JDK 8, so it will not work with earlier versions.
import kotlinx.coroutines.generate fun main(args: Array<String>) { val sequence = generate { for (i in 1..5) { yield(i) } } println(sequence.joinToString(" ")) }
import kotlinx.coroutines.asyncRx import retrofit2.Retrofit import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory import retrofit2.converter.gson.GsonConverterFactory import retrofit2.http.GET import retrofit2.http.Path import rx.Observable interface GitHub { @GET("orgs/{user}/repos") fun orgRepos(@Path("user") user: String): Observable<List<Repo>> } data class Repo(val name: String) fun main(args: Array<String>) { val retrofit = Retrofit.Builder().apply { baseUrl("https://api.github.com") addConverterFactory(GsonConverterFactory.create()) addCallAdapterFactory(RxJavaCallAdapterFactory.create()) }.build() val github = retrofit.create(GitHub::class.java) asyncRx { for (org in listOf("Kotlin", "ReactiveX")) { // `awaitSingle()` call here is a suspension point, // i.e. coroutine's code stops on it until request is not completed val repos = github.orgRepos(org).take(5).awaitSingle().joinToString() println("$org: $repos") } } }
For more examples you can look at kotlinx-coroutines-async-example-ui
and kotlinx-coroutines-rx-example
samples projects or in tests directories.
Add the bintray repository
<repository> <snapshots> <enabled>false</enabled> </snapshots> <id>dl</id> <name>bintray</name> <url>http://dl.bintray.com/kotlin/kotlin-eap-1.1</url> </repository>
Add dependencies:
<dependency> <groupId>org.jetbrains.kotlinx</groupId> <artifactId>kotlinx-coroutines-generate</artifactId> <version>0.2-alpha-1</version> </dependency> <dependency> <groupId>org.jetbrains.kotlinx</groupId> <artifactId>kotlinx-coroutines-async</artifactId> <version>0.2-alpha-1</version> </dependency> <dependency> <groupId>org.jetbrains.kotlinx</groupId> <artifactId>kotlinx-coroutines-rx</artifactId> <version>0.2-alpha-1</version> </dependency>
Just add dependencies:
compile 'org.jetbrains.kotlinx:kotlinx-coroutines-generate:0.2-alpha-1' compile 'org.jetbrains.kotlinx:kotlinx-coroutines-async:0.2-alpha-1' compile 'org.jetbrains.kotlinx:kotlinx-coroutines-rx:0.2-alpha-1'
NB: As async
library is built upon CompletableFuture
it requires JDK 8 (24 Android API level)
Also you should include our bintray repository:
repositories { maven { url "http://dl.bintray.com/kotlin/kotlin-eap-1.1" } }