Key points:
I. Thoughts
1. separate requests received and executed
A. a thread pool blocks network connection requests.
B. A thread pool executes specific tasks in the task queue through non-blocking
Ii. knowledge points
1. selector is safe with multiple threads, but it is easy to get into a deadlock, such
A. selector. slect ();
B. Channel. Register (selector, selectionkey. op_read );
Both methods request the selector lock, causing thread blocking.
2. The same channel registers multiple times with a selector. Instead of overlapping events, the events are replaced. Therefore, pay attention
3. data read/write and bytebuffer
A. when data is read, The bytebuffer flip () method is used to lock the buffer zone.
B. When writing data, the bytebuffer hasremaining () method checks whether the data has been written.
4. When the input and output are complete, call the following method before closing the channel, that is
A. Channel. socket (). shutdowninput ();
B. Channel. socket (). shutdownoutput ();
C. Channel. Close ();
5. When an event is ready for processing, do not forget to delete the key, that is
A. iterator <selectionkey> it = keys. iterator (); it. Remove () after the key is retrieved ();
B. After processing the selectionkey, if it is no longer needed, you must call key. canel ();
Otherwise, the key will be repeated in the next loop. Of course, you can also re-register the event. The previously interesting OPS will be covered.
Iii. Others
A. There are not many default socket ports on Windows. During High concurrency stress testing, the client often encounters an error in Port binding failure.