java.nio.channels.spi
Class AbstractSelector

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.nio.channels.Selector sample code for java.nio.channels.Selector definition code for java.nio.channels.Selector 
      extended by java.nio.channels.spi.AbstractSelector

public abstract class AbstractSelector
extends Selector sample code for java.nio.channels.Selector definition code for java.nio.channels.Selector

Base implementation class for selectors.

This class encapsulates the low-level machinery required to implement the interruption of selection operations. A concrete selector class must invoke the begin sample code for java.nio.channels.spi.AbstractSelector.begin() definition code for java.nio.channels.spi.AbstractSelector.begin() and end sample code for java.nio.channels.spi.AbstractSelector.end() definition code for java.nio.channels.spi.AbstractSelector.end() methods before and after, respectively, invoking an I/O operation that might block indefinitely. In order to ensure that the end sample code for java.nio.channels.spi.AbstractSelector.end() definition code for java.nio.channels.spi.AbstractSelector.end() method is always invoked, these methods should be used within a try ... finally block:

 try {
     begin();
     // Perform blocking I/O operation here
     ...
 } finally {
     end();
 }

This class also defines methods for maintaining a selector's cancelled-key set and for removing a key from its channel's key set, and declares the abstract register sample code for java.nio.channels.spi.AbstractSelector.register(java.nio.channels.spi.AbstractSelectableChannel, int, java.lang.Object) definition code for java.nio.channels.spi.AbstractSelector.register(java.nio.channels.spi.AbstractSelectableChannel, int, java.lang.Object) method that is invoked by a selectable channel's register sample code for java.nio.channels.spi.AbstractSelectableChannel.register(java.nio.channels.Selector, int, java.lang.Object) definition code for java.nio.channels.spi.AbstractSelectableChannel.register(java.nio.channels.Selector, int, java.lang.Object) method in order to perform the actual work of registering a channel.

Since:
1.4

Constructor Summary
protected AbstractSelector sample code for java.nio.channels.spi.AbstractSelector.AbstractSelector(java.nio.channels.spi.SelectorProvider) definition code for java.nio.channels.spi.AbstractSelector.AbstractSelector(java.nio.channels.spi.SelectorProvider) (SelectorProvider sample code for java.nio.channels.spi.SelectorProvider definition code for java.nio.channels.spi.SelectorProvider  provider)
          Initializes a new instance of this class.
 
Method Summary
protected  void begin sample code for java.nio.channels.spi.AbstractSelector.begin() definition code for java.nio.channels.spi.AbstractSelector.begin() ()
          Marks the beginning of an I/O operation that might block indefinitely.
protected  Set sample code for java.util.Set definition code for java.util.Set <SelectionKey sample code for java.nio.channels.SelectionKey definition code for java.nio.channels.SelectionKey > cancelledKeys sample code for java.nio.channels.spi.AbstractSelector.cancelledKeys() definition code for java.nio.channels.spi.AbstractSelector.cancelledKeys() ()
          Retrieves this selector's cancelled-key set.
 void close sample code for java.nio.channels.spi.AbstractSelector.close() definition code for java.nio.channels.spi.AbstractSelector.close() ()
          Closes this selector.
protected  void deregister sample code for java.nio.channels.spi.AbstractSelector.deregister(java.nio.channels.spi.AbstractSelectionKey) definition code for java.nio.channels.spi.AbstractSelector.deregister(java.nio.channels.spi.AbstractSelectionKey) (AbstractSelectionKey sample code for java.nio.channels.spi.AbstractSelectionKey definition code for java.nio.channels.spi.AbstractSelectionKey  key)
          Removes the given key from its channel's key set.
protected  void end sample code for java.nio.channels.spi.AbstractSelector.end() definition code for java.nio.channels.spi.AbstractSelector.end() ()
          Marks the end of an I/O operation that might block indefinitely.
protected abstract  void implCloseSelector sample code for java.nio.channels.spi.AbstractSelector.implCloseSelector() definition code for java.nio.channels.spi.AbstractSelector.implCloseSelector() ()
          Closes this selector.
 boolean isOpen sample code for java.nio.channels.spi.AbstractSelector.isOpen() definition code for java.nio.channels.spi.AbstractSelector.isOpen() ()
          Tells whether or not this selector is open.
 SelectorProvider sample code for java.nio.channels.spi.SelectorProvider definition code for java.nio.channels.spi.SelectorProvider provider sample code for java.nio.channels.spi.AbstractSelector.provider() definition code for java.nio.channels.spi.AbstractSelector.provider() ()
          Returns the provider that created this channel.
