异步调用和非阻塞调用之间的区别是什么?在阻塞和同步调用之间(请提供示例)?
当前回答
同步/异步是用来描述两个模块之间的关系。 阻塞/非阻塞是描述一个模块的情况。
一个例子: 模块X: I。 模块Y:“书店”。 X问Y:你有《c++入门》这本书吗?
blocking: before Y answers X, X keeps waiting there for the answer. Now X (one module) is blocking. X and Y are two threads or two processes or one thread or one process? we DON'T know. non-blocking: before Y answers X, X just leaves there and do other things. X may come back every two minutes to check if Y has finished its job? Or X won't come back until Y calls him? We don't know. We only know that X can do other things before Y finishes its job. Here X (one module) is non-blocking. X and Y are two threads or two processes or one process? we DON'T know. BUT we are sure that X and Y couldn't be one thread. synchronous: before Y answers X, X keeps waiting there for the answer. It means that X can't continue until Y finishes its job. Now we say: X and Y (two modules) are synchronous. X and Y are two threads or two processes or one thread or one process? we DON'T know. asynchronous: before Y answers X, X leaves there and X can do other jobs. X won't come back until Y calls him. Now we say: X and Y (two modules) are asynchronous. X and Y are two threads or two processes or one process? we DON'T know. BUT we are sure that X and Y couldn't be one thread.
请注意上面两个加粗的句子。为什么题目中的粗体字包含两种情况,而题目中的粗体字只包含一种情况?这是区分非阻塞和异步的关键。
让我试着用另一种方式来解释这四个词:
blocking: OMG, I'm frozen! I can't move! I have to wait for that specific event to happen. If that happens, I would be saved! non-blocking: I was told that I had to wait for that specific event to happen. OK, I understand and I promise that I would wait for that. But while waiting, I can still do some other things, I'm not frozen, I'm still alive, I can jump, I can walk, I can sing a song etc. synchronous: My mom is gonna cook, she sends me to buy some meat. I just said to my mom: We are synchronous! I'm so sorry but you have to wait even if I might need 100 years to get some meat back... asynchronous: We will make a pizza, we need tomato and cheeze. Now I say: Let's go shopping. I'll buy some tomatoes and you will buy some cheeze. We needn't wait for each other because we are asynchronous.
下面是一个关于非阻塞和同步的典型例子:
// thread X
while (true)
{
msg = recv(Y, NON_BLOCKING_FLAG);
if (msg is not empty)
{
break;
}
else
{
sleep(2000); // 2 sec
}
}
// thread Y
// prepare the book for X
send(X, book);
You can see that this design is non-blocking (you can say that most of time this loop does something nonsense but in CPU's eyes, X is running, which means that X is non-blocking. If you want you can replace sleep(2000) with any other code) whereas X and Y (two modules) are synchronous because X can't continue to do any other things (X can't jump out of the loop) until it gets the book from Y. Normally in this case, making X blocking is much better because non-blocking spends much resource for a stupid loop. But this example is good to help you understand the fact: non-blocking doesn't mean asynchronous.
这四个字确实很容易让我们困惑,我们应该记住的是,这四个字是为建筑设计服务的。学习如何设计一个好的架构是区分它们的唯一方法。
例如,我们可以设计这样一种架构:
// Module X = Module X1 + Module X2
// Module X1
while (true)
{
msg = recv(many_other_modules, NON_BLOCKING_FLAG);
if (msg is not null)
{
if (msg == "done")
{
break;
}
// create a thread to process msg
}
else
{
sleep(2000); // 2 sec
}
}
// Module X2
broadcast("I got the book from Y");
// Module Y
// prepare the book for X
send(X, book);
在这个例子中,我们可以说
X1是非阻塞的 X1和X2是同步的 X和Y是异步的
如果需要,还可以用这四个字来描述在X1中创建的线程。
再说一次:这四个字是为建筑设计服务的。所以我们需要的是一个合适的架构,而不是像语言律师一样区分这四个词。如果你遇到一些情况,你不能很清楚地区分这四个词,你应该忘记这四个词,用你自己的话来描述你的架构。
所以更重要的事情是:我们什么时候使用同步而不是异步?什么时候用阻塞代替非阻塞?X1的阻塞性比非阻塞性更好吗?X和Y是同步的还是异步的?为什么Nginx是非阻塞的?Apache为什么阻塞?这些问题是你必须弄清楚的。
为了做出正确的选择,您必须分析您的需求并测试不同体系结构的性能。没有这样一种体系结构可以满足各种需求。
其他回答
同步意味着按顺序在另一个结果之后启动一个。
异步意味着一起开始,结果上不保证顺序
阻塞是指某些东西导致执行下一步的阻碍。
无阻塞是指不等待任何东西就继续运行,克服障碍。
我敲门,等着他们开门。(我在这里无所事事)
我敲门,如果他们马上开门,我就和他们打招呼,然后走进去等等。如果门没有立刻打开,我就去下一户人家敲门。(我正在做某事,没有闲着)
只有在下雨的时候我才会出去。(依赖关系存在)
我要出去。可能会下雨。(独立事件,发生时间无关紧要)
同步或异步,两者都可以是阻塞或非阻塞的,反之亦然
synchronous | asynchonous | |
---|---|---|
block | Block I/O must be a synchronus I/O, becuase it has to be executed in order. Synchronous I/O might not be block I/O | Not exist |
non-block | Non-block and Synchronous I/O at the same time is polling/multi-plexing.. | Non-block and Asynchronous I/O at the same time is parallel execution, such as signal trigger… |
block/non-block描述了初始化实体本身的行为,它意味着实体在等待I/O完成期间所做的事情 同步/异步描述了I/O初始化实体和I/O执行器(例如操作系统)之间的行为,它意味着这两个实体是否可以并行执行
正如你可能从众多不同的(通常是相互排斥的)答案中看到的,这取决于你问谁。在某些领域,这两个术语是同义词。或者它们可能分别指两个相似的概念:
One interpretation is that the call will do something in the background essentially unsupervised in order to allow the program to not be held up by a lengthy process that it does not need to control. Playing audio might be an example - a program could call a function to play (say) an mp3, and from that point on could continue on to other things while leaving it to the OS to manage the process of rendering the audio on the sound hardware. The alternative interpretation is that the call will do something that the program will need to monitor, but will allow most of the process to occur in the background only notifying the program at critical points in the process. For example, asynchronous file IO might be an example - the program supplies a buffer to the operating system to write to file, and the OS only notifies the program when the operation is complete or an error occurs.
在任何一种情况下,目的是允许程序不被阻塞,等待一个缓慢的进程完成-程序如何响应是唯一真正的区别。不同的程序员、不同的语言、不同的平台都不一样。或者这些术语可能指的是完全不同的概念(例如在线程编程中使用同步/异步)。
抱歉,但我不相信有一个唯一的正确答案是全面正确的。
阻塞模型要求初始应用程序在I/O开始时阻塞。这意味着不可能同时重叠处理和I/O。同步非阻塞模型允许处理和I/O重叠,但它要求应用程序反复检查I/O的状态。这就留下了异步非阻塞I/O,允许处理和I/O重叠,包括I/O完成的通知。
非阻塞:该函数在栈上时不会等待。
异步:在函数调用离开堆栈后,该函数调用的工作可以继续进行
推荐文章
- 什么时候我应该在ASP中使用异步控制器。净MVC吗?
- 理解设置
- 如何使HTTP请求在PHP和不等待响应
- 反应-显示加载屏幕,而DOM是渲染?
- 如何正确地读取异步/等待文件?
- 使用Moq模拟单元测试的异步方法
- 如何使用JUnit来测试异步进程
- Kotlin协程中的启动/连接和异步/等待之间有什么区别
- CompletableFuture, Future和RxJava的Observable之间的区别
- 为什么要在c#中使用Task<T>而不是ValueTask<T> ?
- Asyncio。Gather vs asyncio.wait
- 同步调用异步方法
- 所有异步forEach回调完成后的回调
- 并发、并行和异步方法之间的区别是什么?
- 同步调用一个异步Javascript函数