并发、并行和异步方法之间有什么区别? [英] What is the difference between concurrency, parallelism and asynchronous methods?

查看:22
本文介绍了并发、并行和异步方法之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

并发是让两个任务在不同的线程上并行运行.但是,异步方法并行运行但在同一个线程上.这是如何实现的?另外,并行性怎么样?

Concurrency is having two tasks run in parallel on separate threads. However, asynchronous methods run in parallel but on the same 1 thread. How is this achieved? Also, what about parallelism?

这三个概念有什么区别?

What are the differences between these 3 concepts?

推荐答案

正如您正确推测的那样,并发和并行实际上是相同的原则,两者都与同时执行的任务有关,尽管我会说并行任务应该是真正的多任务处理,同时"执行,而并发可能意味着任务共享执行线程,同时似乎仍并行执行.

Concurrent and parallel are effectively the same principle as you correctly surmise, both are related to tasks being executed simultaneously although I would say that parallel tasks should be truly multitasking, executed "at the same time" whereas concurrent could mean that the tasks are sharing the execution thread while still appearing to be executing in parallel.

异步方法与前两个概念没有直接关系,异步用于呈现并发或并行任务的印象,但实际上异步方法调用通常用于需要远离当前应用程序进行工作的进程我们不想等待并阻止我们的应用程序等待响应.

Asynchronous methods aren't directly related to the previous two concepts, asynchrony is used to present the impression of concurrent or parallel tasking but effectively an asynchronous method call is normally used for a process that needs to do work away from the current application and we don't want to wait and block our application awaiting the response.

例如,从数据库获取数据可能需要一些时间,但我们不想阻止 UI 等待数据.异步调用采用回调引用,并在远程系统发出请求后立即将执行返回给您的代码.当远程系统执行任何需要的处理时,您的 UI 可以继续响应用户,一旦它将数据返回给您的回调方法,那么该方法可以根据需要更新 UI(或移交该更新).

For example, getting data from a database could take time but we don't want to block our UI waiting for the data. The async call takes a call-back reference and returns execution back to your code as soon as the request has been placed with the remote system. Your UI can continue to respond to the user while the remote system does whatever processing is required, once it returns the data to your call-back method then that method can update the UI (or handoff that update) as appropriate.

从用户的角度来看,它看起来像多任务处理,但也可能不是.

From the User perspective, it appears like multitasking but it may not be.

编辑

可能值得补充的是,在许多实现中,异步方法调用会导致线程启动,但这不是必需的,它实际上取决于正在执行的操作以及如何将响应通知回系统.

It's probably worth adding that in many implementations an asynchronous method call will cause a thread to be spun up but it's not essential, it really depends on the operation being executed and how the response can be notified back to the system.

这篇关于并发、并行和异步方法之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