Given an application that accepts input on multiple sockets, what are the pros/cons of dedicating a thread to each socket vs. using selectors to process input in a single thread? Assume the application can be written either way.
Well imagine that your program is watching for IO from several hundred Channels (this is likely in the case of writing a program to receive data or notification from sensors place in a large factory for example). Thus using a multithread model; you would have a one thread----->one channel design. This doesn't scale. This article suggests using Selectors for a single thread design by employing the Reactor design pattern [ April 15, 2003: Message edited by: Pho Tek ] [ April 15, 2003: Message edited by: Pho Tek ]
posted 15 years ago
Very good article... thanks! Surprised it took so long to get non-blocking I/O into Java. I used the select() call in C many years ago...
Non-blocking and selectors are the primary justification for NIO. Without them, building industrial-strength, enterprise-class applications is tough. Considering that server-side is where Java has really taken root, it is indeed surprising that it didn't happen sooner.