官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 乐昌市| 灵山县| 永德县| 广水市| 五大连池市| 吐鲁番市| 于都县| 和田县| 兴安县| 台东市| 太湖县| 合江县| 尚义县| 海丰县| 通海县| 唐山市| 芜湖市| 资源县| 成武县| 镇原县| 牡丹江市| 辉南县| 炎陵县| 金乡县| 开江县| 榕江县| 昌都县| 昭通市| 泰州市| 晋江市| 建阳市| 乌拉特后旗| 民县| 上杭县| 乐亭县| 田林县| 玛曲县| 古田县| 巩留县| 浦北县| 桦甸市|