What is the difference between asynchronous and non-blocking calls? Also between blocking and synchronous calls (with examples please)?
相关问题
- Load script async?
- C# An asynchronous operation cannot be started at
- aio_write on linux with rtkaio is sometimes long
- await work in chrome console without async wrapper
- jQuery, how to make synchronous animations?
相关文章
- With a Promise, why do browsers return a reject tw
- Asynchronous SHA256 Hashing
- Does aiohttp have ORM?
- Can each Iteration of a for loop/for_each be done
- How does robospice manage activity lifecycle?
- How to call a async function from a synchronized c
- passing multiple arguments to promise resolution w
- Read headers from HttpResponseMessage before Conte
In many circumstances they are different names for the same thing, but in some contexts they are quite different. So it depends. Terminology is not applied in a totally consistent way across the whole software industry.
For example in the classic sockets API, a non-blocking socket is one that simply returns immediately with a special "would block" error message, whereas a blocking socket would have blocked. You have to use a separate function such as
select
orpoll
to find out when is a good time to retry.But asynchronous sockets (as supported by Windows sockets), or the asynchronous IO pattern used in .NET, are more convenient. You call a method to start an operation, and the framework calls you back when it's done. Even here, there are basic differences. Asynchronous Win32 sockets "marshal" their results onto a specific GUI thread by passing Window messages, whereas .NET asynchronous IO is free-threaded (you don't know what thread your callback will be called on).
So they don't always mean the same thing. To distil the socket example, we could say:
As you can probably see from the multitude of different (and often mutually exclusive) answers, it depends on who you ask. In some arenas, the terms are synonymous. Or they might each refer to two similar concepts:
In either case, the intention is to allow the program to not be blocked waiting for a slow process to complete - how the program is expected to respond is the only real difference. Which term refers to which also changes from programmer to programmer, language to language, or platform to platform. Or the terms may refer to completely different concepts (such as the use of synchronous/asynchronous in relation to thread programming).
Sorry, but I don't believe there is a single right answer that is globally true.
The blocking models require the initiating application to block when the I/O has started. This means that it isn't possible to overlap processing and I/O at the same time. The synchronous non-blocking model allows overlap of processing and I/O, but it requires that the application check the status of the I/O on a recurring basis. This leaves asynchronous non-blocking I/O, which permits overlap of processing and I/O, including notification of I/O completion.
Putting this question in the context of NIO and NIO.2 in java 7, async IO is one step more advanced than non-blocking. With java NIO non-blocking calls, one would set all channels (SocketChannel, ServerSocketChannel, FileChannel, etc) as such by calling
AbstractSelectableChannel.configureBlocking(false)
. After those IO calls return, however, you will likely still need to control the checks such as if and when to read/write again, etc.For instance,
With the asynchronous api in java 7, these controls can be made in more versatile ways. One of the 2 ways is to use
CompletionHandler
. Notice that bothread
calls are non-blocking.Blocking: control returns to invoking precess after processing of primitive(sync or async) completes
Non blocking: control returns to process immediately after invocation