美文网首页
协程原理

协程原理

作者: 威风堂堂堂 | 来源:发表于2019-08-22 15:23 被阅读0次

welcome to the co-routines codelab ,in this codelab you will learn the basics of using kotlin co-routines and android apps to perform things like network request. we put this video together as a quick introduction to co-routines . on kotlin if you already know them from another language feel free to skip it and go straight into the codelab. co-routines are a new feature in kotlin but they're actually a pretty old concept they've been used in popular languages for the last few decades and they turn out to be a great solution to some common problems that we have on android co-routines allow you to replace callbacks you can transform callback style into what's called sequential. programming in this video wo'll explore how they do that. in addition to replace callbacks on android they also give us the ability to make our code main safe and this means you can write co-routine base functions to make a network request for example and just call it from the main thread and everything will be okay. so let's write some code to make imaginary network request to make this interesting let's start it on the main thread if we write it in what's called a blocking style where wu return the result directly from the network request it will block the main thread this is a probleam because a network request may take seconds or even minutes to complete and the entire time that it's running the mian thread won't get updates or respond to touch and the user will see your app as frozen this is a really bad experience for the user and wo don't want to do that on android if instead we write the same function with callbacks now our code won't block our networking library we'll take care of running the network request on another thread and when the data is ready it will pass the data or call back to the main thread the callback is the handle the library uses to pass data from the background thread to the main thread while callbacks are a greate solution to async programming they come with a few issuses they don't handle air as well without doing more work and they become overwhelming when too many callbacks are used in the same function to solve these issues we can use co-routines here's the sacme function written with co-routines . to make it work with co-routines it becomes a suspend function it looks just like it did when it was a blocking function however since it's a suspend function instead of blocking the main thread when network request is called it will suspend the co-routine this frees up the main thread to handle on draw and user touches and when the result is ready it'll get passed back instead of useing a callback the co-routine wi resume. now it's important to note that with co-routines our networking library still uses another thread to run the network request just like it did with callbacks the main difference is the code tends to be simpler than the callback version now you can think about suspend and resume as replacing callbacks so when you call network requests suspend makes a callback from the rest of the function so how does kotion do that. let's take a look at how it executes when kotin executes onDataNeeded it will execute it a bit differently on the call statck than a normal function it keeps track of the place it starts the co-routines so that can implement suspend and resume then it will call it just like a normal function executing each line of code untill it reaches a call to another suspend function before it calls the suspend function it will copy the current state for later and that's how kotlin implements suspend you can think of this state as a callback that knows how to resume the current function then kotlin will run network requests until it suspends itself to make the network call now at this point all of the co-routines on main are suspended so main is free to handle ui events fase forward a bit until the network request comes back and kotion will resume these co-routines first it'l resume network requests as it does this by restoring the state it's saved earlier once request is done processing the request it'll return to on data needed and the result is available immediately if the network request failed it would throw an exception right here instead then the co-routine will continue executing just like a normal function until it returns because co-routines allow us to handle long running tasks without introducing callbacks they tend to create simpler more readable code .in the rest of this codelab you'll explore how to integrate co-routine with arc components to make a network request we'll also touch upon how to make code main safe and with that let's suspend this video and resume the codelab

suspend fun onDataNeeded(){

    val result = networkRequest()

    show(result)

}

相关文章

  • 并发编程-协程

    协程greenlet模块 (gevent实现原理)gevent模块 (注册协程,实现异步编程) 协程的应用eg:...

  • Kotlin 协程入门

    本文主要介绍协程长什么样子, 协程是什么东西, 协程挂起的实现原理以及整理了协程学习的资料. 协程 HelloWo...

  • Kotlin Primer·第七章·协程库(上篇)

    本篇只讲了协程库的使用。还有中篇讲协程的启动和切换实现原理,下篇核心讲解kotlin协程在JVM层的实现原理。这可...

  • C#协程

    Unity中协程的执行原理 UnityGems.com给出了协程的定义: A coroutine is a fun...

  • 协程原理

    welcome to the co-routines codelab ,in this codelab you w...

  • 2017/09/28 面试

    智慧芽 面试: 简单描述一下协程的运作原理。(在 "关于协程" 中有描述) 协程在遇到发送IO请求时是如何运作的?...

  • 协程

    对于协程做一个整体的描述,从概念、原理、实现三个方面叙述。侧重有栈协程。 1 概览 1.1 什么是协程 有很多与协...

  • Python 协程 异步资料记录

    1. Python3.5协程原理 详细说明了Python协程产生的历史. 研究明白之后,写一篇文章,解释协程,并附...

  • 协程简单理解

    带着以下几个问题去简单理解协程原理。1.协程到底是怎么创建和启动的?简略版可参考这篇:Kotlin协程create...

  • Kotlin协程之再次读懂协程工作原理

    概述 关于协程的创建,以及挂起和恢复,之前有写过一篇文章 Kotlin协程之深入理解协程工作原理[https://...

网友评论

      本文标题:协程原理

      本文链接:https://www.haomeiwen.com/subject/rgcisctx.html