FileDocCategorySizeDatePackage
AbstractSelector.javaAPI DocJava SE 6 API6732Tue Jun 10 00:25:42 BST 2008java.nio.channels.spi

AbstractSelector

public abstract class AbstractSelector extends 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 {@link #begin begin} and {@link #end end} methods before and after, respectively, invoking an I/O operation that might block indefinitely. In order to ensure that the {@link #end 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 {@link #register register} method that is invoked by a selectable channel's {@link AbstractSelectableChannel#register register} method in order to perform the actual work of registering a channel.

author
Mark Reinhold
author
JSR-51 Expert Group
version
1.21, 05/11/17
since
1.4

Fields Summary
private AtomicBoolean
selectorOpen
private final SelectorProvider
provider
private final Set
cancelledKeys
private Interruptible
interruptor
Constructors Summary
protected AbstractSelector(SelectorProvider provider)
Initializes a new instance of this class.


                  
       
	this.provider = provider;
    
Methods Summary
protected final voidbegin()
Marks the beginning of an I/O operation that might block indefinitely.

This method should be invoked in tandem with the {@link #end 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 {@link Selector#wakeup wakeup} method to be invoked if a thread's {@link Thread#interrupt interrupt} method is invoked while the thread is blocked in an I/O operation upon the selector.


                                                                                        
        
	if (interruptor == null) {
	    interruptor = new Interruptible() {
		    public void interrupt() {
			AbstractSelector.this.wakeup();
		    }};
	}
	AbstractInterruptibleChannel.blockedOn(interruptor);
	if (Thread.currentThread().isInterrupted())
	    interruptor.interrupt();
    
voidcancel(java.nio.channels.SelectionKey k)


       			// package-private
	synchronized (cancelledKeys) {
	    cancelledKeys.add(k);
	}
    
protected final java.util.SetcancelledKeys()
Retrieves this selector's cancelled-key set.

This set should only be used while synchronized upon it.

return
The cancelled-key set

	return cancelledKeys;
    
public final voidclose()
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 {@link #implCloseSelector implCloseSelector} method in order to complete the close operation.

throws
IOException If an I/O error occurs

        boolean open = selectorOpen.getAndSet(false);
        if (!open)
            return;
        implCloseSelector();
    
protected final voidderegister(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.

param
key The selection key to be removed

	((AbstractSelectableChannel)key.channel()).removeKey(key);
    
protected final voidend()
Marks the end of an I/O operation that might block indefinitely.

This method should be invoked in tandem with the {@link #begin begin} method, using a try ... finally block as shown above, in order to implement interruption for this selector.

	AbstractInterruptibleChannel.blockedOn(null);
    
protected abstract voidimplCloseSelector()
Closes this selector.

This method is invoked by the {@link #close 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 {@link java.nio.channels.Selector#wakeup wakeup} method.

throws
IOException If an I/O error occurs while closing the selector

public final booleanisOpen()

	return selectorOpen.get();
    
public final java.nio.channels.spi.SelectorProviderprovider()
Returns the provider that created this channel.

return
The provider that created this channel

	return provider;
    
protected abstract java.nio.channels.SelectionKeyregister(java.nio.channels.spi.AbstractSelectableChannel ch, int ops, java.lang.Object att)
Registers the given channel with this selector.

This method is invoked by a channel's {@link AbstractSelectableChannel#register register} method in order to perform the actual work of registering the channel with this selector.

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