multithreading 异步与多线程 - 有区别吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/600795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 01:00:40  来源:igfitidea点击:

Asynchronous vs Multithreading - Is there a difference?

multithreadinglanguage-agnosticasynchronous

提问by Ted Smith

Does an asynchronous call always create a new thread? What is the difference between the two?

异步调用是否总是创建一个新线程?两者有什么区别?

Does an asynchronous call always create or use a new thread?

异步调用是否总是创建或使用新线程?

Wikipedia says:

维基百科说

In computer programming, asynchronous events are those occurring independently of the main program flow. Asynchronous actions are actions executed in a non-blocking scheme, allowing the main program flow to continue processing.

在计算机编程中,异步事件是独立于主程序流发生的事件。异步动作是以非阻塞方案执行的动作,允许主程序流继续处理。

I know async calls can be done on single threads? How is this possible?

我知道异步调用可以在单线程上完成吗?这怎么可能?

采纳答案by Michael Kohne

This question is darn near too general to answer.

这个问题太笼统了,无法回答。

In the general case, an asynchronous call does not necessarily create a new thread. That's one way to implement it, with a pre-existing thread pool or external process being other ways. It depends heavily on language, object model (if any), and run time environment.

在一般情况下,异步调用不一定会创建新线程。这是实现它的一种方式,预先存在的线程池或外部进程是其他方式。它在很大程度上取决于语言、对象模型(如果有)和运行时环境。

Asynchronous just means the calling thread doesn't sit and wait for the response, nor does the asynchronous activity happen in the calling thread.

异步只是意味着调用线程不会坐下来等待响应,异步活动也不会发生在调用线程中。

Beyond that, you're going to need to get more specific.

除此之外,您将需要变得更具体。

回答by karunski

Whenever the operation that needs to happen asynchronously does not require the CPU to do work, that operation can be done without spawning another thread. For example, if the async operation is I/O, the CPU does not have to wait for the I/O to complete. It just needs to start the operation, and can then move on to other work while the I/O hardware (disk controller, network interface, etc.) does the I/O work. The hardware lets the CPU know when it's finished by interrupting the CPU, and the OS then delivers the event to your application.

每当需要异步发生的操作不需要 CPU 完成工作时,该操作就可以在不产生另一个线程的情况下完成。例如,如果异步操作是 I/O,则 CPU 不必等待 I/O 完成。它只需要开始操作,然后就可以在 I/O 硬件(磁盘控制器、网络接口等)进行 I/O 工作时继续进行其他工作。硬件通过中断 CPU 来让 CPU 知道它何时完成,然后操作系统将事件传递给您的应用程序。

Frequently higher-level abstractions and APIs don't expose the underlying asynchronous API's available from the OS and the underlying hardware. In those cases it's usually easier to create threads to do asynchronous operations, even if the spawned thread is just waiting on an I/O operation.

通常,更高级别的抽象和 API 不会公开从操作系统和底层硬件可用的底层异步 API。在这些情况下,创建线程来执行异步操作通常更容易,即使生成的线程只是在等待 I/O 操作。

If the asynchronous operation requires the CPU to do work, then generally that operation has to happen in another thread in order for it to be truly asynchronous. Even then, it will really only be asynchronous if there is more than one execution unit.

如果异步操作需要 CPU 来完成工作,那么通常该操作必须发生在另一个线程中才能真正实现异步。即便如此,如果有多个执行单元,它实际上只会是异步的。

回答by Jason Orendorff

No, asynchronous calls do not always involve threads.

不,异步调用并不总是涉及线程。

They typically do start some sort of operation which continues in parallel with the caller. But that operation might be handled by another process, by the OS, by other hardware (like a disk controller), by some other computer on the network, or by a human being. Threads aren't the only way to get things done in parallel.

他们通常会启动某种与调用者并行继续的操作。但是该操作可能由另一个进程、操作系统、其他硬件(如磁盘控制器)、网络上的其他计算机或人类处理。线程并不是并行完成任务的唯一方法。

回答by Murugan Gopalan

Multi threading refers to more than one operation happening in the same process. While async programming spreads across processes. For example if my operations calls a web service, The thread need not wait till the web service returns. Here we use async programming which allows the thread not wait for a process in another machine to complete. And when it starts getting response from the webservice it can interrupt the main thread to say that web service has completed processing the request. Now the main thread can process the result.

多线程是指在同一个进程中发生多个操作。而异步编程跨进程传播。例如,如果我的操作调用 Web 服务,则线程无需等到 Web 服务返回。这里我们使用异步编程,它允许线程不等待另一台机器上的进程完成。当它开始从 Web 服务获得响应时,它可以中断主线程,说 Web 服务已完成对请求的处理。现在主线程可以处理结果。

