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

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

问题描述

并发性使两个任务在不同的线程上并行运行。但是,异步方法并行运行但是在同一个线程上。这是如何实现的?

解决方案

$

并行和并行是与你正确推测的原则相同的原则,两者都与同时执行的任务相关,虽然我会说并行任务应该是真正的多任务,同时执行,而并发可能意味着



异步方法与前两个概念不直接相关,异步用于表示并发或并行任务的印象,但是有效地异步方法调用通常用于需要远离当前应用程序工作的进程,并且我们不想等待并阻止我们的应用程序等待响应。



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



从用户角度看,它似乎是多任务,但它可能不是。






EDIT



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


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.

For example, getting data from a database could take time but we don't want to block our UI waiting for the data. The asynch 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 hand off that update) as appropriate.

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


EDIT

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天全站免登陆