Java NIO1: Overview

Source: Internet
Author: User

Objective

More complex and less understandable than I/o,nio, there are some concepts that need to be explained before starting NIO, and if you have a good understanding of these concepts, it is definitely good for learning NiO.

Synchronous vs. asynchronous

The so-called synchronization refers to the completion of a task to rely on another task, only wait for the task to be relied upon to complete, the dependent task can be completed, this is a reliable task sequence. To succeed all succeed, failure fails, and the state of the two tasks can be kept consistent.

Asynchronous does not need to wait for the task to be relied upon to complete, only to notify the dependent task to complete what work, the dependent task is immediately executed, as long as the completion of the entire task, even if completed. As to whether the dependent task is ultimately completed, the task that relies on it cannot be determined, so it is an unreliable task sequence.

The choice of how synchronous or asynchronous processing is often encountered when I/O processing is involved, because synchronous and asynchronous I/O processing can have a significant impact on the caller, and this problem is encountered in the database product because I/O operations are often a very time consuming operation, i/in a task sequence O is often a performance bottleneck. However, the synchronous and asynchronous processing methods have great influence on the reliability of the program, the synchronization can guarantee the program's reliability, while the asynchronous may improve the performance of the program, it must maintain a balance between reliability and performance, but there is no perfect solution.

Blocking and non-blocking

Blocking and non-blocking are said to be from CPU consumption.

Blocking is when the CPU stops to wait for a slow operation to complete, and the CPU does the rest of the work, and during this slow operation the CPU needs to constantly poll the operation for completion.

Non-blocking is when the slow operation is performed, the CPU does other work, and so the slow operation is completed, the CPU then completes the subsequent operation.

Although non-blocking on the surface can significantly improve the CPU utilization, but also brings another consequence, is the system of the increase in thread switching, the increase in CPU use time can compensate for the system's switching costs need to be well evaluated.

The CPU is no longer tied

Java programmers devote all their energies to optimizing processing efficiency, while the lack of attention to I/O is not their fault in some way. In the early days of Java, the JVM tended to have little or no runtime optimizations when interpreting bytecode, which meant that Java programs tended to be very long and inefficient to run much less efficiently than local compiled code, and therefore not very demanding for the OS I/O subsystem.

Now that the JVM has taken a big step forward in the run-time optimization, the JVM is now running at a rate close to the local compiled code, and even beyond the performance of dynamic runtime optimizations. This means that most Java applications are no longer tied to the CPU (spend a lot of time executing code), and more often I am bound by I/O (Waiting for data transfer).

However, in most cases, Java applications are not really constrained by I/O. The operating system is not unable to transfer data quickly, so Java has something to do, but the JVM itself is inefficient in I/O. The operating system does not match the Java stream-based I/O pattern. The operating system is moving large chunks of data (buffers), which are often done with the assistance of hardware Direct memory access (DMA). The JVM's I/O class likes to manipulate small pieces of data----single bytes, a few lines of text. As a result, the operating system sends the entire buffer of data, java.io the flow class to spend a lot of time to split them into small pieces, often copy a small piece will be to and from several layers of objects.

with NIO, you can easily transfer large amounts of data to places that need to be used directly . However, this is not to say that a large amount of data cannot be moved using the traditional I/O model----Of course, the Randomaccesssfile class is available and efficient, as long as you stick to the array-based read (), write () method. These methods are fairly close to the underlying operating system invocation, although at least one copy of the buffer must be kept.

The Making of NiO

JDK1.4 's NIO package introduces a new set of abstractions for I/O processing. Unlike in the past, the new abstraction focuses on how to shorten the distance between abstraction and reality. The NIO abstraction has a very real direct interaction with the entities that exist in reality. Understanding these new abstractions, and the I/O services that interact with them, is the key to maximizing the dense I/O requirements of Java applications.

In the study of NIO, it is important to understand the following concepts:

    • Buffer operation
    • Kernel Space and user space
    • Virtual memory
    • Paging Technology
    • File-oriented I/O vs. stream I/O
    • Multi-work I/O

Buffer operation

buffers, and how buffers work, are the basis for all I/O. The so-called "input/output" is all about moving data into or out of the buffer.

The I/O operation boils down to making a request to the operating system to either read the data in the buffer or fill the buffer with data. The process uses this mechanism to handle all data entry and exit operations. The mechanism within the operating system to handle this task may be beyond imagination, but in terms of concept it is very straightforward.

Describes the process of moving data from an external disk to the memory area of a running process:

The process uses the read () system call to request that its buffer be filled. The kernel randomly issues a command to the disk control hardware that requires it to read data from disk. The disk controller writes data to the kernel memory buffer, which is done through DMA, without the need for primary CPU assistance. Once the disk controller fills the buffer, the kernel copies the data from the temporary buffer in the kernel space to the buffer specified by the process read () call.

Note The concept of user space and kernel space. The user space is the area where the regular process resides, and the JVM is the regular process that is stationed in the user space. User space is a non-privileged zone: for example, code executed in that region cannot directly access hardware devices. Kernel space is the region in which the operating system resides. The kernel code has special rights: it can communicate with the device controller, and control the running state of the user zone process. Most importantly, all I/O is directly or indirectly through kernel space.

When a process requests an I/O operation, it executes a system call that transfers control to the kernel. The low-level functions that C + + programmers are familiar with open (), read (), write (), and close () do nothing more than establish and execute appropriate system calls. Within the party and in this way, it takes any necessary steps to find the data required for the process and transmits the data to the specified buffer within the user's space. The kernel tries to cache or pre-read the data, so the data required for the process may already be in the kernel space. If so, the data needs to be copied simply. If the data is not in kernel space, the process is suspended and the kernel begins to read the data into memory.

One might think, why not just let the disk controller transfer data to the buffer of user space? There are several problems with this:

1. Hardware usually cannot access user space directly

2. Block storage-based hardware devices such as disks operate on fixed-sized blocks of data, and user processes may request data blocks of any size or not. The kernel is responsible for the decomposition and re-grouping of data in the process of data exchange between user space and storage device.

Virtual memory

Virtual memory is used by all modern operating systems. Virtual memory means replacing physical (hardware RAM) memory addresses with false (or virtual) addresses. This is a lot of good, summed up can be divided into:

1, more than one virtual address can point to the same physical memory address

2, virtual memory space can be greater than the actual available hardware memory

As mentioned earlier, the control device cannot be directly stored in the user space through DMA, but the same effect can be achieved by taking advantage of the first item mentioned above. Mapping the kernel space address to the virtual address of the user space to the same physical address, so that the DMA hardware (which only accesses the physical memory address) can populate the buffer that is visible to the kernel and the user space process:

This is really good, eliminating the need for a copy of the kernel space and user space, but only if the kernel and user buffers must be aligned with the same page, the size of the buffer must also be a multiple of the disk controller block size.

Java NIO1: Overview

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.