protected abstract  SelectionKey sample code for java.nio.channels.SelectionKey definition code for java.nio.channels.SelectionKey register sample code for java.nio.channels.spi.AbstractSelector.register(java.nio.channels.spi.AbstractSelectableChannel, int, java.lang.Object) definition code for java.nio.channels.spi.AbstractSelector.register(java.nio.channels.spi.AbstractSelectableChannel, int, java.lang.Object) (AbstractSelectableChannel sample code for java.nio.channels.spi.AbstractSelectableChannel definition code for java.nio.channels.spi.AbstractSelectableChannel  ch, int ops, Object sample code for java.lang.Object definition code for java.lang.Object  att)
          Registers the given channel with this selector.
 
Methods inherited from class java.nio.channels.Selector sample code for java.nio.channels.Selector definition code for java.nio.channels.Selector
keys sample code for java.nio.channels.Selector.keys() definition code for java.nio.channels.Selector.keys() , open sample code for java.nio.channels.Selector.open() definition code for java.nio.channels.Selector.open() , select sample code for java.nio.channels.Selector.select() definition code for java.nio.channels.Selector.select() , select sample code for java.nio.channels.Selector.select(long) definition code for java.nio.channels.Selector.select(long) , selectedKeys sample code for java.nio.channels.Selector.selectedKeys() definition code for java.nio.channels.Selector.selectedKeys() , selectNow sample code for java.nio.channels.Selector.selectNow() definition code for java.nio.channels.Selector.selectNow() , wakeup sample code for java.nio.channels.Selector.wakeup() definition code for java.nio.channels.Selector.wakeup()
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Constructor Detail

AbstractSelector sample code for java.nio.channels.spi.AbstractSelector(java.nio.channels.spi.SelectorProvider) definition code for java.nio.channels.spi.AbstractSelector(java.nio.channels.spi.SelectorProvider)

protected AbstractSelector(SelectorProvider sample code for java.nio.channels.spi.SelectorProvider definition code for java.nio.channels.spi.SelectorProvider  provider)
Initializes a new instance of this class.

Method Detail

close sample code for java.nio.channels.spi.AbstractSelector.close() definition code for java.nio.channels.spi.AbstractSelector.close()

public final void close()
                 throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Closes this selector.

If the selector has already been closed then this method returns immediately. Otherwise it marks the selector as closed and then invokes the implCloseSelector sample code for java.nio.channels.spi.AbstractSelector.implCloseSelector() definition code for java.nio.channels.spi.AbstractSelector.implCloseSelector() method in order to complete the close operation.

Specified by:
close sample code for java.nio.channels.Selector.close() definition code for java.nio.channels.Selector.close() in class Selector sample code for java.nio.channels.Selector definition code for java.nio.channels.Selector
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - If an I/O error occurs

implCloseSelector sample code for java.nio.channels.spi.AbstractSelector.implCloseSelector() definition code for java.nio.channels.spi.AbstractSelector.implCloseSelector()

protected abstract void implCloseSelector()
                                   throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Closes this selector.

This method is invoked by the close sample code for java.nio.channels.spi.AbstractSelector.close() definition code for java.nio.channels.spi.AbstractSelector.close() method in order to perform the actual work of closing the selector. This method is only invoked if the selector 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 a selection operation upon this selector to return immediately as if by invoking the wakeup sample code for java.nio.channels.Selector.wakeup() definition code for java.nio.channels.Selector.wakeup() method.

Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - If an I/O error occurs while closing the selector

isOpen sample code for java.nio.channels.spi.AbstractSelector.isOpen() definition code for java.nio.channels.spi.AbstractSelector.isOpen()

public final boolean isOpen()
Description copied from class: Selector sample code for java.nio.channels.Selector.isOpen() definition code for java.nio.channels.Selector.isOpen()
Tells whether or not this selector is open.

