conception

【NIO】解读 java.nio.channels.Selector

喜欢而已 提交于 2020-08-04 11:10:15
目录 Part 1. What's Selector? Part 2. Why Selector? Part 3. How to use Selector? 1) Create Selector 2) Channel's Register 3) Selection Key 4) Selector.select() 5) Selector.selectedKeys() Part 4. Demo Train Part 1. What's Selector? Selector is the core conception for NIO from JDK1.4 which is a multiplexor of SelectableChannel objects that allows one thread to handle multiple channels. Part 2. Why Selector? Before NIO came out, We used Socket programming like this: //---Server--- //Create ServerSocket and bind the port ServerSocket serverSocket = ……; serverSocket.bind(8899); While (true) { //