回答by George V. Reilly

JavaScript is single-threaded and asynchronous. When you use XmlHttpRequest, for example, you provide it with a callback function that will be executed asynchronously when the response returns.

JavaScript 是单线程和异步的。例如,当您使用 XmlHttpRequest 时,您为其提供了一个回调函数,该函数将在响应返回时异步执行。

John Resig has a good explanation of the related issue of how timers work in JavaScript.

John Resig对 JavaScript 中计时器如何工作的相关问题有很好的解释。

回答by Otávio Décio

Windows always had asynchronous processing since the non preemptive times (versions 2.13, 3.0, 3.1, etc) using the message loop, way before supporting real threads. So to answer your question, no, it is not necessary to create a thread to perform asynchronous processing.

自非抢占时间(版本 2.13、3.0、3.1 等)以来,Windows 始终使用消息循环进行异步处理,这是在支持真实线程之前的方式。所以要回答你的问题,不,没有必要创建一个线程来执行异步处理。

回答by Mike

Asynchronous calls don't even need to occur on the same system/device as the one invoking the call. So if the question is, does an asynchronous call require a thread in the current process, the answer is no. However, there must be a thread of execution somewhere processing the asynchronous request.

异步调用甚至不需要与调用调用的系统/设备发生在同一系统/设备上。因此,如果问题是,异步调用是否需要当前进程中的线程,那么答案是否定的。但是,在处理异步请求的某处必须有一个执行线程。

Thread of execution is a vague term. In a cooperative tasking systems such as the early Macintosh and Windows OS'es, the thread of execution could simply be the same process that made the request running another stack, instruction pointer, etc... However, when people generally talk about asynchronous calls, they typically mean calls that are handled by another thread if it is intra-process (i.e. within the same process) or by another process if it is inter-process.

执行线程是一个模糊的术语。在早期的 Macintosh 和 Windows 操作系统等协作任务系统中,执行线程可能只是使请求运行另一个堆栈、指令指针等的同一进程……但是,当人们通常谈论异步调用时,它们通常表示由另一个线程处理的调用,如果它是进程内的(即在同一进程内),或者由另一个进程处理,如果它是进程间的。

Note that inter-process (or interprocess) communication (IPC) is commonly generalized to include intra-process communication, since the techniques for locking, and synchronizing data are usually the same regardless of what process the separate threads of execution run in.

请注意,进程间(或进程间)通信 (IPC) 通常泛化为包括进程内通信,因为无论单独的执行线程在哪个进程中运行,锁定和同步数据的技术通常是相同的。

回答by dmckee --- ex-moderator kitten

Some systems allow you to take advantage of the concurrency in the kernel for some facilities using callbacks. For a rather obscure instance, asynchronous IO callbacks were used to implement non-blocking internet severs back in the no-preemptive multitasking days of Mac System 6-8.

某些系统允许您利用内核中的并发性来使用回调来实现某些功能。对于一个相当模糊的例子,在 Mac System 6-8 的非抢占式多任务时代,异步 IO 回调被用来实现非阻塞互联网服务器。

This way you have concurrent execution streams "in" you program without threads as such.

这种方式你并发执行流“中的”你没有线程编程这样

回答by Milan Babu?kov

Asynchronous just means that you don't block your program waiting for something (function call, device, etc.) to finish. It can be implemented in a separate thread, but it is also common to use a dedicated thread for synchronous tasks and communicate via some kind of event system and thus achieve asynchronous-like behavior.

异步只是意味着您不会阻止程序等待某些事情(函数调用、设备等)完成。它可以在单独的线程中实现,但使用专用线程执行同步任务并通过某种事件系统进行通信从而实现类似异步的行为也很常见。

There are examples of single-threaded asynchronous programs. Something like:

有单线程异步程序的例子。就像是:

...do something
...send some async request
while (not done)
    ...do something else
    ...do async check for results

回答by LeopardSkinPillBoxHat

The nature of asynchronous calls is such that, if you want the application to continue running while the call is in progress, you will either need to spawna new thread, or at least utiliseanother thread you that you have created solely for the purposes of handling asynchronous callbacks.

异步调用的性质是这样的,如果您希望应用程序在调用过程中继续运行,您将需要生成一个新线程,或者至少使用您创建的另一个线程,该线程仅用于处理异步回调。

Sometimes, depending on the situation, you may want to invoke an asynchronous method but make it appear to the user to be be synchronous (i.e. block until the asynchronous method has signalled that it is complete). This can be achieved through Win32 APIs such as WaitForSingleObject.

有时,根据情况,您可能希望调用异步方法,但要让用户看起来是同步的(即阻塞,直到异步方法发出信号表示它已完成)。这可以通过 Win32 API 实现,例如WaitForSingleObject