The first thing to know is what the so-called Java NIO is!

Source: Internet
Author: User

The first thing to know is what the so-called Java NIO is!
IO is transmitted by character or byte, slower! And NiO is a block, it's equivalent to a buffer, a piece of
Transmission, faster
! Multiple threads are also added
Control, a NIO stream can transmit multiple blocks at the same time, which is called asynchronous transmission

Traditional
The Concurrency type
Server design is the use of blocking network I/O
In multithreaded mode (a socket link, the server starts a thread to accept the service
) to be implemented, however, by the
In the system often in the network reading and writing in the blocking state, will greatly affect the performance of the system
; self-Java1. 4 Start of introduction
The NIO (new I/O) API by using non-blocking
/ o
, enabling smooth network read and write operations, for developing high-performance concurrency
Server program provides a good solution. This is the Java NiO
First of all, the traditional blocking network I/O for the poor Java platform traditional I/O systems are based on byte (bytes) and stream (data flow), the corresponding I/O operations are blocking type

So the server program also uses blocking I/O
Perform read and write operations on the data. In this paper, the TCP long connection mode is discussed and the related design of the hairstyle server, in order to realize the concurrency requirement of the server program, the system is monitored by a separate main thread for user-initiated
Connection request, always in a blocking state; When a user connection request arrives, the program will start a new thread to uniformly handle the read and write operations of the user data.
The advantages of this model are simple, practical and manageable; however, the drawbacks are obvious: Because each client is assigned a thread to process the input and output data, its thread is approximately 1:1 proportional to the client.
, as the number of threads increases, the server initiates a large number of concurrent threads, which greatly increases the administrative overhead of the system on threads
, which will be a major cause of throughput bottlenecks
Secondly, due to the synchronous mode of the bottom-level I/O operation, the blocking management granularity of I/O operation is in the service of the requested thread, and it is possible that a large number of threads will be idle and in a blind state, resulting in low I/O resource utilization and affecting the performance of the whole system.
For the same hairstyle server, the system is used for blocking I/O waits and switching between threads much more time than
CPU,
Data processing time, so traditional blocking I/O has become a bottleneck that restricts the performance of the system. Java1.4 version
Post-Launch NIO toolkit that provides asynchronous input and output of non-blocking I/O
Mechanism
, to improve the performance of the system to provide
The underlying mechanisms that can be implemented.
NIO Package and working principle
In response to the lack of traditional I/O operating modes, the NIO Toolkit proposes a buffer-based, channel (pass-
Selector (selector), Selector (selector), selectable channel (channel), and
The Selectionkey (select key) is used together to achieve concurrent non-blocking I/O capability.
Members of the NIO Toolkit

Buffer (buffer)

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
  1. Package sample;
  2. Import Java.io.FileInputStream;
  3. Import Java.io.FileOutputStream;
  4. Import Java.nio.ByteBuffer;
  5. Import Java.nio.channels.FileChannel;
  6. public class CopyFile Www.2018yulpt.com {
  7. public static void Main (string[] args) throws Exception {
  8. String infile = "C:\\copy.sql";
  9. String outfile = "C:\\copy.txt";
  10. Get input and output streams for source and destination files
  11. FileInputStream fin = new FileInputStream (infile);
  12. FileOutputStream fout = new FileOutputStream (outfile);
  13. Get input and output channels
  14. FileChannel fcin = Fin.getchannel (www.thqpt.com);
  15. FileChannel fcout = Fout.getchannel ();
  16. Creating buffers
  17. Bytebuffer buffer = bytebuffer.allocate (1024);
  18. while (true) www.fencaiyule.cn {
  19. The Clear method resets the buffer so that it can accept the read-in data
  20. Buffer.clear ();
  21. Reading data from input channels to buffers
  22. int r = fcin.www.dongfan178.com/read (buffer);
  23. The Read method returns the number of bytes read, possibly zero, and returns 1 if the channel has reached the end of the stream.
  24. if (r = =-1) {
  25. Break
  26. }
  27. The flip method allows the buffer to write newly read-in data to another channel
  28. Buffer.flip ();
  29. Write data to buffer from output channel
  30. Fcout.write (buffer);
  31. }
  32. }
  33. }

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
  1. Package nio.readpage;
  2. Import Java.nio.ByteBuffer;
  3. Import Java.nio.channels.SocketChannel;
  4. Import Java.nio.charset.Charset;
  5. Import java.net.InetSocketAddress;
  6. Import java.io.IOException;
  7. public class Baidureader {
  8. Private Charset Charset = Charset.forname ("GBK");//create GBK Character Set
  9. Private Socketchannel channel;
  10. public void Readhtmlcontent () {
  11. try {
  12. Inetsocketaddress socketaddress = new Inetsocketaddress (
  13. "Www.baidu.com", 80);
  14. Step1: Open Connection
  15. Channel = Socketchannel.open (socketaddress);
  16. Step2: Sending a request using GBK encoding
  17. Channel.write (Charset.encode ("GET" www.yongxinzaixian.cn+ "/http/1.1" + "\r\n\r\n"));
  18. Step3: Reading data
  19. Bytebuffer buffer = bytebuffer.allocate (1024);//create 1024-byte buffer
  20. while (channel.read (buffer)! =-1) {
  21. Buffer.flip (www.078881.cn);//The Flip method is called before the read buffer byte operation.
  22. SYSTEM.OUT.PRINTLN (Charset.decode (buffer));
  23. Convert bytes to strings using the Charset.decode method
  24. Buffer.clear ();//emptying buffer
  25. }
  26. } catch (IOException e) {
  27. System.err.println (E.tostring ());
  28. } finally {
  29. if (channel! = NULL) {
  30. try {
  31. Channel.close ();
  32. } catch (IOException e) {
  33. }
  34. }
  35. }
  36. }
  37. public static void Main (string[] args) {
  38. New Baidureader (). Readhtmlcontent ();
  39. }
  40. }

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)

The first thing to know is what the so-called Java NIO is!

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.