1 coroutines: Coroutines are lightweight threads, a very similar concept to threads for multitasking. The Lightweight thread framework 2 in Kotlin is a set of official thread apis like Java Executor and Android AsyncTask 3 a thread framework, which is convenient! The ability to switch threads multiple times in the same code block! Write asynchronous code in a synchronous mannerCopy the code
  • Features: Non-blocking suspend (coroutine suspends, giving up thread)
  • Basic function: handle multithreading concurrency
  • Convenience: To solve callback hell development and change the difficulty of concurrent operations, you can write code running in different threads in the same code block, up and down two lines of code, threads cut away and cut back, without taking a cloud

How to use

Use the thread IO\Main directly in the Lauch function

Create a new coroutine and run it on the specified thread (Io or Main). The code inside the function body is the coroutine being run

When to use it

Get the data in the background when you need to cut threads or when you specify threads? Slice! Update at the front desk? Cut again!

No! It’s a lot like nested development

So if you’re switching threads in a thread body, you’re using the withContext function.

WithContext () is a function of the suspend modifier

Coroutines can cut back automatically! Eliminates nesting of concurrent code in collaboration !!!!!

Made a mistake! Come again

Easy like this! Enables collaborative routines between multiple threads

Suspend non-blocking, suspending functions (coroutines suspend, giving up threads)

If the withContext function is to be treated as logic in a normal function (suspenddingGetImage()), you need to suspend the function you declare before the code in the coroutine block is called. Otherwise, an error is reported.

Section 2 Notes hanging juejin.cn/post/684490…

Lesson video www.bilibili.com/video/av671…