Java NIO Framework Netty Tutorial (14)-netty in Oio model (vs. NiO)

Source: Internet
Author: User

Onecoder this weekend, and the new home does not have a network, the task of this week's translation has not been completed, the next Monday onwards, first on a comparison of Oio and NiO small research.

Netty not only supports the NIO model in Java, but also provides support for the OIO model. (New io vs old io).

First, in Netty, switching between the OIO and NiO two modes is very convenient, only need to initialize different channel works.

  serverbootstrap bootstrap = new serverbootstrap (new  Oioserversocketchannelfactory (executors. Newcachedthreadpool (), executors. (4      
ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(),Executors.newFixedThreadPool(4)));

This is the contribution that the Netty framework makes for us.

Besides, the difference between the two cases. Onecoder according to the information on the Internet and their own understanding, summed up: In the Netty , is through the worker to perform the task. That is, the worker thread pool that we passed in when we constructed the bootstrap . For traditional OIO , a worker corresponds to a channel, from read to operation to back to write, as long as the channel operation is through this worker to complete, for NIO , after the messagerecieved , the worker 's task is complete. So, from this point of view, it is highly recommended that you start the thread immediately after recieve to perform time-consuming logic to release the worker.

Based on this analysis, you may also find that we use fiexedthreadpoolin the code above. Fixed size is 4, theoretically, the number of clients supported byOIO should be 4. And NIO should not be affected by this. Test results such as:

8 Client connections:

Nioserver Thread Condition:

and 8 client requests are handled normally.

For Oio, if you have no control over the worker pool, then supporting 8 clients requires 8 worker,8 threads, which is why traditional oio concurrency is limited.

When the OIO uses Fixedthreadpool:

Can only handle the first four client requests, his is blocked.

Hello action.: 32
Hello action.: 33
Hello action.: 34
Hello action.: 35
Hello action.: 36
Hello action.: 37
Hello action.: 38
Hello action.: 39
Hello action.: 40

However, in the Netty framework, both the OIO and the NIO models do not clog the read-write end. The client returns immediately after writing, regardless of whether the server is received or not, and whether the processing is completed after receiving. In the next chapter, we will look at how the workers in the two modes of OIO and NIO are handled in Netty from a code perspective.

Java NIO Framework Netty Tutorial (14)-netty in Oio model (vs. NiO)

Related Article

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.