Java Reference
In-Depth Information
Watching C:\kishori for events.
ENTRY_DELETE occurred on temp
ENTRY_CREATE occurred on hello.txt
ENTRY_MODIFY occurred on hello.txt
Asynchronous File I/O
NIO.2 supports asynchronous file I/O. In a synchronous file I/O, the thread that requests the I/O operation waits
until the I/O operation is complete. In an asynchronous file I/O, the Java application requests the system for an I/O
operation and the operation is performed by the system asynchronously. When the system is performing the file I/O
operation, the application continues doing other work. When the system finishes the file I/O, it notifies the application
about the completion of its request.
The asynchronous file I/O model is scalable as compared to the synchronous file I/O model. The requests for an
asynchronous file I/O and the completion notification to the application are performed by a pool of threads that are
specially created for this purpose. The asynchronous file I/O API has options to let you use the default thread pool
or a custom thread pool. It offers enhanced scalability by using a predefined dedicated pool of threads to handle all
asynchronous file I/O operations, instead of creating a new thread for each I/O operation.
An instance of the java.nio.channels.AsynchronousFileChannel class represents an asynchronous file channel that
is used to read, write, and perform other operations on a file asynchronously. Multiple I/O operations can be performed
simultaneously on an asynchronous file channel. An asynchronous file channel does not maintain a current position where
a read or a write operation starts. You need to provide the position for each read and write operation with each request.
The static open() method of the AsynchronousFileChannel class is used to get an instance of the
AsynchronousFileChannel class. The method is overloaded. One version uses the default thread pool to handle the
I/O operations and the completion notification. Another version lets you specify an ExecutorService to which the
asynchronous tasks will be submitted for handling the I/O operations and the completion notifications. The following
snippet of code gets an AsynchronousFileChannel on a file for writing. It creates the file if the file does not exist.
// Get a Path object
Path path = Paths.get("C:\\poems\\rainbow.txt");
// Get an asynchronous file channel for WRITE.
// Create the file, if it does not exist
AsynchronousFileChannel afc = AsynchronousFileChannel.open(path, WRITE, CREATE);
The AsynchronousFileChannel provides two ways to handle the result of an asynchronous file I/O operation.
Using a
java.util.concurrent.Future object.
java.nio.channels.CompletionHandler object.
Each method of the AsynchronousFileChannel class that supports asynchronous file I/O operation has two
versions. One version returns a Future object, which you can use to handle the result of the requested asynchronous
operation. The get() method of the Future object returns the number of bytes written to the file channel. The
following snippet of code uses the version of the write() method that returns a Future object:
Using a
// Get the data to write in a ByteBuffer
ByteBuffer dataBuffer = get a byte buffer filled with data;
// Perform the asynchronous write operation
long startPosition = 0;
Future<Integer> result = afc.write(dataBuffer, startPosition);
 
Search WWH ::




Custom Search