Nio learning
The article is a summary of my own study. If there is any misunderstanding, please leave a message.
What is Nio?
The full name of java.nio is java non-blocking IO (actually new io), which refers to the new api (New IO) provided in jdk1.4 and above for all primitive types (except boolean types). cache Supported data container, which can provide non-blocking high scalability network.
What is blocking and what is non-blocking?
- Traditional IO: Traditional IO is blocking. When a server wants to read data from a file system, it needs to set up a thread to read. But at the beginning of reading, it may be that the file system is not ready for the data, but the thread can only wait for the file system to prepare the data before reading. It can't do anything else first. This is the blockage.
- NIO: Nio can solve the blocking problem in traditional IO. It uses Selector (selector, which will be explained later) to monitor whether the data is ready. When the data is ready, the server allocates threads for the read operation. In this way, the server can make good use of the only thread resources.
It's like booking a car on a drop in your life, estimating that the 8:00 driver will arrive, and you'll be waiting at 7:50 at the gate of the community, so you'll have 10 minutes waiting time, which is congestion. Instead, you wait for the 8:00 driver to arrive and call to inform you that you have arrived at the gate of the community, then you go out again. If you save 10 minutes, you can choose to do something meaningful, which is non-blocking.
But please note that there will be no blockage without NIO!!! It's not that NIO won't block up!!! It's not that NIO won't block up!!! Important things are to be repeated for 3 times. In NIO, there are blocking and non-blocking, which will be discussed later.
Three components in NIO that you should know about
There are three important concepts in NIO, (o)... If you don't remember, I'm afraid you can't keep looking down.
- Buffer: Buffer, a container for storing data. It's actually an array.
- channel: channel that represents the connection between IO sources and Applications
- Selector: Selector. If channel is registered in the selector, the selector can listen on the channel. A selector can monitor multiple channels.
Buffer can be understood as a train, Channel can be understood as a railway, and buffer runs in channel. So I can conclude that channel does not store data!!!
Channel reads and writes data from buffer s every time
buffer learning( java.nio.Buffer)
buffer is actually an array. Buffers can be of many types. Basic types in java can be associated with buffers (except boolean).
Suddenly, I felt that boolean was so pitiful that people didn't take it to play with them. The most used one was probably Byte Buffer.
There are three important concepts in Buffer: capacity, limitation and location.
- position: Marks the location of the current operand
- Limit: Represents the size of the data that can be manipulated in the buffer, and the data after limit cannot be read or written.
- Capacity: Marks the current capacity size
For example, I initialize a
ByteBuffer buffer = new ByteBuffer.allocate(5);
(allocate specifies the size of the buffer buffer. ) So the relationship between position,limit,capacith is as follows
It's too difficult to draw a picture. My concubines can't do it!!! At this time I
String str = "123";
buffer.put(str.getBytes());
Then position will move to the third grid, limit,capacity will remain unchanged.
But if you switch buffer to read mode
buffer.flip()
So the current position: 3, limit: 3, capacity: 5
@Test public void test() { String str = "123"; //Appoint buffer Size of capacity ByteBuffer buffer = ByteBuffer.allocate(1024); //attribute System.out.println("-----------------------"); //Location of data in current operation System.out.println(buffer.position()); //Limits, which indicate the size of data that can be manipulated in a buffer. limit Later data cannot be read or written System.out.println(buffer.limit()); //Maximum storage capacity in buffer System.out.println(buffer.capacity()); buffer.put(str.getBytes()); System.out.println("---------put--------------"); System.out.println(buffer.position()); System.out.println(buffer.limit()); System.out.println(buffer.capacity()); //Switch to Read Mode buffer.flip(); System.out.println("---------flip--------------"); System.out.println(buffer.position()); System.out.println(buffer.limit()); System.out.println(buffer.capacity()); byteArrs = new byte[buffer.limit()]; ByteBuffer byteBuffer = buffer.get(byteArrs); System.out.println(byteBuffer.toString()); // rewind,Switch to Read Mode and Read Again buffer.rewind(); System.out.println("===========rewind============"); System.out.println(buffer.toString()); //Clear the buffer buffer.clear(); }
Channel(java.nio.channels.Channel)
The main implementation classes in channel: FileChannel, Socket Channel, Server Socket Channel.
Method for obtaining Channel:
Prior to JDK 1.7: Access to channel via IO stream
JDK1.7 before/** * Using Channel to Complete File Replication * @throws IOException */ @Test public void test() { long start = System.currentTimeMillis(); FileInputStream fis = null; FileOutputStream fos = null; FileChannel inChannel = null; FileChannel outChannel = null; try { //jdk1.7 before NIO Writing of access channel fis = new FileInputStream("./resource/Java NIO.pdf"); fos = new FileOutputStream("./resource/demoCopyTest.jpeg"); //1.Access channel inChannel = fis.getChannel(); outChannel = fos.getChannel(); //2.Creating Buffers,And assign size ByteBuffer byteBuffer = ByteBuffer.allocate(1024); //3.Write data into the buffer while (inChannel.read(byteBuffer) != -1) { //4.Switching Read Data Mode byteBuffer.flip(); outChannel.write(byteBuffer); byteBuffer.clear(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (outChannel != null) { outChannel.close(); } if (inChannel != null) { inChannel.close(); } if (fos != null) { fos.close(); } if (fis != null) { fis.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } long end = System.currentTimeMillis(); System.out.println("Time-consuming indirect caching" + (end - start)); }