Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/nio/channels/SelectableChannel.java
38918 views
/*1* Copyright (c) 2000, 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.nio.channels.spi.AbstractInterruptibleChannel;29import java.nio.channels.spi.SelectorProvider;303132/**33* A channel that can be multiplexed via a {@link Selector}.34*35* <p> In order to be used with a selector, an instance of this class must36* first be <i>registered</i> via the {@link #register(Selector,int,Object)37* register} method. This method returns a new {@link SelectionKey} object38* that represents the channel's registration with the selector.39*40* <p> Once registered with a selector, a channel remains registered until it41* is <i>deregistered</i>. This involves deallocating whatever resources were42* allocated to the channel by the selector.43*44* <p> A channel cannot be deregistered directly; instead, the key representing45* its registration must be <i>cancelled</i>. Cancelling a key requests that46* the channel be deregistered during the selector's next selection operation.47* A key may be cancelled explicitly by invoking its {@link48* SelectionKey#cancel() cancel} method. All of a channel's keys are cancelled49* implicitly when the channel is closed, whether by invoking its {@link50* Channel#close close} method or by interrupting a thread blocked in an I/O51* operation upon the channel.52*53* <p> If the selector itself is closed then the channel will be deregistered,54* and the key representing its registration will be invalidated, without55* further delay.56*57* <p> A channel may be registered at most once with any particular selector.58*59* <p> Whether or not a channel is registered with one or more selectors may be60* determined by invoking the {@link #isRegistered isRegistered} method.61*62* <p> Selectable channels are safe for use by multiple concurrent63* threads. </p>64*65*66* <a name="bm"></a>67* <h2>Blocking mode</h2>68*69* A selectable channel is either in <i>blocking</i> mode or in70* <i>non-blocking</i> mode. In blocking mode, every I/O operation invoked71* upon the channel will block until it completes. In non-blocking mode an I/O72* operation will never block and may transfer fewer bytes than were requested73* or possibly no bytes at all. The blocking mode of a selectable channel may74* be determined by invoking its {@link #isBlocking isBlocking} method.75*76* <p> Newly-created selectable channels are always in blocking mode.77* Non-blocking mode is most useful in conjunction with selector-based78* multiplexing. A channel must be placed into non-blocking mode before being79* registered with a selector, and may not be returned to blocking mode until80* it has been deregistered.81*82*83* @author Mark Reinhold84* @author JSR-51 Expert Group85* @since 1.486*87* @see SelectionKey88* @see Selector89*/9091public abstract class SelectableChannel92extends AbstractInterruptibleChannel93implements Channel94{9596/**97* Initializes a new instance of this class.98*/99protected SelectableChannel() { }100101/**102* Returns the provider that created this channel.103*104* @return The provider that created this channel105*/106public abstract SelectorProvider provider();107108/**109* Returns an <a href="SelectionKey.html#opsets">operation set</a>110* identifying this channel's supported operations. The bits that are set111* in this integer value denote exactly the operations that are valid for112* this channel. This method always returns the same value for a given113* concrete channel class.114*115* @return The valid-operation set116*/117public abstract int validOps();118119// Internal state:120// keySet, may be empty but is never null, typ. a tiny array121// boolean isRegistered, protected by key set122// regLock, lock object to prevent duplicate registrations123// blocking mode, protected by regLock124125/**126* Tells whether or not this channel is currently registered with any127* selectors. A newly-created channel is not registered.128*129* <p> Due to the inherent delay between key cancellation and channel130* deregistration, a channel may remain registered for some time after all131* of its keys have been cancelled. A channel may also remain registered132* for some time after it is closed. </p>133*134* @return <tt>true</tt> if, and only if, this channel is registered135*/136public abstract boolean isRegistered();137//138// sync(keySet) { return isRegistered; }139140/**141* Retrieves the key representing the channel's registration with the given142* selector.143*144* @param sel145* The selector146*147* @return The key returned when this channel was last registered with the148* given selector, or <tt>null</tt> if this channel is not149* currently registered with that selector150*/151public abstract SelectionKey keyFor(Selector sel);152//153// sync(keySet) { return findKey(sel); }154155/**156* Registers this channel with the given selector, returning a selection157* key.158*159* <p> If this channel is currently registered with the given selector then160* the selection key representing that registration is returned. The key's161* interest set will have been changed to <tt>ops</tt>, as if by invoking162* the {@link SelectionKey#interestOps(int) interestOps(int)} method. If163* the <tt>att</tt> argument is not <tt>null</tt> then the key's attachment164* will have been set to that value. A {@link CancelledKeyException} will165* be thrown if the key has already been cancelled.166*167* <p> Otherwise this channel has not yet been registered with the given168* selector, so it is registered and the resulting new key is returned.169* The key's initial interest set will be <tt>ops</tt> and its attachment170* will be <tt>att</tt>.171*172* <p> This method may be invoked at any time. If this method is invoked173* while another invocation of this method or of the {@link174* #configureBlocking(boolean) configureBlocking} method is in progress175* then it will first block until the other operation is complete. This176* method will then synchronize on the selector's key set and therefore may177* block if invoked concurrently with another registration or selection178* operation involving the same selector. </p>179*180* <p> If this channel is closed while this operation is in progress then181* the key returned by this method will have been cancelled and will182* therefore be invalid. </p>183*184* @param sel185* The selector with which this channel is to be registered186*187* @param ops188* The interest set for the resulting key189*190* @param att191* The attachment for the resulting key; may be <tt>null</tt>192*193* @throws ClosedChannelException194* If this channel is closed195*196* @throws ClosedSelectorException197* If the selector is closed198*199* @throws IllegalBlockingModeException200* If this channel is in blocking mode201*202* @throws IllegalSelectorException203* If this channel was not created by the same provider204* as the given selector205*206* @throws CancelledKeyException207* If this channel is currently registered with the given selector208* but the corresponding key has already been cancelled209*210* @throws IllegalArgumentException211* If a bit in the <tt>ops</tt> set does not correspond to an212* operation that is supported by this channel, that is, if213* {@code set & ~validOps() != 0}214*215* @return A key representing the registration of this channel with216* the given selector217*/218public abstract SelectionKey register(Selector sel, int ops, Object att)219throws ClosedChannelException;220//221// sync(regLock) {222// sync(keySet) { look for selector }223// if (channel found) { set interest ops -- may block in selector;224// return key; }225// create new key -- may block somewhere in selector;226// sync(keySet) { add key; }227// attach(attachment);228// return key;229// }230231/**232* Registers this channel with the given selector, returning a selection233* key.234*235* <p> An invocation of this convenience method of the form236*237* <blockquote><tt>sc.register(sel, ops)</tt></blockquote>238*239* behaves in exactly the same way as the invocation240*241* <blockquote><tt>sc.{@link242* #register(java.nio.channels.Selector,int,java.lang.Object)243* register}(sel, ops, null)</tt></blockquote>244*245* @param sel246* The selector with which this channel is to be registered247*248* @param ops249* The interest set for the resulting key250*251* @throws ClosedChannelException252* If this channel is closed253*254* @throws ClosedSelectorException255* If the selector is closed256*257* @throws IllegalBlockingModeException258* If this channel is in blocking mode259*260* @throws IllegalSelectorException261* If this channel was not created by the same provider262* as the given selector263*264* @throws CancelledKeyException265* If this channel is currently registered with the given selector266* but the corresponding key has already been cancelled267*268* @throws IllegalArgumentException269* If a bit in <tt>ops</tt> does not correspond to an operation270* that is supported by this channel, that is, if {@code set &271* ~validOps() != 0}272*273* @return A key representing the registration of this channel with274* the given selector275*/276public final SelectionKey register(Selector sel, int ops)277throws ClosedChannelException278{279return register(sel, ops, null);280}281282/**283* Adjusts this channel's blocking mode.284*285* <p> If this channel is registered with one or more selectors then an286* attempt to place it into blocking mode will cause an {@link287* IllegalBlockingModeException} to be thrown.288*289* <p> This method may be invoked at any time. The new blocking mode will290* only affect I/O operations that are initiated after this method returns.291* For some implementations this may require blocking until all pending I/O292* operations are complete.293*294* <p> If this method is invoked while another invocation of this method or295* of the {@link #register(Selector, int) register} method is in progress296* then it will first block until the other operation is complete. </p>297*298* @param block If <tt>true</tt> then this channel will be placed in299* blocking mode; if <tt>false</tt> then it will be placed300* non-blocking mode301*302* @return This selectable channel303*304* @throws ClosedChannelException305* If this channel is closed306*307* @throws IllegalBlockingModeException308* If <tt>block</tt> is <tt>true</tt> and this channel is309* registered with one or more selectors310*311* @throws IOException312* If an I/O error occurs313*/314public abstract SelectableChannel configureBlocking(boolean block)315throws IOException;316//317// sync(regLock) {318// sync(keySet) { throw IBME if block && isRegistered; }319// change mode;320// }321322/**323* Tells whether or not every I/O operation on this channel will block324* until it completes. A newly-created channel is always in blocking mode.325*326* <p> If this channel is closed then the value returned by this method is327* not specified. </p>328*329* @return <tt>true</tt> if, and only if, this channel is in blocking mode330*/331public abstract boolean isBlocking();332333/**334* Retrieves the object upon which the {@link #configureBlocking335* configureBlocking} and {@link #register register} methods synchronize.336* This is often useful in the implementation of adaptors that require a337* specific blocking mode to be maintained for a short period of time.338*339* @return The blocking-mode lock object340*/341public abstract Object blockingLock();342343}344345346