How is Apache NIO HttpAsyncClient able to wait for a remote response without blocking any thread? Does it have a way to setup a callback with the OS (I doubt so?). Otherwise does it perform some sort of polling?
How is Apache NIO HttpAsyncClient performing non-blocking HTTP Client
Asked Answered
Upon starting, the asynchronous http client starts one or more threads. Some of them will be blocked waiting for data, but they are internal to the client, so none of the threads created by the user will be blocked.
The mechanism is less primitive than just starting a thread per request though: there is a way to multiplex many sockets onto one object ("selector") and do a blocking wait for any of the sockets to become ready for reading or writing: this way, the (internal) thread is blocked, but since it is handling many sockets at once, it is not blocked by any one of them. Maybe this also contributes to the "without blocking" metaphor.
© 2022 - 2024 — McMap. All rights reserved.