Using a selector

A selector is used in an NIO application and allows one thread to handle multiple channels. The selector coordinates multiple channels and their events. It identifies those channels that are ready for processing. If we were to use a thread per channel, then we will find ourselves switching between threads frequently. This switching process can be expensive. Using a single thread to handle multiple channels avoids some of this overhead.

The following figure depicts this architecture. A thread is registered with a selector. The selector will identify the channels and events that are ready for processing.

Using a selector

A selector is supported by two ...

Get Learning Network Programming with Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.