A ramble on the basics of Java IO

Source: Internet
Author: User

Java's network programming is almost impossible if you are not specifically engaged in server performance development or message distribution. But it is an interview to find a job must ask a knowledge point, covering the knowledge system is also very broad, from the Java underlying IO principle to the operating system core composition, and then to the network TCP, UDP, HTTP application practice .... Therefore, even in the workplace for many years of the old fritters, still need to review, not to mention my only seven seconds of memory of the small rookie.

The evolution of Java network IO, From the very beginning JDK1.4 was based on blocking IO; the development to 1.4 post-release NIO provides a selector multiplexing mechanism, as well as channel and buffer, to 1.7 of NIO upgrades to provide true asynchronous APIs, and then to the later budding Mina and Netty. Therefore, the whole network IO programming learning and finishing, will follow the following steps to do:

    1. Basic knowledge and concepts of network IO
    2. General IO and Bio server
    3. Use of NiO with server Hello World
    4. Netty Getting Started with server Hello World
    5. Netty in Layman's

The basic knowledge and concepts of network IO need to be understood today.

synchronous, asynchronous, and blocking, non-blocking

Often hear people mention, synchronous blocking server or asynchronous non-blocking server, there are many articles on the Internet to explain the concept, everyone understands the seemingly different. The easiest to confuse asynchronous and non-blocking .... I would simply say that my own understanding:

Synchronous synchronous, asynchronous asynchronous, their difference is that after the launch of the task, a state of itself-if it is waiting for the result, it is synchronization, if you return immediately, and other ways to get the result is asynchronous (such as state, notification, callback).

As an example:

    1. In the past, when technology was underdeveloped, banks were queuing up to withdraw money. If you want to get money, you have to go in line, until your turn, and that is synchronization;
    2. Now go to the bank generally direct call, and then go to rest location to play games, log on to their own time, there will be notice, this is asynchronous;

Blocking blocking, non-blocking non-blocking, focuses on the state of the CPU as it waits for the result. As in the previous example, the process of queuing is not doing anything is blocking, while standing in line, playing King Glory is non-blocking.

User space and interior and space

This concept involves the operating system, in order to protect the security of the operating system, the memory will be divided into user space and kernel space two pieces. If the user wants to manipulate the data in the kernel space, they need to copy the data from the inside and the space to the user control.

As an example:

The server receives a request from the client and wants to process it, roughly following several steps:

    1. The server's network driver receives the message, goes to the kernel to request space, and waits for the complete packet to arrive (it is possible to send the packet, not finished ...). ), copied to the kernel space;
    2. Data copied from kernel space to user space
    3. User program for processing

Therefore, it is possible to understand the receiving message as two stages: 1. Wait for the data to reach 2. Copy to User space

By understanding this process, you can see why the Classic 5 network models are present.

Five major network models

These network models are also the time of the students have seen, but the understanding is not thorough, and do not know what the difference. Recently on the Internet also read a lot of articles, found that some articles cited small examples are good, can be very simple to understand the meaning of these models. So my side also draws on:

Dalian High-tech Wanda behind a snack street called the Golden Street, there is an open-air roadside stall called "small red Flag", mainly to do fried tofu and cold noodles, and then use cold noodles to roll up the stinky tofu, brush the smelly sauce, very delicious. Each passing can see a lot of people in line, the team has a kind of want to let people resign to join the feeling .... Basically, it's half an hour to row a team-one hours.

This process of queuing is obviously the above-mentioned synchronous blocking mode .... Then I would like to imagine, if the small red flag business is bigger, how can development? Just apply the concept of the network model ...

1. Synchronous Blocking IO

This is not detailed said, the process of the queue can not go, if you have not brought a cell phone, the process of queuing can only despair. This is the obvious synchronization + blocking mode.

2. Synchronous non-blocking IO

If the boss of the small red flag made a ordering machine, to order the customer to eat their own row, and then wait for the boss to do, they can in this one hours of time to go around the mall to stroll down. But because there is no way of communication, can only keep back to ask the boss, do not.

Come back to ask the time is the customer's own control, if the time is very short, then you can as early as possible to know that stinky tofu fried good, but also will affect the shopping experience, if the time is long, it is possible that stinky tofu has been done already. The result was long, but it was not good to eat.

Therefore, the non-blocking IO is based on the state rotation, although it allows the program to do something else in the process of waiting, but the frequent switching of the program, it will cause great pressure.

3. IO multiplexing/Event-driven

The boss of the small red flag upgraded the system, abandoned the use of a la carte machine, instead of McDonald's kind of food large screen. The same is ordered meal, but a large screen inside shows a lot of people's stinky tofu progress, that is, saving resources, but also to avoid constant inquiries.

In fact, NiO is alive Netty is based on this mode, a thread can listen to a lot of IO operations, so in the IO wait on more efficient. The implementation is dependent on the operating system, and Windows and Linux have different implementations. The initial select or poll has a concurrency limit, and the NIO select has a problem with the empty rotation, and epool breaks the limit of the number of connections, and one thread can listen to a lot of IO operations. This interested friend can get a deep understanding of the principles of Select, poll, and Epool.

4. Signal-driven IO

The small red flag boss is fashionable again, has made an upgrade version of the delicious not to wait. Customer based on the small program à la carte, the dishes are done automatically remind customers to take the meal .... The process of reminding is like launching a special signal.

However, the UNIX network programming inside the signal-driven, can not be so simple, the signal is dependent on the operating system underlying, capture signal or processing is very troublesome, so now the application is not very extensive.

5. Asynchronous non-blocking IO

A couple of little lovers Li Lei and Han Meimei, Han Meimei taste very heavy, especially like to eat stinky tofu, but Li Lei completely not interested, smell the taste to vomit. Li Lei then agreed with Han Meimei, let Han Meimei himself to eat, Li Lei ran to the café next to coffee. Han Meimei himself to the line to buy stinky tofu, buy finished by the way, and then come back to find Li Lei ....

This process is asynchronous non-blocking, the message waiting and processing are done on the server side, the user as long as the last receive the message processing the notification on the line.

Summarize

In summary, these network models:

    1. Synchronous blocking: The emphasis is on what I'm going to do! --Don't say anything else, just do it!
    2. Synchronous non-blocking: The emphasis is on what I want to do! -In the process of thinking, do something else better.
    3. Asynchronous non-blocking: the emphasis is that I'm done! -When the results are notified, the work is done.

Details of which need to be slowly realized ... The following article will pick a few models to do the code demonstration, more content also please continue to pay attention to.

Reference
    1. Concurrent Programming Network
    2. Netty Source Code Analysis
    3. Netty Source Code Analysis
    4. Netty Source Code Analysis
    5. IO multiplexing Select, poll, Epoll
    6. Talk about Linux five IO models
    7. Talk about synchronous, asynchronous, blocking, non-blocking
    8. Netty Official documents
    9. An article that reads Netty's high-performance architecture
    10. Code sample Sharing

A ramble on the basics of Java IO

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.