Hello everyone, I am, I
方圆
hope you all go forward
table of Contents
1. Character stream and byte stream under java.io
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.
CharArrayReader
: It is a basic character input stream, which can read data from a character arrayStringReader
: Read data from the string input stream- Same for output stream
1.1.1 More advanced character streams
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 sourceFilterReader
: 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
ByteArrayInputStream
: Byte array input stream, get data from byte arrayFileInputStream
: File input stream, get data from file- Same for output stream
1.2.1 More advanced byte streams
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 upgradingDataInputStream与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
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
3.1 Send data through Socket
- Create Socket, bind specific ip address and port number
- Bind the Socket with the network card driver
- We can send data through Socket
- The network card driver reads Socket data
3.2 Read data through Socket
- Similar to sending data, except that the process is reversed
- We still have to create Socket first
- Binding the Socket to the network card driver
- Socket receives data from the network card driver
- 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
4.1 Synchronization (girl)
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)
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)
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)
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
- 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. - 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 - 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 - 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
The use of thread pool is to avoid this situation and save system resources
5.1 Thread pool provided by java
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
newSingleThreadExecutor
: There is only one thread in this thread pool, and we continue to reuse this threadnewFixedThreadPool
: A thread pool with a fixed number of threads. When the thread capacity is exceeded, waitnewCachedThreadPool
: This thread pool will create new threads to execute the extra tasksnewScheduledThreadPool
: A thread pool capable of realizing timed processing tasks
forward!