Specified by:
isOpen sample code for java.nio.channels.Selector.isOpen() definition code for java.nio.channels.Selector.isOpen() in class Selector sample code for java.nio.channels.Selector definition code for java.nio.channels.Selector
Returns:
true if, and only if, this selector is open

provider sample code for java.nio.channels.spi.AbstractSelector.provider() definition code for java.nio.channels.spi.AbstractSelector.provider()

public final SelectorProvider sample code for java.nio.channels.spi.SelectorProvider definition code for java.nio.channels.spi.SelectorProvider  provider()
Returns the provider that created this channel.

Specified by:
provider sample code for java.nio.channels.Selector.provider() definition code for java.nio.channels.Selector.provider() in class Selector sample code for java.nio.channels.Selector definition code for java.nio.channels.Selector
Returns:
The provider that created this channel

cancelledKeys sample code for java.nio.channels.spi.AbstractSelector.cancelledKeys() definition code for java.nio.channels.spi.AbstractSelector.cancelledKeys()

protected final Set sample code for java.util.Set definition code for java.util.Set <SelectionKey sample code for java.nio.channels.SelectionKey definition code for java.nio.channels.SelectionKey > cancelledKeys()
Retrieves this selector's cancelled-key set.

This set should only be used while synchronized upon it.

Returns:
The cancelled-key set

register sample code for java.nio.channels.spi.AbstractSelector.register(java.nio.channels.spi.AbstractSelectableChannel, int, java.lang.Object) definition code for java.nio.channels.spi.AbstractSelector.register(java.nio.channels.spi.AbstractSelectableChannel, int, java.lang.Object)

protected abstract SelectionKey sample code for java.nio.channels.SelectionKey definition code for java.nio.channels.SelectionKey  register(AbstractSelectableChannel sample code for java.nio.channels.spi.AbstractSelectableChannel definition code for java.nio.channels.spi.AbstractSelectableChannel  ch,
                                         int ops,
                                         Object sample code for java.lang.Object definition code for java.lang.Object  att)
Registers the given channel with this selector.

This method is invoked by a channel's register sample code for java.nio.channels.spi.AbstractSelectableChannel.register(java.nio.channels.Selector, int, java.lang.Object) definition code for java.nio.channels.spi.AbstractSelectableChannel.register(java.nio.channels.Selector, int, java.lang.Object) method in order to perform the actual work of registering the channel with this selector.

Parameters:
ch - The channel to be registered
ops - The initial interest set, which must be valid
att - The initial attachment for the resulting key
Returns:
A new key representing the registration of the given channel with this selector

deregister sample code for java.nio.channels.spi.AbstractSelector.deregister(java.nio.channels.spi.AbstractSelectionKey) definition code for java.nio.channels.spi.AbstractSelector.deregister(java.nio.channels.spi.AbstractSelectionKey)

protected final void deregister(AbstractSelectionKey sample code for java.nio.channels.spi.AbstractSelectionKey definition code for java.nio.channels.spi.AbstractSelectionKey  key)
Removes the given key from its channel's key set.

This method must be invoked by the selector for each channel that it deregisters.

Parameters:
key - The selection key to be removed

begin sample code for java.nio.channels.spi.AbstractSelector.begin() definition code for java.nio.channels.spi.AbstractSelector.begin()

protected final void begin()
Marks the beginning of an I/O operation that might block indefinitely.

This method should be invoked in tandem with the end sample code for java.nio.channels.spi.AbstractSelector.end() definition code for java.nio.channels.spi.AbstractSelector.end() method, using a try ... finally block as shown above, in order to implement interruption for this selector.

Invoking this method arranges for the selector's wakeup sample code for java.nio.channels.Selector.wakeup() definition code for java.nio.channels.Selector.wakeup() method to be invoked if a thread's interrupt sample code for java.lang.Thread.interrupt() definition code for java.lang.Thread.interrupt() method is invoked while the thread is blocked in an I/O operation upon the selector.


end sample code for java.nio.channels.spi.AbstractSelector.end() definition code for java.nio.channels.spi.AbstractSelector.end()

protected final void end()
Marks the end of an I/O operation that might block indefinitely.

This method should be invoked in tandem with the end sample code for java.nio.channels.spi.AbstractSelector.end() definition code for java.nio.channels.spi.AbstractSelector.end() method, using a try ... finally block as shown above, in order to implement interruption for this selector.