Path: blob/master/src/java.base/share/classes/java/nio/channels/AsynchronousChannel.java
41159 views
/*1* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.nio.channels;2627import java.io.IOException;28import java.util.concurrent.Future; // javadoc2930/**31* A channel that supports asynchronous I/O operations. Asynchronous I/O32* operations will usually take one of two forms:33*34* <ol>35* <li><pre>{@link Future}<V> <em>operation</em>(<em>...</em>)</pre></li>36* <li><pre>void <em>operation</em>(<em>...</em> A attachment, {@link37* CompletionHandler}<V,? super A> handler)</pre></li>38* </ol>39*40* where <i>operation</i> is the name of the I/O operation (read or write for41* example), <i>V</i> is the result type of the I/O operation, and <i>A</i> is42* the type of an object attached to the I/O operation to provide context when43* consuming the result. The attachment is important for cases where a44* <em>state-less</em> {@code CompletionHandler} is used to consume the result45* of many I/O operations.46*47* <p> In the first form, the methods defined by the {@link Future Future}48* interface may be used to check if the operation has completed, wait for its49* completion, and to retrieve the result. In the second form, a {@link50* CompletionHandler} is invoked to consume the result of the I/O operation when51* it completes or fails.52*53* <p> A channel that implements this interface is <em>asynchronously54* closeable</em>: If an I/O operation is outstanding on the channel and the55* channel's {@link #close close} method is invoked, then the I/O operation56* fails with the exception {@link AsynchronousCloseException}.57*58* <p> Asynchronous channels are safe for use by multiple concurrent threads.59* Some channel implementations may support concurrent reading and writing, but60* may not allow more than one read and one write operation to be outstanding at61* any given time.62*63* <h2>Cancellation</h2>64*65* <p> The {@code Future} interface defines the {@link Future#cancel cancel}66* method to cancel execution. This causes all threads waiting on the result of67* the I/O operation to throw {@link java.util.concurrent.CancellationException}.68* Whether the underlying I/O operation can be cancelled is highly implementation69* specific and therefore not specified. Where cancellation leaves the channel,70* or the entity to which it is connected, in an inconsistent state, then the71* channel is put into an implementation specific <em>error state</em> that72* prevents further attempts to initiate I/O operations that are <i>similar</i>73* to the operation that was cancelled. For example, if a read operation is74* cancelled but the implementation cannot guarantee that bytes have not been75* read from the channel then it puts the channel into an error state; further76* attempts to initiate a {@code read} operation cause an unspecified runtime77* exception to be thrown. Similarly, if a write operation is cancelled but the78* implementation cannot guarantee that bytes have not been written to the79* channel then subsequent attempts to initiate a {@code write} will fail with80* an unspecified runtime exception.81*82* <p> Where the {@link Future#cancel cancel} method is invoked with the {@code83* mayInterruptIfRunning} parameter set to {@code true} then the I/O operation84* may be interrupted by closing the channel. In that case all threads waiting85* on the result of the I/O operation throw {@code CancellationException} and86* any other I/O operations outstanding on the channel complete with the87* exception {@link AsynchronousCloseException}.88*89* <p> Where the {@code cancel} method is invoked to cancel read or write90* operations then it is recommended that all buffers used in the I/O operations91* be discarded or care taken to ensure that the buffers are not accessed while92* the channel remains open.93*94* @since 1.795*/9697public interface AsynchronousChannel98extends Channel99{100/**101* Closes this channel.102*103* <p> Any outstanding asynchronous operations upon this channel will104* complete with the exception {@link AsynchronousCloseException}. After a105* channel is closed, further attempts to initiate asynchronous I/O106* operations complete immediately with cause {@link ClosedChannelException}.107*108* <p> This method otherwise behaves exactly as specified by the {@link109* Channel} interface.110*111* @throws IOException112* If an I/O error occurs113*/114@Override115void close() throws IOException;116}117118119