Why use Java’s asynchronous filechannel?
I can understand why web applications will use multiplexing (not creating too many threads) and why programs will use asynchronous calls for pipelining (more efficient) But I don't understand the efficiency purpose of asynchronous filechannel
Any ideas?
Solution
This is a channel that can be used to read files asynchronously, that is, the I / O operation is completed on a separate thread, so the thread calling it can perform other operations when the I / O operation occurs
For example, the read () method of the class returns a future object to get the result of reading data from the file So what you can do is call read (), which will immediately return a future object In the background, another thread will read the actual data from the file Your own thread can continue to execute the task. When you need to read data, you can call get () on the future object That will return data (if the background thread has not finished reading data, it will block your thread until the data is ready) The advantage of this is that your thread does not have to wait for the entire length of the read operation; It can do something else until it really needs data
See the documentation
Please note that asynchronousfilechannel will be a new class in Java se 7 and has not been published yet