Asynchronous I/O
I/O is a costlier operation as it will slow down the processing because it involves communicating and making another hardware device work. I/O also means that the process that triggers it is put under wait until it completes it. Say you have a requirement to read some information from a huge file on the server and you have to display it as part of a web application screen. Logically this means you have to read the entire file and then embed it as part of your HTML and send it back to the browser client. The disadvantage here is bigger the file is, more time the client is going to wait. This also means that the client cannot do anything else as the page will load only when the server application completes reading the entire file, frames the HTML and sends it back to the browser.
Basically the thread that was initiated in the request in the above scenario gets locked until the file I/O is complete. What if you actually wanted to bring back the rest of the screen and bring up the information from the file through an AJAX request and update it as and when you have chunks of information? On the server side you need to have the capability to get the information from the file in chunks and keep sending it to the browser client. Non-blocking I/O (NBIO) or Asynchronous I/O mechanism help in achieving this. Basically in block I/O data is processed in chunks rather than byte by byte. Java introduced New IO (NIO) as part of JDK starting from version 1.4. Part of it is asynchronous I/O which can help in enabling producer/consumer kind of requirement without blocking the request during I/O. This IBM developerWorks tutorial “Getting started with new I/O” (needs signing up with IBM developerWorks) explains NIO with a section on Asynchronous I/O.









