Detailed introduction to synchronization and asynchrony in Java
Process synchronization is used to achieve the reproducibility of program concurrent execution.
1、 Concepts of process synchronization and asynchrony
1. Process synchronization: when a function call is issued, the call will not return until the result is obtained. That is, you must do one thing at a time, and you can't do the next thing until the previous one is finished It's like getting up in the morning, washing first and then eating. You can't start eating when the washing is not finished According to this definition, in fact, most functions are called synchronously (such as sin, isDigit, etc.). However, generally speaking, when we talk about synchronization and asynchrony, we specifically refer to tasks that require the cooperation of other components or take a certain time to complete. The most common example is
sendmessage。 This function sends a message to a window. This function does not return until the other party has processed the message. After the other party completes processing, the function returns the lresult value returned by the message processing function to the caller.
2. Asynchronous
The concept of asynchrony is relative to synchronization. When an asynchronous procedure call is issued, the caller can not immediately get the result. The part that actually handles the call notifies the caller through status, notification, and callback after completion.
Take the casycsocket class as an example (note that CSocket is derived from CAsyncSocket, but its function has been transformed from asynchronism to synchronization). When a client sends a connection request after calling the connect function, the caller thread can run downwards immediately. When the connection is really established, the bottom of the socket will send a message to the object.
It is mentioned here that the executing part and the caller return results in three ways: status, notification and callback. Which implementation can be used depends on the execution part and is not controlled by the caller unless the execution part provides multiple choices. If the execution part is notified by status, the caller needs to check it at regular intervals, The efficiency is very low (some beginners of multi-threaded programming always like to use a loop to check the value of a variable, which is actually a very serious error). If you use notification, the efficiency is very high, because the execution part hardly needs to do additional operations. As for callback functions, there is not much difference between callback functions and notification.
Basic concepts of process synchronization
In the computer system, resource competition and sharing between processes are caused by limited resources. Therefore, the concurrent execution of processes is not only the result of the randomness of the execution start time of user programs and the improvement of resource utilization, but also the restriction of resource competition and sharing on the execution process of processes caused by limited resources. So, what are the constraints in the process of concurrent execution?
2、 Synchronous and asynchronous transmission:
1. Asynchronous transmission
Generally, asynchronous transmission takes characters as the transmission unit, and each character must be attached with 1 start bit and 1 stop bit to mark the beginning and end of a character, so as to realize data transmission synchronization. The so-called asynchronous transmission means that the time interval between characters (from the end of one character to the beginning of the next character) is variable, and there is no need to strictly limit their time relationship. The start bit corresponds to the binary value 0, expressed as a low level and occupies a 1-bit width. The stop bit corresponds to the binary value 1, which is represented by high level and occupies 1 ~ 2 bit width. A character occupies 5 ~ 8 bits, depending on the character set used by the data. For example, the telegraph code character is 5 bits, the ASCII code character is 7 bits, and the Chinese character code is 8 bits. In addition, a 1-bit parity bit should be added. Odd or even parity can be selected to implement simple error control for the character. In addition to adopting the same data format (number of characters, number of stop bits, whether there is check bit and check method, etc.), the transmitting end and the receiving end shall also adopt the same transmission rate. Typical rates are 9600b / s, 19.2kb/s, 56KB / s, etc.
Asynchronous transmission is also called start stop asynchronous communication. Its advantage is simple and reliable. It is suitable for character oriented and low-speed asynchronous communication. For example, the communication between computer and modem is in this way. Its disadvantage is that the communication overhead is large, and 2 ~ 3 bits must be added for each character transmitted, so the communication efficiency is relatively low. For example, when using modem to surf the Internet, it is generally felt that the speed is very slow. In addition to the low transmission rate, it is also closely related to high communication overhead and low communication efficiency.
2. Synchronous transmission
Generally, synchronous transmission is based on data blocks. A special character or bit sequence shall be attached to the head and tail of each data block to mark the beginning and end of a data block. Generally, a verification sequence (such as 16 bit or 32-bit CRC verification code) shall be added to control the error of the data block. The so-called synchronous transmission means that the time interval between data blocks is fixed, and their time relationship must be strictly specified.
3、 Synchronous blocking and asynchronous blocking:
Synchronization is blocking mode and asynchronous is non blocking mode.
My understanding: synchronization means that the operation of two threads is related. One thread has to block and wait for the operation of the other thread. Asynchronous means that two threads are unrelated and run their own.
Synchronization refers to the communication mode in which the sender sends data and the receiver sends a response before sending the next data packet.
Asynchrony refers to the communication mode in which the sender sends data, does not wait for the receiver to send back the response, and then sends the next data packet.
Take an inappropriate example, such as:
SendMessage does not return when calling. Trace0 is executed only after the message response. This is synchronization
PostMessage is returned immediately after calling, and trace0 is executed without message response, which is asynchronous
4、 Other explanations:
The difference between synchronous and asynchronous
For example: normal B / S mode (synchronous) Ajax Technology (asynchronous)
Synchronization: submit the request - > wait for the server to process - > return after processing. During this period, the client browser cannot do anything
Asynchronous: the request is triggered by an event - > processed by the server (this is the browser can still do other things) - > processing is completed
Synchronization is that you ask me to have dinner, and I will have dinner with you when I hear it; If you don't hear it, you keep yelling until I tell you you hear it, and then we go to dinner together.
Asynchrony is that you call me and go to dinner by yourself. I may leave immediately after I get the news, or I may not go to dinner until after work.
Therefore, if you want me to invite you to dinner, use synchronous method, and if you want me to eat, use asynchronous method, so you can save money.
For example, synchronous messaging on a phone call is asynchronous
summary
The above is all about the detailed introduction of synchronization and asynchrony in Java. I hope it will be helpful to you. Interested friends can continue to refer to this website:
Java implementation of a simple web crawler code example
Java NiO instance UDP sending and receiving data code sharing
Java Web applications use stream limiting to handle a large number of concurrent requests