An asynchronous file channel is created when a file is opened by invoking one of the {@link #open open} methods defined by this class. The file containsa variable-length sequence of bytes that can be read and written and whose current size can be {@link #size() queried}. The size of the file increases when bytes are written beyond its current size; the size of the file decreases when it is {@link #truncate truncated}.
An asynchronous file channel does not have a current position within the file. Instead, the file position is specified to each read and write method that initiates asynchronous operations. A {@link CompletionHandler}is specified as a parameter and is invoked to consume the result of the I/O operation. This class also defines read and write methods that initiate asynchronous operations, returning a {@link Future} to represent the pendingresult of the operation. The {@code Future} may be used to check if theoperation has completed, wait for its completion, and retrieve the result.
In addition to read and write operations, this class defines the following operations:
Updates made to a file may be {@link #force forcedout} to the underlying storage device, ensuring that data are notlost in the event of a system crash.
A region of a file may be {@link #lock locked} againstaccess by other programs.
An {@code AsynchronousFileChannel} is associated with a thread pool towhich tasks are submitted to handle I/O events and dispatch to completion handlers that consume the results of I/O operations on the channel. The completion handler for an I/O operation initiated on a channel is guaranteed to be invoked by one of the threads in the thread pool (This ensures that the completion handler is run by a thread with the expected identity). Where an I/O operation completes immediately, and the initiating thread is itself a thread in the thread pool, then the completion handler may be invoked directly by the initiating thread. When an {@code AsynchronousFileChannel} iscreated without specifying a thread pool then the channel is associated with a system-dependent default thread pool that may be shared with other channels. The default thread pool is configured by the system properties defined by the {@link AsynchronousChannelGroup} class.
Channels of this type are safe for use by multiple concurrent threads. The {@link Channel#close close} method may be invoked at any time, as specifiedby the {@link Channel} interface. This causes all outstanding asynchronousoperations on the channel to complete with the exception {@link AsynchronousCloseException}. Multiple read and write operations may be outstanding at the same time. When multiple read and write operations are outstanding then the ordering of the I/O operations, and the order that the completion handlers are invoked, is not specified; they are not, in particular, guaranteed to execute in the order that the operations were initiated. The {@link java.nio.ByteBuffer ByteBuffers} used when reading or writing are notsafe for use by multiple concurrent I/O operations. Furthermore, after an I/O operation is initiated then care should be taken to ensure that the buffer is not accessed until after the operation has completed.
As with {@link FileChannel}, the view of a file provided by an instance of this class is guaranteed to be consistent with other views of the same file provided by other instances in the same program. The view provided by an instance of this class may or may not, however, be consistent with the views seen by other concurrently-running programs due to caching performed by the underlying operating system and delays induced by network-filesystem protocols. This is true regardless of the language in which these other programs are written, and whether they are running on the same machine or on some other machine. The exact nature of any such inconsistencies are system-dependent and are therefore unspecified. @since 1.7
|
|
|
|
|
|
|
|
|
|