1. basic Concepts
IO is the process of copying data from main memory and external devices (hard disks, terminals, and networks). IO is the underlying functional implementation of the operating system, which is done through I/O directives.
All language runtime systems provide tools that perform high levels of I/O. (c's printf Scanf,java object-oriented encapsulation)
2. Java standard io review
The Java standard IO class library is an object-oriented abstraction of IO. Based on the underlying implementation of the local method, we do not need to focus on the underlying implementation. Inputstream\outputstream (Byte stream): one bytes of transmission at a time. Reader\writer (character stream): one character at a time.
3. introduction to NiO
NIO is the abbreviation for Java New IO, the new API provided in jdk1.4. The features of Sun's official branding are as follows:
– Provides (buffer) cache support for all primitive types.
– Character set encoding and decoding solution.
–channel: A new primitive I/O abstraction.
– A file access interface that supports lock and memory-mapped files.
– Provides multi-channel (non-bloking) non-blocking, high scalability network I/O.
This article will focus on these characteristics to learn and introduce.
4. Buffer&chanel
Channel and buffer are NIO is the two most basic data type abstraction.
Buffer:
– is a contiguous block of memory.
– Is the transfer of NIO data to read or write.
Channel:
– The source of the data or the destination of the data
– The only interface for buffer objects that provide data to buffer or read buffer data.
– Asynchronous I/O support
Figure 1:channel and buffer relationships
Example 1:copyfile.java:
Java code
Package sample;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.nio.ByteBuffer;
Import Java.nio.channels.FileChannel;
public class CopyFile {
public static void Main (string[] args) throws Exception {
String infile = "C:\\copy.sql";
String outfile = "C:\\copy.txt";
Get input and output streams for source and destination files
FileInputStream fin = new FileInputStream (infile);
FileOutputStream fout = new FileOutputStream (outfile);
Get input and output channels
FileChannel fcin = Fin.getchannel ();
FileChannel fcout = Fout.getchannel ();
Creating buffers
Bytebuffer buffer = bytebuffer.allocate (1024);
while (true) {
The Clear method resets the buffer so that it can accept the read-in data
Buffer.clear ();
Reading data from input channels to buffers
int r = fcin.read (buffer);
The Read method returns the number of bytes read, possibly zero, and returns 1 if the channel has reached the end of the stream.
if (r = =-1) {
Break
}
The flip method allows the buffer to write newly read-in data to another channel
Buffer.flip ();
Write data to buffer from output channel
Fcout.write (buffer);
}
}
}
Where buffer internal structure is as follows (copy from data):
Figure 2:buffer Internal structure
A buffer mainly consists of position,limit,capacity three variables to control the read and write process. The meanings of these three variables are shown in the following table:
|
Parameters |
Write mode |
Read mode |
|
Position |
The number of unit data currently written. |
The unit data location that is currently being read. |
|
Limit |
Represents the maximum number of units of data and capacity that can be written. |
Represents the maximum number of units of data that can be read, consistent with the amount of data previously written. |
|
Capacity |
Buffer capacity |
Buffer capacity |
Common Methods for Buffer:
Flip (): Write mode into read mode
Rewind (): Resets the position to 0, which is typically used for repeated reads.
Clear (): Empty buffer, ready to be written again (position becomes 0, limit becomes capacity).
Compact (): Copies the unread data to the head bit of the buffer.
Mark (), Reset (): Mark can mark a location, reset can be reset to that location.
Buffer Common types: Bytebuffer, Mappedbytebuffer, Charbuffer, DoubleBuffer, Floatbuffer, Intbuffer, Longbuffer, ShortBuffer 。
Channel Common types: FileChannel, Datagramchannel (UDP), Socketchannel (TCP), Serversocketchannel (TCP)
A simple performance test is done on this machine. My notebook performance is general. (The specific code can be found in the attachment.) See Nio.sample.filecopy package below for example) here is the reference data:
– Scene 1:copy a 370M file
– Scenario 2: Three threads are copied at the same time, each thread copies a 370M file
|
Scene |
fileinputstream+
FileOutputStream |
fileinputstream+
bufferedinputstream+
FileOutputStream |
bytebuffer+
FileChannel |
Mappedbytebuffer
+filechannel |
|
Scene time (milliseconds) |
25155 |
17500 |
19000 |
16500 |
|
Scene two time (milliseconds) |
69000 |
67031 |
74031 |
71016 |
- 5. Nio.charset
Character encoding decoding: The bytecode itself is just some number that is correctly parsed in the correct context. It is necessary to consider the encoding of the character set when storing the data in Bytebuffer, and the decoding of the character set is involved when reading the Bytebuffer data.
Java.nio.charset provides a solution for encoding and decoding a set of solutions.
As an example of our most common HTTP request, the request must be correctly encoded at the time of the request. The response must be correctly decoded when it gets a response.
The following code sends a request to Baidu and gets the results displayed. The example shows the use of CharSet.
Example 2baidureader.java
Java code
- Package nio.readpage;
- Import Java.nio.ByteBuffer;
- Import Java.nio.channels.SocketChannel;
- Import Java.nio.charset.Charset;
- Import java.net.InetSocketAddress;
- Import java.io.IOException;
- public class Baidureader {
- Private Charset Charset = Charset.forname ("GBK");//create GBK Character Set
- Private Socketchannel channel;
- public void Readhtmlcontent () {
- try {
- Inetsocketaddress socketaddress = new Inetsocketaddress (
- "Www.baidu.com", 80);
- Step1: Open Connection
- Channel = Socketchannel.open (socketaddress);
- Step2: Sending a request using GBK encoding
- Channel.write (Charset.encode ("GET" + "/http/1.1" + "\r\n\r\n"));
- Step3: Reading data
- Bytebuffer buffer = bytebuffer.allocate (1024);//create 1024-byte buffer
- while (channel.read (buffer)! =-1) {
- Buffer.flip ();//The Flip method is called before the read buffer byte operation.
- SYSTEM.OUT.PRINTLN (Charset.decode (buffer));
- Convert bytes to strings using the Charset.decode method
- Buffer.clear ();//emptying buffer
- }
- } catch (IOException e) {
- System.err.println (E.tostring ());
- } finally {
- if (channel! = NULL) {
- try {
- Channel.close ();
- } catch (IOException e) {
- }
- }
- }
- }
- public static void Main (string[] args) {
- New Baidureader (). Readhtmlcontent ();
- }
- }
- 6. non-blocking IO
Understanding how nonblocking IO will be blocked, what is non-blocking, non-blocking, and asynchronous core APIs.
What is blocking?
A common network IO communication flow is as follows:
Figure 3: Basic process of network communication
From this network communication process to understand what is blocking:
In the above process if the connection has not yet arrived, then the accept will block, the program runs here to suspend, the CPU to execute other threads.
If the data is not ready in the process above, read will be blocked as well.
Blocking Network IO Features: multi-threaded processing of multiple connections. Each thread has its own stack space and consumes some CPU time. Each thread will block when it encounters external readiness. The result of blocking is a lot of process context switching. And most of the process context switches may be meaningless. For example, if a thread listens on a port, only a few requests come in a day, but the CPU has to do a context-switching attempt for that thread, and most of the switching ends up blocking.
What is non-blocking?
Here's a metaphor:
A bus from a to B, there are many points on the road may be people get off the train. The driver does not know which points will have who will get off the car, for need to get off the person, how to handle better?
1. The driver periodically asks if each passenger arrives at the destination, and if someone says so, the driver stops and the passenger gets off. (similar to block type)
2. Each person tells the conductor his or her destination, then sleeps, the driver only interacts with the conductor, and at some point the conductor notifies the passenger to alight. (similar to non-blocking)
It is clear that each person arriving at a destination can be considered a thread and the driver can be considered a CPU. Inside the block, each thread needs constant polling, context switching, to achieve the result of finding the destination. In a non-blocking way, each passenger (thread) sleeps (sleeps) and wakes up only when the real external environment is ready, so that the wake is definitely not blocked.
principle of non-blocking
The whole process is replaced by small tasks, which are accomplished through collaboration between tasks.
All IO events are handled by a dedicated thread and are responsible for distribution.
Event-driven: events are triggered when they arrive, rather than synchronized to monitor events.
Thread communication: The threads communicate through wait,notify and other means. Ensure that each context switch is meaningful. Reduce the unnecessary process switching.
The following is the structure of asynchronous IO:
Figure 4: Non-blocking fundamentals
Reactor is the role of the conductor of the metaphor above. The process of each thread is probably to read data, decode, calculate processing, encode, send the response.
Asynchronous IO Core API
Selector
The core class of asynchronous IO, which detects events on one or more channels (channel) and distributes the events.
Using a Select thread, you can listen for events on multiple channels and trigger corresponding responses based on the event driver. Instead of assigning a thread to each channel.
Selectionkey
Contains the state information of the event and the binding of the channel corresponding to the time.
Example 1 single thread implementation listens on two ports. (See the Nio.asyn package below for an example.) )
Example 2 NIO thread collaboration for rational use of resources. (wait,notify). (see example under Nio.asyn.multithread)
Introduction to JAVA NIO