AbstractSelectableChannel (Java Platform SE 8 ) (original) (raw)
- java.nio.channels.spi.AbstractInterruptibleChannel
- java.nio.channels.SelectableChannel
- java.nio.channels.spi.AbstractSelectableChannel
- java.nio.channels.SelectableChannel
All Implemented Interfaces:
Closeable, AutoCloseable, Channel, InterruptibleChannel
Direct Known Subclasses:
DatagramChannel, Pipe.SinkChannel, Pipe.SourceChannel, ServerSocketChannel, SocketChannel
public abstract class AbstractSelectableChannel
extends SelectableChannel
Base implementation class for selectable channels.
This class defines methods that handle the mechanics of channel registration, deregistration, and closing. It maintains the current blocking mode of this channel as well as its current set of selection keys. It performs all of the synchronization required to implement the SelectableChannel specification. Implementations of the abstract protected methods defined in this class need not synchronize against other threads that might be engaged in the same operations.
Since:
1.4
Constructor Summary
Constructors
Modifier Constructor Description protected AbstractSelectableChannel(SelectorProvider provider) Initializes a new instance of this class. Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods
Modifier and Type Method Description Object blockingLock() Retrieves the object upon which the configureBlocking and register methods synchronize. SelectableChannel configureBlocking(boolean block) Adjusts this channel's blocking mode. protected void implCloseChannel() Closes this channel. protected abstract void implCloseSelectableChannel() Closes this selectable channel. protected abstract void implConfigureBlocking(boolean block) Adjusts this channel's blocking mode. boolean isBlocking() Tells whether or not every I/O operation on this channel will block until it completes. boolean isRegistered() Tells whether or not this channel is currently registered with any selectors. SelectionKey keyFor(Selector sel) Retrieves the key representing the channel's registration with the given selector. SelectorProvider provider() Returns the provider that created this channel. SelectionKey register(Selector sel, int ops,Object att) Registers this channel with the given selector, returning a selection key. * ### Methods inherited from class java.nio.channels.[SelectableChannel](../../../../java/nio/channels/SelectableChannel.html "class in java.nio.channels") `[register](../../../../java/nio/channels/SelectableChannel.html#register-java.nio.channels.Selector-int-), [validOps](../../../../java/nio/channels/SelectableChannel.html#validOps--)` * ### Methods inherited from class java.nio.channels.spi.[AbstractInterruptibleChannel](../../../../java/nio/channels/spi/AbstractInterruptibleChannel.html "class in java.nio.channels.spi") `[begin](../../../../java/nio/channels/spi/AbstractInterruptibleChannel.html#begin--), [close](../../../../java/nio/channels/spi/AbstractInterruptibleChannel.html#close--), [end](../../../../java/nio/channels/spi/AbstractInterruptibleChannel.html#end-boolean-), [isOpen](../../../../java/nio/channels/spi/AbstractInterruptibleChannel.html#isOpen--)` * ### Methods inherited from class java.lang.[Object](../../../../java/lang/Object.html "class in java.lang") `[clone](../../../../java/lang/Object.html#clone--), [equals](../../../../java/lang/Object.html#equals-java.lang.Object-), [finalize](../../../../java/lang/Object.html#finalize--), [getClass](../../../../java/lang/Object.html#getClass--), [hashCode](../../../../java/lang/Object.html#hashCode--), [notify](../../../../java/lang/Object.html#notify--), [notifyAll](../../../../java/lang/Object.html#notifyAll--), [toString](../../../../java/lang/Object.html#toString--), [wait](../../../../java/lang/Object.html#wait--), [wait](../../../../java/lang/Object.html#wait-long-), [wait](../../../../java/lang/Object.html#wait-long-int-)` * ### Methods inherited from interface java.nio.channels.[Channel](../../../../java/nio/channels/Channel.html "interface in java.nio.channels") `[close](../../../../java/nio/channels/Channel.html#close--), [isOpen](../../../../java/nio/channels/Channel.html#isOpen--)`
Constructor Detail
* #### AbstractSelectableChannel protected AbstractSelectableChannel([SelectorProvider](../../../../java/nio/channels/spi/SelectorProvider.html "class in java.nio.channels.spi") provider) Initializes a new instance of this class. Parameters: `provider` \- The provider that created this channel
Method Detail
* #### provider public final [SelectorProvider](../../../../java/nio/channels/spi/SelectorProvider.html "class in java.nio.channels.spi") provider() Returns the provider that created this channel. Specified by: `[provider](../../../../java/nio/channels/SelectableChannel.html#provider--)` in class `[SelectableChannel](../../../../java/nio/channels/SelectableChannel.html "class in java.nio.channels")` Returns: The provider that created this channel * #### isRegistered public final boolean isRegistered() Tells whether or not this channel is currently registered with any selectors. A newly-created channel is not registered. Due to the inherent delay between key cancellation and channel deregistration, a channel may remain registered for some time after all of its keys have been cancelled. A channel may also remain registered for some time after it is closed. Specified by: `[isRegistered](../../../../java/nio/channels/SelectableChannel.html#isRegistered--)` in class `[SelectableChannel](../../../../java/nio/channels/SelectableChannel.html "class in java.nio.channels")` Returns: true if, and only if, this channel is registered * #### keyFor public final [SelectionKey](../../../../java/nio/channels/SelectionKey.html "class in java.nio.channels") keyFor([Selector](../../../../java/nio/channels/Selector.html "class in java.nio.channels") sel) Retrieves the key representing the channel's registration with the given selector. Specified by: `[keyFor](../../../../java/nio/channels/SelectableChannel.html#keyFor-java.nio.channels.Selector-)` in class `[SelectableChannel](../../../../java/nio/channels/SelectableChannel.html "class in java.nio.channels")` Parameters: `sel` \- The selector Returns: The key returned when this channel was last registered with the given selector, or null if this channel is not currently registered with that selector * #### register public final [SelectionKey](../../../../java/nio/channels/SelectionKey.html "class in java.nio.channels") register([Selector](../../../../java/nio/channels/Selector.html "class in java.nio.channels") sel, int ops, [Object](../../../../java/lang/Object.html "class in java.lang") att) throws [ClosedChannelException](../../../../java/nio/channels/ClosedChannelException.html "class in java.nio.channels") Registers this channel with the given selector, returning a selection key. This method first verifies that this channel is open and that the given initial interest set is valid. If this channel is already registered with the given selector then the selection key representing that registration is returned after setting its interest set to the given value. Otherwise this channel has not yet been registered with the given selector, so the [register](../../../../java/nio/channels/spi/AbstractSelector.html#register-java.nio.channels.spi.AbstractSelectableChannel-int-java.lang.Object-) method of the selector is invoked while holding the appropriate locks. The resulting key is added to this channel's key set before being returned. Specified by: `[register](../../../../java/nio/channels/SelectableChannel.html#register-java.nio.channels.Selector-int-java.lang.Object-)` in class `[SelectableChannel](../../../../java/nio/channels/SelectableChannel.html "class in java.nio.channels")` Parameters: `sel` \- The selector with which this channel is to be registered `ops` \- The interest set for the resulting key `att` \- The attachment for the resulting key; may be null Returns: A key representing the registration of this channel with the given selector Throws: `[ClosedSelectorException](../../../../java/nio/channels/ClosedSelectorException.html "class in java.nio.channels")` \- If the selector is closed `[IllegalBlockingModeException](../../../../java/nio/channels/IllegalBlockingModeException.html "class in java.nio.channels")` \- If this channel is in blocking mode `[IllegalSelectorException](../../../../java/nio/channels/IllegalSelectorException.html "class in java.nio.channels")` \- If this channel was not created by the same provider as the given selector `[CancelledKeyException](../../../../java/nio/channels/CancelledKeyException.html "class in java.nio.channels")` \- If this channel is currently registered with the given selector but the corresponding key has already been cancelled `[IllegalArgumentException](../../../../java/lang/IllegalArgumentException.html "class in java.lang")` \- If a bit in the ops set does not correspond to an operation that is supported by this channel, that is, if`set & ~validOps() != 0` `[ClosedChannelException](../../../../java/nio/channels/ClosedChannelException.html "class in java.nio.channels")` \- If this channel is closed * #### implCloseChannel protected final void implCloseChannel() throws [IOException](../../../../java/io/IOException.html "class in java.io") Specified by: `[implCloseChannel](../../../../java/nio/channels/spi/AbstractInterruptibleChannel.html#implCloseChannel--)` in class `[AbstractInterruptibleChannel](../../../../java/nio/channels/spi/AbstractInterruptibleChannel.html "class in java.nio.channels.spi")` Throws: `[IOException](../../../../java/io/IOException.html "class in java.io")` \- If an I/O error occurs while closing the channel * #### implCloseSelectableChannel protected abstract void implCloseSelectableChannel() throws [IOException](../../../../java/io/IOException.html "class in java.io") Closes this selectable channel. This method is invoked by the [close](../../../../java/nio/channels/Channel.html#close--) method in order to perform the actual work of closing the channel. This method is only invoked if the channel has not yet been closed, and it is never invoked more than once. An implementation of this method must arrange for any other thread that is blocked in an I/O operation upon this channel to return immediately, either by throwing an exception or by returning normally. Throws: `[IOException](../../../../java/io/IOException.html "class in java.io")` \- If an I/O error occurs * #### isBlocking public final boolean isBlocking() Tells whether or not every I/O operation on this channel will block until it completes. A newly-created channel is always in blocking mode. If this channel is closed then the value returned by this method is not specified. Specified by: `[isBlocking](../../../../java/nio/channels/SelectableChannel.html#isBlocking--)` in class `[SelectableChannel](../../../../java/nio/channels/SelectableChannel.html "class in java.nio.channels")` Returns: true if, and only if, this channel is in blocking mode * #### blockingLock public final [Object](../../../../java/lang/Object.html "class in java.lang") blockingLock() Retrieves the object upon which the [configureBlocking](../../../../java/nio/channels/SelectableChannel.html#configureBlocking-boolean-) and [register](../../../../java/nio/channels/SelectableChannel.html#register-java.nio.channels.Selector-int-java.lang.Object-) methods synchronize. This is often useful in the implementation of adaptors that require a specific blocking mode to be maintained for a short period of time. Specified by: `[blockingLock](../../../../java/nio/channels/SelectableChannel.html#blockingLock--)` in class `[SelectableChannel](../../../../java/nio/channels/SelectableChannel.html "class in java.nio.channels")` Returns: The blocking-mode lock object * #### configureBlocking public final [SelectableChannel](../../../../java/nio/channels/SelectableChannel.html "class in java.nio.channels") configureBlocking(boolean block) throws [IOException](../../../../java/io/IOException.html "class in java.io") Adjusts this channel's blocking mode. If the given blocking mode is different from the current blocking mode then this method invokes the [implConfigureBlocking](../../../../java/nio/channels/spi/AbstractSelectableChannel.html#implConfigureBlocking-boolean-) method, while holding the appropriate locks, in order to change the mode. Specified by: `[configureBlocking](../../../../java/nio/channels/SelectableChannel.html#configureBlocking-boolean-)` in class `[SelectableChannel](../../../../java/nio/channels/SelectableChannel.html "class in java.nio.channels")` Parameters: `block` \- If true then this channel will be placed in blocking mode; if false then it will be placed non-blocking mode Returns: This selectable channel Throws: `[ClosedChannelException](../../../../java/nio/channels/ClosedChannelException.html "class in java.nio.channels")` \- If this channel is closed `[IOException](../../../../java/io/IOException.html "class in java.io")` \- If an I/O error occurs * #### implConfigureBlocking protected abstract void implConfigureBlocking(boolean block) throws [IOException](../../../../java/io/IOException.html "class in java.io") Adjusts this channel's blocking mode. This method is invoked by the [configureBlocking](../../../../java/nio/channels/spi/AbstractSelectableChannel.html#configureBlocking-boolean-) method in order to perform the actual work of changing the blocking mode. This method is only invoked if the new mode is different from the current mode. Parameters: `block` \- If true then this channel will be placed in blocking mode; if false then it will be placed non-blocking mode Throws: `[IOException](../../../../java/io/IOException.html "class in java.io")` \- If an I/O error occurs
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2025, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.