# Class NIOServerCnxnFactory.SelectorThread

java.lang.Object
java.lang.Thread
org.apache.zookeeper.server.ZooKeeperThread
org.apache.zookeeper.server.NIOServerCnxnFactory.SelectorThread

All Implemented Interfaces:
  `Runnable`

Enclosing class:
  `NIOServerCnxnFactory`

public class NIOServerCnxnFactory.SelectorThread extends ZooKeeperThread
The SelectorThread receives newly accepted connections from the AcceptThread and is responsible for selecting for I/O readiness across the connections. This thread is the only thread that performs any non-threadsafe or potentially blocking calls on the selector (registering new connections and reading/writing interest ops). Assignment of a connection to a SelectorThread is permanent and only one SelectorThread will ever interact with the connection. There are 1-N SelectorThreads, with connections evenly apportioned between the SelectorThreads. If there is a worker thread pool, when a connection has I/O to perform the SelectorThread removes it from selection by clearing its interest ops and schedules the I/O for processing by a worker thread. When the work is complete, the connection is placed on the ready queue to have its interest ops restored and resume selection. If there is no worker thread pool, the SelectorThread performs the I/O directly.

## Nested Class Summary

### Nested classes/interfaces inherited from class Thread

`Thread.State, Thread.UncaughtExceptionHandler`

## Field Summary

Fields
Modifier and Type
Field
Description
`protected final Selector`
`selector`

### Fields inherited from class Thread

`MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY`

## Constructor Summary

Constructors
Constructor
Description
`SelectorThread(int id)`

## Method Summary

Modifier and Type
Method
Description
`boolean`
`addAcceptedConnection(SocketChannel accepted)`
Place new accepted connection onto a queue for adding.
`boolean`
`addInterestOpsUpdateRequest(SelectionKey sk)`
Place interest op update requests onto a queue so that only the selector thread modifies interest ops, because interest ops reads/sets are potentially blocking operations if other select operations are happening.
`protected void`
`cleanupSelectionKey(SelectionKey key)`
`protected void`
`closeSelector()`
Close the selector.
`protected void`
`fastCloseSock(SocketChannel sc)`
`void`
`run()`
The main loop for the thread selects() on the connections and dispatches ready I/O work requests, then registers all pending newly accepted connections and updates any interest ops on the queue.
`void`
`wakeupSelector()`

### Methods inherited from class ZooKeeperThread

`handleException`

### Methods inherited from class Thread

`activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield`

### Methods inherited from class Object

`equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

## Field Details

### selector

protected final Selector selector

## Constructor Details

### SelectorThread

public SelectorThread(int id) throws IOException

Throws:
  `IOException`

## Method Details

### addAcceptedConnection

public boolean addAcceptedConnection(SocketChannel accepted)
Place new accepted connection onto a queue for adding. Do this so only the selector thread modifies what keys are registered with the selector.

### addInterestOpsUpdateRequest

public boolean addInterestOpsUpdateRequest(SelectionKey sk)
Place interest op update requests onto a queue so that only the selector thread modifies interest ops, because interest ops reads/sets are potentially blocking operations if other select operations are happening.

### run

public void run()
The main loop for the thread selects() on the connections and dispatches ready I/O work requests, then registers all pending newly accepted connections and updates any interest ops on the queue.

Specified by:
  `run` in interface `Runnable`
Overrides:
  `run` in class `Thread`

### wakeupSelector

public void wakeupSelector()

### closeSelector

protected void closeSelector()
Close the selector. This should be called when the thread is about to exit and no operation is going to be performed on the Selector or SelectionKey

### cleanupSelectionKey

protected void cleanupSelectionKey(SelectionKey key)

### fastCloseSock

protected void fastCloseSock(SocketChannel sc)