异步调用和非阻塞调用之间的区别是什么?在阻塞和同步调用之间(请提供示例)?


当前回答

非阻塞调用立即返回任何可用的数据:请求的全部字节数、更少的字节数或根本没有。

异步调用请求的传输将以其整体(全部)执行,但将在未来某个时间完成。

其他回答

把这个问题放在java 7中的NIO和NIO.2上下文中,异步IO比非阻塞先进了一步。 使用java NIO非阻塞调用,可以通过调用abstractselectablecchannel . configureblocking (false)来设置所有通道(SocketChannel、ServerSocketChannel、FileChannel等)。 然而,在这些IO调用返回之后,您可能仍然需要控制检查,例如是否以及何时再次读/写等等。 例如,

while (!isDataEnough()) {
    socketchannel.read(inputBuffer);
    // do something else and then read again
}

使用java 7中的异步api,可以以更通用的方式创建这些控件。 两种方法之一是使用CompletionHandler。注意,两个读调用都是非阻塞的。

asyncsocket.read(inputBuffer, 60, TimeUnit.SECONDS /* 60 secs for timeout */, 
    new CompletionHandler<Integer, Object>() {
        public void completed(Integer result, Object attachment) {...}  
        public void failed(Throwable e, Object attachment) {...}
    }
}

非阻塞调用立即返回任何可用的数据:请求的全部字节数、更少的字节数或根本没有。

异步调用请求的传输将以其整体(全部)执行,但将在未来某个时间完成。

异步指的是并行完成的事情,比如另一个线程。 非阻塞通常指轮询,即检查给定条件是否成立(套接字是可读的,设备有更多的数据,等等)。

简单地说,

function sum(a,b){
return a+b;
}

为非阻塞。而异步则用于执行阻塞任务,然后返回阻塞任务的响应

同步/异步是用来描述两个模块之间的关系。 阻塞/非阻塞是描述一个模块的情况。

一个例子: 模块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为什么阻塞?这些问题是你必须弄清楚的。

为了做出正确的选择,您必须分析您的需求并测试不同体系结构的性能。没有这样一种体系结构可以满足各种需求。