- Reactive Programming in Kotlin
- Rivu Chakraborty
- 406字
- 2021-07-02 22:26:39
Coroutines
Path breaking and, probably, the most exciting feature in Kotlin are coroutines. They are a new way to write asynchronous, non-blocking code somewhere like the threads, but way more simple, efficient, and lightweight. Coroutines were added in Kotlin 1.1 and are still experimental, so think before using it in production.
In the later chapters of this book, you'll learn about Schedulers in RxKotlin, which encapsulates the complexities of threading, but you can use it only in RxKotlin chain, while you can use coroutines anywhere and everywhere. That is indeed a path-breaking feature of Kotlin. They provide a great abstraction on threads, making context changes and concurrency easier.
Keep in mind that RxKotlin does not use coroutines yet; the reason is quite simple–both coroutines and Schedulers in RxKotlin share nearly the same internal architecture; while coroutines are new, Schedulers have been there for a long time with RxJava, RxJs, RxSwift, and more.
Coroutines are the best fit for developers to implement concurrency when they're not using/can't use RxKotlin Schedulers.
So, let's start by adding it to our project. If you are using Gradle, follow these steps (apply plugin could be 'kotlin' or 'kotlin-android', depending on whether you use it for JVM or Android):
apply plugin: 'kotlin' kotlin { experimental { coroutines 'enable' } }
And then, we have to add the following dependency:
repositories { ... jcenter() } dependencies { ... compile "org.jetbrains.kotlinx:kotlinx-coroutines-core:0.16" }
If you are using Maven, then add the following code block in the pom.xml file:
<plugin> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-maven-plugin</artifactId> ... <configuration> <args> <arg>-Xcoroutines=enable</arg> </args> </configuration> </plugin> <repositories> ... <repository> <id>central</id> <url>http://jcenter.bintray.com</url> </repository> </repositories> <dependencies> ... <dependency> <groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlinx-coroutines-core</artifactId> <version>0.16</version> </dependency> </dependencies>
Apache Maven is a software project management and comprehension tool. Based on the concept of a Project Object Model (POM), Maven can manage a project's build, reporting, and documentation from a central piece of information. Please refer to the following URL for more information–https://maven.apache.org/.
So, what exactly is a coroutine? While developing applications, we often come into situations where we need to perform long running or time taking operations, such as network call, database operations, or some complex computations. The only option in Java is to use a thread to handle such situations, which is very complex itself. Whenever we face those situations, we feel the need for a simple yet powerful API to handle such cases. Developers from the .NET domain, especially those who used C# before, are familiar with the async/await operators; this is somehow the closest to Kotlin coroutines.
- DB29forLinux,UNIX,Windows數(shù)據(jù)庫(kù)管理認(rèn)證指南
- SQL Server 2008數(shù)據(jù)庫(kù)應(yīng)用技術(shù)(第二版)
- 大話Oracle Grid:云時(shí)代的RAC
- Dependency Injection with AngularJS
- 深度剖析Hadoop HDFS
- 基于OPAC日志的高校圖書館用戶信息需求與檢索行為研究
- 信息學(xué)競(jìng)賽寶典:數(shù)據(jù)結(jié)構(gòu)基礎(chǔ)
- 視覺大數(shù)據(jù)智能分析算法實(shí)戰(zhàn)
- Augmented Reality using Appcelerator Titanium Starter
- Spark分布式處理實(shí)戰(zhàn)
- SQL Server深入詳解
- Python數(shù)據(jù)分析從小白到專家
- 改變未來(lái)的九大算法
- 改進(jìn)的群智能算法及其應(yīng)用
- 企業(yè)大數(shù)據(jù)處理:Spark、Druid、Flume與Kafka應(yīng)用實(shí)踐