19

Coroutine

 5 years ago
source link: https://studygolang.com/articles/17003?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

概述

协程可能是近几年来最新的并行/并发模型实现,常见语言中最早的实现可能是lua,近年来新晋语言golang的崛起更把这个概念发扬光大。

Kotlin 的实现设计思路有以下几个特点:

尽可能不使用核心语言,而是标准库来实现。

标准库只提供核心功能,官方的第三方库提供应用层接口。

异步等操作使用协程封装,同时协程可用于其他方面尽可能替代线程。

尽管如此,官方也不得不加入suspend关键字来标记协程可挂起函数,同时协程的实现也需要编译器支持。

suspend关键字与C#等语言中的async关键字很接近。线程的调度由操作系统完成,而协程需要由运行时管理。为了标记一个函数可以被运行时挂起,需要此关键字。当然协程的范围比异步更广,这里不再赘述。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK