Path: blob/master/src/java.base/share/classes/java/nio/channels/SelectableChannel.java
41159 views
/*1* Copyright (c) 2000, 2018, 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 id="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/**120* Tells whether or not this channel is currently registered with any121* selectors. A newly-created channel is not registered.122*123* <p> Due to the inherent delay between key cancellation and channel124* deregistration, a channel may remain registered for some time after all125* of its keys have been cancelled. A channel may also remain registered126* for some time after it is closed. </p>127*128* @return {@code true} if, and only if, this channel is registered129*/130public abstract boolean isRegistered();131132/**133* Retrieves the key representing the channel's registration with the given134* selector.135*136* @param sel137* The selector138*139* @return The key returned when this channel was last registered with the140* given selector, or {@code null} if this channel is not141* currently registered with that selector142*/143public abstract SelectionKey keyFor(Selector sel);144145/**146* Registers this channel with the given selector, returning a selection147* key.148*149* <p> If this channel is currently registered with the given selector then150* the selection key representing that registration is returned. The key's151* interest set will have been changed to {@code ops}, as if by invoking152* the {@link SelectionKey#interestOps(int) interestOps(int)} method. If153* the {@code att} argument is not {@code null} then the key's attachment154* will have been set to that value. A {@link CancelledKeyException} will155* be thrown if the key has already been cancelled.156*157* <p> Otherwise this channel has not yet been registered with the given158* selector, so it is registered and the resulting new key is returned.159* The key's initial interest set will be {@code ops} and its attachment160* will be {@code att}.161*162* <p> This method may be invoked at any time. If this method is invoked163* while a selection operation is in progress then it has no effect upon164* that operation; the new registration or change to the key's interest set165* will be seen by the next selection operation. If this method is invoked166* while an invocation of {@link #configureBlocking(boolean) configureBlocking}167* is in progress then it will block until the channel's blocking mode has168* been adjusted.169*170* <p> If this channel is closed while this operation is in progress then171* the key returned by this method will have been cancelled and will172* therefore be invalid. </p>173*174* @param sel175* The selector with which this channel is to be registered176*177* @param ops178* The interest set for the resulting key179*180* @param att181* The attachment for the resulting key; may be {@code null}182*183* @throws ClosedChannelException184* If this channel is closed185*186* @throws ClosedSelectorException187* If the selector is closed188*189* @throws IllegalBlockingModeException190* If this channel is in blocking mode191*192* @throws IllegalSelectorException193* If this channel was not created by the same provider194* as the given selector195*196* @throws CancelledKeyException197* If this channel is currently registered with the given selector198* but the corresponding key has already been cancelled199*200* @throws IllegalArgumentException201* If a bit in the {@code ops} set does not correspond to an202* operation that is supported by this channel, that is, if203* {@code set & ~validOps() != 0}204*205* @return A key representing the registration of this channel with206* the given selector207*/208public abstract SelectionKey register(Selector sel, int ops, Object att)209throws ClosedChannelException;210211/**212* Registers this channel with the given selector, returning a selection213* key.214*215* <p> An invocation of this convenience method of the form216*217* <blockquote>{@code sc.register(sel, ops)}</blockquote>218*219* behaves in exactly the same way as the invocation220*221* <blockquote>{@code sc.}{@link222* #register(java.nio.channels.Selector,int,java.lang.Object)223* register(sel, ops, null)}</blockquote>224*225* @param sel226* The selector with which this channel is to be registered227*228* @param ops229* The interest set for the resulting key230*231* @throws ClosedChannelException232* If this channel is closed233*234* @throws ClosedSelectorException235* If the selector is closed236*237* @throws IllegalBlockingModeException238* If this channel is in blocking mode239*240* @throws IllegalSelectorException241* If this channel was not created by the same provider242* as the given selector243*244* @throws CancelledKeyException245* If this channel is currently registered with the given selector246* but the corresponding key has already been cancelled247*248* @throws IllegalArgumentException249* If a bit in {@code ops} does not correspond to an operation250* that is supported by this channel, that is, if {@code set &251* ~validOps() != 0}252*253* @return A key representing the registration of this channel with254* the given selector255*/256public final SelectionKey register(Selector sel, int ops)257throws ClosedChannelException258{259return register(sel, ops, null);260}261262/**263* Adjusts this channel's blocking mode.264*265* <p> If this channel is registered with one or more selectors then an266* attempt to place it into blocking mode will cause an {@link267* IllegalBlockingModeException} to be thrown.268*269* <p> This method may be invoked at any time. The new blocking mode will270* only affect I/O operations that are initiated after this method returns.271* For some implementations this may require blocking until all pending I/O272* operations are complete.273*274* <p> If this method is invoked while another invocation of this method or275* of the {@link #register(Selector, int) register} method is in progress276* then it will first block until the other operation is complete. </p>277*278* @param block If {@code true} then this channel will be placed in279* blocking mode; if {@code false} then it will be placed280* non-blocking mode281*282* @return This selectable channel283*284* @throws ClosedChannelException285* If this channel is closed286*287* @throws IllegalBlockingModeException288* If {@code block} is {@code true} and this channel is289* registered with one or more selectors290*291* @throws IOException292* If an I/O error occurs293*/294public abstract SelectableChannel configureBlocking(boolean block)295throws IOException;296297/**298* Tells whether or not every I/O operation on this channel will block299* until it completes. A newly-created channel is always in blocking mode.300*301* <p> If this channel is closed then the value returned by this method is302* not specified. </p>303*304* @return {@code true} if, and only if, this channel is in blocking mode305*/306public abstract boolean isBlocking();307308/**309* Retrieves the object upon which the {@link #configureBlocking310* configureBlocking} and {@link #register register} methods synchronize.311* This is often useful in the implementation of adaptors that require a312* specific blocking mode to be maintained for a short period of time.313*314* @return The blocking-mode lock object315*/316public abstract Object blockingLock();317318}319320321