Java non-blocking (NIO) programming high concurrency network programs

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.