Java I/O working mechanism and optimization __java

Source: Internet
Author: User
I . Java I/O operation class grouping
1. I/O interface based on byte operation: InputStream and OutputStream
2. I/O interface based on character manipulation: Writer and Reader
3. I/O interface for disk-based operation: File
4. I/O interface based on network operation: Socket
The core problem with I/O is either that the data format affects I/O operations, or that the transfer mode affects I/O operations, which is where the data is written.

Two, Java IO working mechanism (1) disk I/O working mechanism
The usual file in Java does not represent a real object, and when you specify a path descriptor, it returns a virtual object that is associated with the path, either a real file or a directory containing multiple files. When you really want to check that a file is not saved. When you really want to read this file, if the file does not exist, it throws a filenotfoundexception.

When you pass in a file path, you will create a file object to identify it based on this path, and then you will create an action object that actually reads the file based on this file object, and you will actually create a file descriptor FileDescriptor that associates the real-world disk file. This object allows you to control the disk file directly. If we need to read a character format, we also need the Streamdecoder class to decode Byte to char (the decoding process) as to how to read a piece of data from a disk drive, which the operating system can do for us.

(2), Java Socket's working mechanism host A's application must be able to communicate with host B's application, and the connection has to be established through the socket, while establishing the socket connection requires the underlying TCP/IP protocol to establish the TCP connection. Establishing a TCP connection requires the underlying IP protocol to address the host in the network. We know that the IP protocol used by the network layer can help us locate the target host based on the IP address, but there may be multiple applications running on one host, and how to communicate with the specified application must be specified by TCP or UPD address, which is the port number. This allows a single Socket instance to represent the communication link of an application on one host.
A, establish communication links
When the client wants to communicate with the server, the client first creates a socket instance, the operating system assigns an unused local port number to the socket instance, and creates a socket data structure that contains both local and remote addresses and port numbers. This data structure will remain in the system until the connection is closed. Before the constructor that creates the Socket instance returns correctly, a three handshake protocol will be performed for TCP, and the socket instance object will be created when the TCP handshake is complete, otherwise a IOException error will be thrown.

The corresponding server will create a ServerSocket instance, ServerSocket creation is simpler as long as the specified port number is not occupied, the common instance creation succeeds and the operating system creates an underlying data structure for the ServerSocket instance. This data structure contains the port number that specifies the listener and the wildcard character that contains the listener address, which is usually "*" that listens to all addresses. Later, when the Accept () method is invoked, the blocking state is entered, waiting for the client's request. When a new request arrives, a new socket data structure is created for the connection, which contains the address and port information that is the source address and port of the request. This newly created data structure will be associated with a list of incomplete connection data structures for the ServerSocket instance, noting that the socket instance that corresponds to the server does not complete the creation, and that the socket instance on this server will not return until the three handshake with the client is completed. and move the data structure of this Socket instance to the completed list from the finished list. Therefore, each data structure in the associated list of ServerSocket represents a TCP connection with a client's establishment.

b, transmission data transmission is the main purpose of our connection, how to transfer data through the Socket.

When the connection has been established successfully, both the server and the client will have a socket instance, and each socket instance has a InputStream and OutputStream, which is the exchange of data between the two objects. At the same time, we also know that the network I/O is transmitted as a byte stream. When the Socket object is created, the operating system allocates a certain size buffer for both InputStream and OutputStream, and the data is written and read through this buffer. The write end writes the data to the outputstream corresponding SENDQ queue, and when the queue fills up, the data is sent to the RECVQ queue on the other end InputStream, and if Recvq is full, the OutputStream write method will Block until the RECVQ queue has enough space to hold the data sent by SENDQ. It is worth noting that the size of this buffer, as well as the speed of the write end and the speed of the reading side, greatly affect the data transfer efficiency of this connection, because of the possibility of blocking, network I/O and disk I/O have a coordinated process of writing and reading data, and deadlocks can occur if both sides of the data are transferred simultaneously.

Third, Java I/O tuning

1. Disk I/O optimization

If we can stress test the application to see whether the system I/O wait indicator is normal, for example, the test machine has 4 CPUs, then the ideal I/O wait parameter should not exceed 25%, if more than 25%, I/O is likely to become the application performance bottleneck. The Linux operating system can be viewed through the iostat command.

2, network I/O optimize network I/O optimization there are usually some basic principles of processing:

1. One is to reduce the number of network interactions: to reduce the number of network interactions Typically, we set caching at both ends of the network interaction, such as Oracle's JDBC driver, to provide a cache of SQL results for queries, both on the client and the database side, to effectively reduce access to the database. Memory management for Oracle JDBC can be referenced in Oracle JDBC memory management. In addition to setting up the cache, there is also a way to merge access requests: For example, when querying a database, we need to look up 10 IDs, I can check one ID at a time, and I can check 10 IDs at a time. For example, when accessing a page through a number of JS or CSS files, we can merge a number of JS files in an HTTP link, each file separated by commas, and then sent to the back-end Web server according to the URL link, then split the various files, and then packaged and sent back to the front-end browser. These are common ways to reduce network I/O.

2. Reduce the amount of data transmitted by the network: the way to reduce the amount of network data is usually to compress the data and then transfer it, such as in an HTTP request, the Web server typically compresses the requested Web page gzip and transmits it to the browser. It is also through the design of simple protocols, as far as possible by reading the protocol headers to obtain useful value information. Try to avoid reading the entire communication data to get the information you need.

3. Minimize coding: Typically data transfers in network I/O are in byte form, which is usually serialized. But we send the data to be transmitted in the form of characters, which must be encoded from character to byte. But this coding process is time-consuming, so when you want to pass network I/O transmission, try to send directly as Byte. That is to try to convert characters into bytes in advance, or to reduce the conversion process of characters to bytes.

4. According to the application scenario design appropriate interaction mode: the so-called interactive scene mainly includes synchronous and asynchronous blocking and non-blocking mode.








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.