Java Network IO Model

Source: Internet
Author: User
Tags epoll new set

Read a simple example of Java network programming before entering the topic: The code is simple, the client communicates with the server, and the server replies get for each input to the client. Note that the server can allow multiple clients to connect at the same time.

Service end-of-line code:

Create service-side socketserversocket ServerSocket = new ServerSocket (20000); client = Serversocket.accept ();//clients connected successfully, The output prompts System.out.println ("Client connection succeeded");//starts a new thread processing client request new Thread (new Serverthread (client)). Start ();// The input class Serverthread implements Runnable {    ...    ) that handles the client in a child thread. @Override public    Void Run () {        Boolean flag = true;        while (flag) {            //Read the data sent by the client            String str = buf.readline ();            Reply to client get indicates received data            out.println ("Get");}}    

Client code:

Socket client = new socket ("127.0.0.1", 20000); Boolean flag = True;while (flag) {    //reads the input from the keyboard from the user    String str = input . ReadLine ();    Send the user input to the server    out.println (str);    Get string    echo = Buf.readline ()    received to the service-side callback System.out.println (Echo);}    }

Considering that the full Java sample code is too large to affect reading, so here is not a full post, if need to download directly on GitHub, here is.

As you can see, server needs to open a thread for each client in order to be able to handle multiple client requests at the same time, and this one-thread-per-client mode is very stressful for the server. Assuming that there are 1k clients, the corresponding server should start 1k thread, then the memory consumed by the server, and the time taken during thread switching, etc. are fatal. Even with the thread pool technology to limit the number of threads, this Blocking-io model has no way of supporting a large number of connections.

Each client needs a thread to request processing.

Nio

The main reason this one-thread-per-client mode cannot support a large number of connections is that it readLine blocks IO, that is, readLine when the data is not able to be read, it blocks the thread, so that the thread cannot continue executing, so the server can handle multiple Client, you can only open multiple threads at the same time.

So, after Java 1.4, a set of NIO interfaces was introduced. One of the main functions of NIO is the ability to do non-blocking IO operations: If the data is not readable, the non-blocking IO does not block the thread, but returns 0 directly. In this case, the thread can handle other things without being blocked by judging that the data is not ready by the return value.

Is the difference between blocking IO and non-blocking IO, it can be seen that while nonblocking Io is not blocked, it is still constantly calling functions to check whether the data is readable, which is shown in this form in the code:

while ((str = read ()) = = 0) {}//Continue reading the logic after the data.

It can be understood that although non-blocking IO does not block threads, there is no way for a thread to continue executing the following logic because there is no data to read, and to wait until the data arrives. This scenario is called synchronous IO. So in summary, NIO is essentially a non-blocking synchronous IO.

IO multiplexing

Since NIO is not blocked because the data has not yet arrived, it is not necessary for each client to assign a thread to continually poll to determine if there is data to read. You can use a thread to listen to all client connections, the thread loop to determine if there is a client's data readable, if any, to tell other thread a client connection is readable by the data. This behavior is referred to as IO multiplexing. Classes are provided in NIO Selector to listen for data readable by all client connections.

Using it for Selector IO multiplexing, only one thread needs to be concerned about whether the data is coming and other threads waiting for the notification to be good. As a result, only the listener thread will always be looping and will not occupy too much CPU resources. When it comes to NiO Selector , I have to talk about IO multiplexing in Linux programming, because the bottom of NiO Selector is using a system-level IO multiplexing scheme.

There are 2 implementations of IO multiplexing for Linux systems:

    • select
    • epoll

In the version of the Linux 2.6+, NIO is using a epoll function in the 2.4.x version select . epollthe function is much better in terms of performance select , and here you don't care about the specifics of Linux programming. It is worth mentioning that the bottom of Java's Netty network framework is the use of NIO technology.

Aio

Review the NIO: Use the listener thread to invoke the select function to listen to all requests for data arrival and, if there is data, to notify other threads to read the data. During the process of reading the data, the thread is in a blocking state until the data has been read, and the thread can continue to execute the logic only after the data has been read. As I said before, this is called synchronous IO. A new set of interface AIO (asynchronous IO) has been added to JDK 7.

AIO has a magical feature: When the IO operation is initiated, the thread does not have to wait for the IO to be read, but can return directly and proceed with other operations. When the data is read, the system notifies the thread that the data has been read. This initiates an IO operation, but does not have to wait for the IO operation to be read by the data called asynchronous IO. If AIO is used, a thread can initiate multiple IO operations at the same time, which means that one thread can process multiple requests at the same time. The famous Web server Nginx uses asynchronous IO. For more details, refer to my other article <apache--mpms && Nginx event driver >.

End

So far, the article explains the difference between blocking/nonblocking IO, synchronous/asynchronous Io, and the IO model, which inevitably involves 5 types of IO models of Linux

    • Blocking IO
    • Non-blocking IO
    • IO multiplexing
    • Signal-driven IO
    • Asynchronous IO

Except that the signal-driven IO is not mentioned, the other 4 major IO models are explained, and understanding the concepts of these IO models is a great help in writing code.

Java Learning Exchange QQ Group: 589809992 prohibit small talk, non-happy do not enter!

Java Network IO Model

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.