kotlin协程
In this tutorial, we’ll be looking into Kotlin Coroutines. Coroutines is a vital concept since asynchronous programming is the way to go over the traditional blocking a thread and polling stuff.
在本教程中,我们将研究Kotlin协程。 协程是一个至关重要的概念,因为异步编程是克服传统的阻塞线程和轮询内容的方法。
Kotlin协程 (Kotlin Coroutines)
Concurrency is vital in programming. Running different tasks on different threads. But sometimes, we as developers can get carried away and create too many Thread objects which would eventually consume a lot of memory and time. Thankfully, Kotlin has introduced Coroutines which are light-weight threads.
并发在编程中至关重要。 在不同的线程上运行不同的任务。 但是有时候,作为开发人员,我们会被带走,创建太多的Thread对象,这些对象最终会占用大量内存和时间。 值得庆幸的是,Kotlin推出了轻量级的协程。
By light-weight threads we mean, Coroutines take less memory and time compared with the default Java threads.
Coroutines though currently in the initial stages only, has a powerful set of libraries which we can use to make the computation stuff easy.
轻量级线程是我们的意思,与默认的Java线程相比,协程占用的内存和时间更少。
虽然协程目前仅处于初始阶段,但它具有一组功能强大的库,我们可以使用它们简化计算工作。
Coroutines computations can be done without blocking other threads.
协程计算可以在不阻塞其他线程的情况下完成。
How?
怎么样?
Suspending Functions is the answer to it.
暂停功能是解决之道。
Unlike blocking a thread, suspending functions are less expensive and do not require a context switching.
与阻塞线程不同,挂起函数的开销较小,并且不需要上下文切换。