简体   繁体   English

kotlin 协程和 asyncTask class 和多线程有什么区别

[英]what is difference between kotlin coroutine and asyncTask class and multiThread

I have read about Kotlin coroutines recently and now, I wonder what is the difference between asyncTask class and multi Thread programming and coroutines?我最近和现在阅读了有关 Kotlin 协程的内容,我想知道 asyncTask class 和多线程编程和协程有什么区别? in what situation I should use each one?在什么情况下我应该使用每一个?

AsyncTask is abstract class and it must be subclassed. AsyncTask是抽象的 class ,它必须是子类。 AsyncTask has 4 steps: onPreExecute, doInBackground, onProgressUpdate and onPostExecute. AsyncTask 有 4 个步骤:onPreExecute、doInBackground、onProgressUpdate 和 onPostExecute。 They are executed serially on single background thread.它们在单个后台线程上串行执行。

  • If you want to fetch a URL or perform a heavyweight computation in Android, you have to use async programming.如果要获取 URL 或在 Android 中执行重量级计算,则必须使用异步编程。

  • they can be used when there is small task to communicate with main thread.当有小任务与主线程通信时,可以使用它们。 for tasks that use multiple instances in parallel.对于并行使用多个实例的任务。

Thread is a concurrent unit of execution.线程是一个并发的执行单元。 It has its own call stack.它有自己的调用堆栈。 With threads, the operating system switches running threads preemptively according to its scheduler.使用线程,操作系统根据其调度程序抢先切换正在运行的线程。

  • they can be used for tasks running in parallel use Multiple threads.它们可以用于并行运行的任务使用多个线程。

  • for task where you want to control the CPU usage relative to the GUI thread.对于要控制相对于 GUI 线程的 CPU 使用率的任务。

Coroutines use to write asynchronous code that will look like normal sequential code.协程用于编写看起来像普通顺序代码的异步代码。 they can provide a very high level of concurrency with very little overhead.它们可以以很少的开销提供非常高的并发性。

  • They are simple to read, unlike thread they are lightweight and unlike AsyncTask lot of them can run at same time.它们易于阅读,与线程不同,它们是轻量级的,并且与 AsyncTask 不同,它们中的许多可以同时运行。

AsyncTask was the first hand solution proposed by Google in Android SDK in order to process work in the background, while keeping the main thread free from many complex operations. AsyncTask是 Google 在 Android SDK 中提出的第一手解决方案,以便在后台处理工作,同时保持主线程免于许多复杂操作。 In fact, AsyncTask let you to do complex processing in an asynchronous manner.实际上, AsyncTask可以让你以异步的方式进行复杂的处理。 Compared to classical Java Thread , the AsyncTask was somehow specialized, providing UI wrappers around threads in order to allow a more enjoyable experience as a developer, coding in an async way.与经典的 Java Thread相比, AsyncTask在某种程度上是专门的,它提供围绕线程的 UI 包装器,以便作为开发人员提供更愉快的体验,以异步方式编码。 The AsyncTask class was deprecated in the meantime and the recommended way to solve things is by using coroutines.与此同时, AsyncTask class 已被弃用,推荐的解决方法是使用协程。

Coroutines are not a new concept introducer by Kotlin, in fact this concept exists in a lot of programming languages (Go has Goroutines and Java will provide something called Fibers ). Coroutines并不是 Kotlin 引入的新概念,实际上这个概念存在于很多编程语言中(Go 有Goroutines并且 Java 会提供一个叫做Fibers的东西)。 The main advantage of using coroutines is the simplicity of code, the only thing that differentiate a sync task/function in face of an async task/function is the usage of suspend keyword put in front of the function.使用协程的主要优点是代码的简单性,面对异步任务/函数的唯一区别是在 function 前面使用了suspend关键字。 For example, the following function is executed in a synchronous way:比如下面的function是同步执行的:

fun doSomething() = println("Print something")

while the following one is executed on a asynchronous way, due to the usage of suspend keyword:由于使用了suspend关键字,以下以异步方式执行:

suspend fun doSomething()  = println("Print something")

When a suspend function is reached, the program will not block there, and will go further in running the rest of the code, but will receive a Continuation which will return the value computed by the suspended function when this one will be available. When a suspend function is reached, the program will not block there, and will go further in running the rest of the code, but will receive a Continuation which will return the value computed by the suspended function when this one will be available.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM