score:6

Accepted answer

the java asynchronous i/o model is described here at an easy enough level to grasp. the basic idea is that there's an internal thread pool which retrieves completed i/o notifications from the kernel and then dispatch to other threads to perform the required actions on it.

so, in a sense, yes, it uses threads. and here's something else to consider: so does everything. every piece of software out there requires that a process, at some point, check whether a piece of i/o has completed so that it can perform follow-up tasks on it (well, it could be fire-and-forget, but that's somewhat limited for practical sues). on nodejs, famous for its asynchronous i/o, that thread is called the "event loop" (though the overall model is very different).

the point here is that there is not a correspondence of one thread per i/o operation. instead, there's a single internal thread pool that's responsible for receiving all asynchronous i/o completion events, and then taking whatever actions are required on their completion.

perhaps a better question is: does asynchronous i/o in java consume threads in proportion to the number of i/o requests being processed? no, it doesn't; it consumes a fixed number of threads. more useful question: when initiating an asynchronous i/o in java, does that block the thread that initiated the i/o? no, it does not; it returns immediately. and relevant question to the topic: does asynchronous i/o in java uses threads from the actor thread pool? no, it doesn't.

next, to the future returned by asynchronous i/o. while the i/o does not complete, no thread will be used. however, there is a thread pool assigned to the completion of that future, and, when the i/o completes, one thread from that pool will be used to perform the actions that you associate with that future's completion. once those actions are finished, the thread will be returned to that thread pool. that thread pool is probably not going to be the same as the thread pool used by the actors (though i suppose there might be a way to make it so).

score:1

tl;dr on the accepted answer: no, threads are not consumed by aysnchronous i/o, but threads are used to retrieve the i/o results from the kernel.

also, from play framework: async i/o without the thread pool and callback hell:

on evented servers, waiting for i/o is very cheap: idle requests have negligible cost, as they don’t hold up a thread.


Related Query

More Query from same tag