Top Description Fields Constructors Methods
org.jruby.util.io

public Class SelectorPool

extends Object
Class Inheritance
Imports
java.io.IOException, java.nio.channels.ClosedSelectorException, .SelectionKey, .Selector, java.nio.channels.spi.SelectorProvider, java.util.Map, .NoSuchElementException, .Queue, java.util.concurrent.ConcurrentHashMap, .ConcurrentLinkedQueue

This is a simple implementation of a hard-referenced java.nio.channels.Selector pool. It is intended to allow us to reuse a small pool of selectors rather than creating them new for each use (which causes problem for Windows and its socket-per-selector impl) or saving them per-thread (which causes problems when there are many not-quite-dead threads in flight. The selectors are kept open in the pool and you should call cleanup() for releasing selectors.
Author
headius

Field Summary

Modifier and TypeField and Description
private final Map<SelectorProvider, Queue<Selector>>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
cleanup()

Clean up a pool.

private static void
public Selector

Returns:

a java.nio.channels.Selector
get
()

Get a selector from the pool (or create a new one).

public Selector

Returns:

a java.nio.channels.Selector
get
(SelectorProvider provider)

Get a selector from the pool (or create a new one).

public void
put(Selector
the selector to put back
selector
)

Put a selector back into the pool.

private Selector
private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

poolback to summary
private final Map<SelectorProvider, Queue<Selector>> pool

Constructor Detail

SelectorPoolback to summary
public SelectorPool()

Method Detail

cleanupback to summary
public void cleanup()

Clean up a pool. All selectors in a pool are closed and the pool gets empty.

clearProviderPoolback to summary
private static void clearProviderPool(SelectorProvider provider, Queue<Selector> providerPool)
getback to summary
public Selector get() throws IOException

Get a selector from the pool (or create a new one). Selectors come from the default selector provider on the current JVM.

Returns:Selector

a java.nio.channels.Selector

Exceptions
IOException:
if there's a problem opening a new selector
getback to summary
public Selector get(SelectorProvider provider) throws IOException

Get a selector from the pool (or create a new one). Selectors come from the given provider.

Returns:Selector

a java.nio.channels.Selector

Exceptions
IOException:
if there's a problem opening a new selector
putback to summary
public void put(Selector selector)

Put a selector back into the pool.

Parameters
selector:Selector

the selector to put back

retrieveFromPoolback to summary
private Selector retrieveFromPool(SelectorProvider provider) throws IOException
returnToPoolback to summary
private void returnToPool(Selector selector)