One-stop learning Java network programming comprehensive understanding of BIO_NIO_AIO, learning notes (2)

Hello everyone, I am, I 方圆
hope you all go forward


1. Character stream and byte stream under java.io

Insert picture description here

1.1 Character stream

The character stream is more convenient for us to use. Generally, characters are formed by multiple bytes. If we use byte stream transmission, we also need to convert it to characters by ourselves. If we use the character stream directly, so Can directly read and output characters.
Insert picture description here

  • CharArrayReader: It is a basic character input stream, which can read data from a character array
  • StringReader: Read data from the string input stream
  • Same for output stream

1.1.1 More advanced character streams

Insert picture description here

  • BufferedReader: Character input stream with buffer area, but we cannot directly instantiate it for us to use. Instead, we will pass in the Reader character stream we have created, just like it 升级, with more functions, such as this The character stream is to provide a buffer area, which speeds up the efficiency of io, without having to repeatedly read the original data source
  • FilterReader: It is also used 装饰模式, its additional function can filter the characters in the character stream, etc.
  • InputStreamReader: A bridge connecting byte stream and character stream, converting byte stream into character stream, the more commonly used is FileReader

1.2 Byte stream

Byte stream reads bytes one by one
Insert picture description here

  • ByteArrayInputStream: Byte array input stream, get data from byte array
  • FileInputStream: File input stream, get data from file
  • Same for output stream

1.2.1 More advanced byte streams

Insert picture description here

  • BufferedInputStream: Byte input stream with buffer area, it is also the same principle as BufferedReader, it also needs to be passed into the input Stream for upgrading
  • DataInputStream与DataOutputStream: These two are more interesting. They complement each other and are 基本数据类型related to the built-in java . You can use some methods, such as toLong or toInt, to directly convert the bytes in them into java basic data types.

2. Decorator mode

Insert picture description here
What we have just seen is that the BufferedReader, InputStreamReader and FilterReader in the character stream, and the BufferedInputStream, DataInputStreamReader and DataOutputStream in the byte stream all use the decorator pattern, because they cannot be instantiated and need to pass in the basic Reader or InputStream. To proceed 升级, here is the decorator mode.


3. Socket

  • We can think of Socket as 网络传输的端点a kind 数据源, binding a specific ip address and port number to it, so that it can realize the communication function. As shown in the figure below, the socket in the server is bound to the corresponding ip and port Number, to communicate with the client
    Insert picture description here

3.1 Send data through Socket

Insert picture description here

  1. Create Socket, bind specific ip address and port number
  2. Bind the Socket with the network card driver
  3. We can send data through Socket
  4. The network card driver reads Socket data

3.2 Read data through Socket

Insert picture description here

  • Similar to sending data, except that the process is reversed
  1. We still have to create Socket first
  2. Binding the Socket to the network card driver
  3. Socket receives data from the network card driver
  4. Read data from Socket

4. The concept of synchronous, asynchronous and blocking non-blocking

They have four different permutations and combinations of two-by-two combinations. Synchronous and asynchronous are emphasized 通信机制. In layman's terms, they can be related to the concept of responders; blocking and non-blocking emphasizes 调用状态, we can understand them as callers or request initiators
Insert picture description here

4.1 Synchronization (girl)

Insert picture description here
The boy confessed to the girl and 女孩immediately thought about and reacted to the incident. This is the synchronous communication mechanism

4.2 Asynchronous (girl)

Insert picture description here
The asynchronous communication mechanism is that the girl 不会immediately responds to the boy’s request, but the girl considers it first. If there is a result, I will send you a text message

4.3 Blocking (boys)

Insert picture description here
The boy is a melon skin. After he confessed to the girl automatically, he has no thoughts about food and food, waiting for the girl’s response.不做其他的事情

4.4 Non-blocking (boys)

Insert picture description here
This kind of boy is more powerful, able to coordinate life and love, and after confessing to a girl, he is not confused about the result, but 该干啥还去干啥this is a non-blocking call

4.5 Four permutations and combinations

  1. Synchronous blocking After the
    boy confessed, the girl immediately thought and reacted. The boy waited silly in the process and did not do anything else.
  2. Asynchronous blocking After the
    boy confessed, the girl slipped away, went home to think about how to respond, didn’t think immediately, the boy waited stupidly, don’t do anything else
  3. Synchronous non-blocking After the
    boy confessed, the boy did something else without waiting for you to reply, but the girl immediately fell into thought, thinking about the result of the reaction
  4. Asynchronous and non-blocking
    Both people are more likely to think about it. The boy finishes his confession and does something else. The girl does not immediately consider this matter. When he considers it, he will send a message to the boy.

Synchronous blocking, synchronous non-blocking, asynchronous blocking, asynchronous non-blocking


5. Thread pool in network communication

When we are processing high concurrent requests, if the following methods are used, it will cause a great waste of resources
Insert picture description here

The use of thread pool is to avoid this situation and save system resources

5.1 Thread pool provided by java

Insert picture description here
We can use Runnable and Callable to create tasks, use threads in the thread pool to perform tasks, and finally get results

5.2 java static method to create thread pool

Insert picture description here

  • newSingleThreadExecutor: There is only one thread in this thread pool, and we continue to reuse this thread
  • newFixedThreadPool: A thread pool with a fixed number of threads. When the thread capacity is exceeded, wait
  • newCachedThreadPool: This thread pool will create new threads to execute the extra tasks
  • newScheduledThreadPool: A thread pool capable of realizing timed processing tasks

forward!

Guess you like

Origin blog.csdn.net/qq_46225886/article/details/107465199