Name | Date | Size | #Lines | LOC | ||
---|---|---|---|---|---|---|
.. | - | - | ||||
api/ | 03-May-2024 | - | 9 | 7 | ||
src/ | 03-May-2024 | - | 463 | 168 | ||
test/ | 03-May-2024 | - | 658 | 572 | ||
README.md | D | 03-May-2024 | 3.5 KiB | 61 | 46 | |
build.gradle.kts | D | 03-May-2024 | 317 | 14 | 7 | |
package.list | D | 03-May-2024 | 402 | 17 | 16 |
README.md
1# Module kotlinx-coroutines-guava 2 3Integration with Guava [ListenableFuture](https://github.com/google/guava/wiki/ListenableFutureExplained). 4 5Coroutine builders: 6 7| **Name** | **Result** | **Scope** | **Description** 8| -------- | ---------- | ---------- | --------------- 9| [future] | [ListenableFuture][com.google.common.util.concurrent.ListenableFuture] | [CoroutineScope] | Returns a single value with the future result 10 11Extension functions: 12 13| **Name** | **Description** 14| -------- | --------------- 15| [ListenableFuture.await][com.google.common.util.concurrent.ListenableFuture.await] | Awaits for completion of the future (cancellable) 16| [Deferred.asListenableFuture][kotlinx.coroutines.Deferred.asListenableFuture] | Converts a deferred value to the future 17 18## Example 19 20Given the following functions defined in some Java API based on Guava: 21 22```java 23public ListenableFuture<Image> loadImageAsync(String name); // starts async image loading 24public Image combineImages(Image image1, Image image2); // synchronously combines two images using some algorithm 25``` 26 27We can consume this API from Kotlin coroutine to load two images and combine then asynchronously. 28The resulting function returns `ListenableFuture<Image>` for ease of use back from Guava-based Java code. 29 30```kotlin 31fun combineImagesAsync(name1: String, name2: String): ListenableFuture<Image> = future { 32 val future1 = loadImageAsync(name1) // start loading first image 33 val future2 = loadImageAsync(name2) // start loading second image 34 combineImages(future1.await(), future2.await()) // wait for both, combine, and return result 35} 36``` 37 38Note that this module should be used only for integration with existing Java APIs based on `ListenableFuture`. 39Writing pure-Kotlin code that uses `ListenableFuture` is highly not recommended, since the resulting APIs based 40on the futures are quite error-prone. See the discussion on 41[Asynchronous Programming Styles](https://github.com/Kotlin/kotlin-coroutines/blob/master/kotlin-coroutines-informal.md#asynchronous-programming-styles) 42for details on general problems pertaining to any future-based API and keep in mind that `ListenableFuture` exposes 43a _blocking_ method 44[get](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Future.html#get--) 45that makes it especially bad choice for coroutine-based Kotlin code. 46 47# Package kotlinx.coroutines.future 48 49Integration with Guava [ListenableFuture](https://github.com/google/guava/wiki/ListenableFutureExplained). 50 51<!--- MODULE kotlinx-coroutines-core --> 52<!--- INDEX kotlinx.coroutines --> 53[CoroutineScope]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html 54<!--- MODULE kotlinx-coroutines-guava --> 55<!--- INDEX kotlinx.coroutines.guava --> 56[future]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-guava/kotlinx.coroutines.guava/kotlinx.coroutines.-coroutine-scope/future.html 57[com.google.common.util.concurrent.ListenableFuture]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-guava/kotlinx.coroutines.guava/com.google.common.util.concurrent.-listenable-future/index.html 58[com.google.common.util.concurrent.ListenableFuture.await]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-guava/kotlinx.coroutines.guava/com.google.common.util.concurrent.-listenable-future/await.html 59[kotlinx.coroutines.Deferred.asListenableFuture]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-guava/kotlinx.coroutines.guava/kotlinx.coroutines.-deferred/as-listenable-future.html 60<!--- END --> 61