异步调用和非阻塞调用之间的区别是什么?在阻塞和同步调用之间(请提供示例)?
当前回答
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执行器(例如操作系统)之间的行为,它意味着这两个实体是否可以并行执行
其他回答
简单地说,
function sum(a,b){
return a+b;
}
为非阻塞。而异步则用于执行阻塞任务,然后返回阻塞任务的响应
正如你可能从众多不同的(通常是相互排斥的)答案中看到的,这取决于你问谁。在某些领域,这两个术语是同义词。或者它们可能分别指两个相似的概念:
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.
在任何一种情况下,目的是允许程序不被阻塞,等待一个缓慢的进程完成-程序如何响应是唯一真正的区别。不同的程序员、不同的语言、不同的平台都不一样。或者这些术语可能指的是完全不同的概念(例如在线程编程中使用同步/异步)。
抱歉,但我不相信有一个唯一的正确答案是全面正确的。
同步意味着按顺序在另一个结果之后启动一个。
异步意味着一起开始,结果上不保证顺序
阻塞是指某些东西导致执行下一步的阻碍。
无阻塞是指不等待任何东西就继续运行,克服障碍。
我敲门,等着他们开门。(我在这里无所事事)
我敲门,如果他们马上开门,我就和他们打招呼,然后走进去等等。如果门没有立刻打开,我就去下一户人家敲门。(我正在做某事,没有闲着)
只有在下雨的时候我才会出去。(依赖关系存在)
我要出去。可能会下雨。(独立事件,发生时间无关紧要)
同步或异步,两者都可以是阻塞或非阻塞的,反之亦然
非阻塞:该函数在栈上时不会等待。
异步:在函数调用离开堆栈后,该函数调用的工作可以继续进行
阻塞:在原语(同步或异步)处理完成后,控制返回到调用进程
非阻塞:调用后控制权立即返回进程