Synchronous asynchronous and non-blocking, synchronous asynchronous Blocking

Source: Internet
Author: User

Synchronous asynchronous and non-blocking, synchronous asynchronous Blocking

Synchronous/asynchronous and blocking/non-blocking have different meanings in different scenarios.


In the operating system

Blocking statusIt means that when a running process is temporarily unable to continue execution due to something, the processor is abandoned and paused.

The process hasAsynchronousThe process is independent of each other and advances at an unpredictable speed, or the process entity runs asynchronously. Synchronization is required because of Asynchronization.

ProcessSynchronization: Coordinates multiple processes in the execution order, so that the concurrent processes can effectively share resources and cooperate with each other, so that the execution of the program can be reproducible.

The synchronous Asynchronization mentioned here must be performed between multiple processes. It is a process-driven approach. Asynchronization means there is no relationship between processes and mutual interference. Synchronization is related to processes and interference.


In terms of communication technology

The synchronous Asynchronization here is about whether the caller perceives that the called party has successfully called and whether the caller can receive feedback.

For example, James asked James to pick him up at the station. When James arrived at the station, he saw that the guests had not arrived. Instead, he did not return to his home and told his mother, but waited until the guests arrived to tell his mother. This isSynchronization.

For example, James asked James to pick him up at the station, but he was not sure about the arrival time of the guests. Then James went to the station every 10 minutes to check whether the guests arrived. He immediately returned to his house and told his mother. This isAsynchronous.


On IO operations

Blocking and non-blocking means whether the process needs to wait when the data accessed by the process is not ready..

For example, James asked James to pick him up at the station. Before James took the guest to his house, James did nothing. This is the blocking IO model.

For example, James asked James to pick up his mother at the station. Before James took the guest to his house, James could wash his food, cook and clean his house until the guest arrived, stop or greet the guests. This is a non-blocking IO model.

Synchronization and Asynchronization refer to the data access mechanism.Synchronization generally refers to the method of actively requesting and waiting for the completion of I/O operations. when the data is ready, it must be blocked when reading and writing (the difference is between the two phases: readiness and read/write, synchronous read/write must be blocked ). Asynchronous means that the process can continue to process other tasks after actively requesting data, and then wait for the notification of the completion of the I/O operation, which can prevent the process from being blocked during data read/write, but will wait for "notification ".

Synchronization is mostly blocked.But there are also non-blocking. Because the thread can do other things and does not always wait, it can be round-robin.

Asynchronization is mostly non-blockingBut it can also be blocked. Because the thread can do nothing else and has been waiting for notifications.

There are many similarities between synchronization and Asynchronization, blocking, and non-blocking. In some materials, the two are equivalent and can be exchanged.


NIO in java

Introduced in java 6NIO(New IO) package, throughSelectersNon-blocking IO is provided.

 

The Java NIO channel is similar to a stream, but it is somewhat different:

(1) data can be read from the channel and written to the channel. However, stream read/write is usually unidirectional.

(2) The channel can be read and written asynchronously.

(3) the data in the channel always needs to read a Buffer first, or always needs to be written from a Buffer.

 

Stream-oriented and buffer-oriented

The first major difference between Java NIO and IO is that IO is stream-oriented and NIO is buffer-oriented. JavaIO stream orientation means that one or more bytes are read from the stream each time until all bytes are read, and they are not cached anywhere. In addition, it cannot move data in the stream before and after. If you need to move the data read from the stream before and after, you must first cache it to a buffer zone.

The buffer-oriented method of Java NIO is slightly different. The data is read to a buffer that will be processed later. When necessary, it can be moved before and after the buffer. This increases the flexibility in the processing process. However, you also need to check whether the buffer contains all the data you need to process. In addition, make sure that when more data is read into the buffer, do not overwrite the unprocessed data in the buffer.

 

Blocking and non-blocking IO

Various Java IO streams are blocked. This means that when a thread calls read () or write (), the thread is blocked until some data is read or completely written. This thread can no longer do anything during this period.

The non-blocking mode of Java NIO enables a thread to send requests from a channel to read data, but it can only obtain currently available data. If no data is available, you won't get anything. Instead of keeping the thread congested, the thread can continue to do other things until the data becomes readable. This is also true for non-blocking writes. A thread requests to write some data to a channel, but does not need to wait for it to write completely. This thread can also do other things. Threads usually use the idle time of non-blocking IO to execute IO operations on other channels, so a separate thread can now manage multiple input and output channels ).

 

Selectors)

Java NIO selector allows a single thread to monitor multiple input channels. You can register multiple channels and use one selector. Then, you can use a separate thread to select a channel: there are input that can be processed in these channels, or select the channel to be written. This selection mechanism makes it easy for a single thread to manage multiple channels.

Selector allows a single thread to process multiple channels. If the application opens multiple connections (channels), but the traffic for each connection is low, it is very convenient to use Selector. For example, in a chat server.

The following figure shows how to use a Selector to process three channels in a single thread:


To use Selector, you must register a Channel with Selector and call its select () method. This method will be blocked until a registered channel has an event ready. Once this method is returned, the thread can process these events, such as new connections and data reception.

You can register events of interest on the channel. There are four types of events:

The server receives Client Connection events.SelectionKey. OP_ACCEPT

Client Connection server eventsSelectionKey. OP_CONNECT

Read eventSelectionKey. OP_READ

Write eventSelectionKey. OP_WRITE

 

Compared with the old Java. io library, java NIO does not need to be replaced, but proposes three new design ideas:

(1) encapsulation of the original type of read/write buffer

(2) The Channel-based read/write Mechanism further abstracts Stream.

(3) event polling/response design mode (I .e., Selector mechanism)

According to the above ideasThe Channel mechanism is generated as a further abstraction of Stream.So what is the difference between the Channel and Stream? Literally, you can get the information: Stream is a Stream with a direction, while Channel is a Channel without specifying a direction. Therefore, read/write operations can be performed in the same Channel. The Channel name emphasizes the versatility of the data input and output objects in nio and provides the foundation for non-blocking implementation.

In Channel implementation, there are also read-only channels and write-only channels, which actually Abstract The read and write behaviors of the Channel.

The read/write status of Channel IO blocking is similar to that of the traditional java. io package. But there is only one buffer layer. Therefore, it is also feasible to use nio according to the original design idea. However, in essence, nio design is still non-blocking Input and Output Control, and control is handed over to programmers.

Therefore, from the design perspective, java. nio does not replace java. io packages, but provides more control options for java. io.

 

Relationship between Channel and Buffer

Buffer is mainly used in non-blocking Channel. If the Channel is not blocked, it must be returned immediately after each call, so that the read/write operations will not affect the other party. So, how much data can be read and written if the returned result is returned immediately? This is uncertain and depends on the current transmission status. Therefore, the Buffer as a Buffer provides a guarantee for information integrity. Each read/write operation first puts the data in the Buffer, and then calls the Channel's read/write method multiple times to operate on the data, relying on the Buffer status to judge the data integrity.

 

The following are the key Buffer implementations in Java NIO:

ByteBuffer

CharBuffer

DoubleBuffer

FloatBuffer

IntBuffer

LongBuffer

Protocol Buffer

These buffers cover the basic data types that can be sent through IO: byte, short, int, long, float, double, and char.

Java NIO also has a Mappedyteuffer used to indicate memory ing files.

As mentioned above, data is read from the channel to the buffer zone, and data is written from the buffer zone to the channel.


The figure above describes the process of sending data from a client to the server and then receiving data from the server. When sending data, the client must first store the data in the Buffer and then write the content in the Buffer to the channel. The server side must read data into the Buffer through the Channel, and then retrieve the data from the Buffer for processing.

Non-blocking IO instances

Important implementation classes in java NIO:

FileChannelRead and Write Data from a file.

DatagramChannelReads and writes data in the network through UDP.

SocketChannelRead and Write Data in the network through TCP.

ServerSocketChannelYou can listen to new TCP connections, just like the Web server. A SocketChannel is created for each new connection.

 

The following uses SocketChannel and ServerSocketChannel to implement non-blocking network communication.

 

Server:

Import java. io. IOException; import java.net. inetSocketAddress; import java. nio. byteBuffer; import java. nio. channels. selectionKey; import java. nio. channels. selector; import java. nio. channels. serverSocketChannel; import java. nio. channels. socketChannel; import java. util. iterator; public class NIOServer {// channel manager private Selector selector;/*** get a ServerSocket channel, initialize the channel * @ param port bound port * @ throws I OException */public void initServer (int port) throws IOException {// obtain a ServerSocket channel ServerSocketChannel serverChannel = ServerSocketChannel. open (); // set the channel to non-blocking serverChannel. configureBlocking (false); // bind the ServerSocket corresponding to the channel to the port serverChannel. socket (). bind (newInetSocketAddress (port); // obtain a channel manager this. selector = Selector. open (); // bind the channel manager to the channel and register SelectionKey for the channel. OP_ACCEPT event, after the event is registered, // when this event occurs When the event arrives, selector. select () will return. If the event does not reach selector. select (), it will be blocked all the time. ServerChannel. register (selector, SelectionKey. OP_ACCEPT);}/*** use round robin to listen for events to be processed on the selector. If yes, process */public void listen () throws IOException {System. out. println ("server started successfully! "); // Round-robin access selector while (true) {// The method returns when the registered event arrives; otherwise, the method will always block selector. select (); // gets the Iterator of the selected item in the selector. The selected item is the registered event Iterator ite = this. selector. selectedKeys (). iterator (); while (ite. hasNext () {SelectionKey key = (SelectionKey) ite. next (); // Delete the selected key to prevent repeated processing of ite. remove (); // client request connection event if (key. isAcceptable () {ServerSocketChannel server = (ServerSocketChannel) key. channel (); // obtain the SocketC channel connected to the client Hannelchannel = server. accept (); // set to non-blocking channel. configureBlocking (false); // you can send a channel to the client. write (ByteBuffer. wrap (newString ("sent a message to the client "). getBytes (); // after the connection to the client is successful, you must set the read permission for the channel to receive client information. Channel. register (this. selector, SelectionKey. OP_READ); // get readable events} else if (key. isReadable () {read (key );}}}} /*** event for processing the information sent from the client * @ param key * @ throws IOException */public void read (SelectionKey key) throws IOException {// the server can read the message: obtain the Socket channel SocketChannel = (SocketChannel) key of the event. channel (); // create the read buffer ByteBuffer buffer = ByteBuffer. allocate (10); channel. read (buffer); byte [] data = buffer. array (); String msg = newString (data ). trim (); System. out. println ("message received by the server:" + msg); ByteBuffer outBuffer = ByteBuffer. wrap (msg. getBytes (); channel. write (outBuffer); // send the message back to the client}/*** start the server test * @ throws IOException */public static void main (String [] args) throws IOException {NIOServer server = new NIOServer (); server. initServer (8000); server. listen ();}}


Client:

 

Import java. io. IOException; import java.net. inetSocketAddress; import java. nio. byteBuffer; import java. nio. channels. selectionKey; import java. nio. channels. selector; import java. nio. channels. socketChannel; import java. util. iterator; public class NIOClient {// channel manager private Selector selector;/*** get a Socket channel, perform Initialization on the channel * @ param ip connection Server ip * @ param port connection server port number * @ throws IOException */public void InitClient (String ip, int port) throws IOException {// obtain a Socket channel SocketChannel channel = SocketChannel. open (); // set the channel to be non-blocking channel. configureBlocking (false); // obtain a channel manager this. selector = Selector. open (); // The client connects to the server. In fact, the method execution does not implement the connection. You need to call/use the channel in the listen () method. finishConnect (); to connect to the channel. connect (newInetSocketAddress (ip, port); // bind the channel manager to the channel and register the SelectionKey for the channel. OP_CONNECT event. Channel. register (selector, SelectionKey. OP_CONNECT);}/*** use round robin to listen for events to be processed on the selector. If yes, process * @ throws IOException */public void listen () throws IOException {// round-robin access selector while (true) {selector. select (); // gets the Iterator ite = this for the items selected in the selector. selector. selectedKeys (). iterator (); while (ite. hasNext () {SelectionKey key = (SelectionKey) ite. next (); // Delete the selected key to prevent repeated processing of ite. remove (); // connect Event occurs if (key. isConnectable () {SocketChannelchannel = (SocketChannel) key. channel (); // if the connection is in progress, the connection if (channel. isConnectionPending () {channel. finishConnect ();} // set to non-blocking channel. configureBlocking (false); // you can send a channel to the server. write (ByteBuffer. wrap (newString ("sent a message to the server "). getBytes (); // after the connection to the server is successful, you must set the read permission for the channel to receive information from the server. Channel. register (this. selector, SelectionKey. OP_READ); // get readable events} else if (key. isReadable () {read (key );}}}} /*** event for processing the information sent from the server * @ param key * @ throws IOException */public void read (SelectionKey key) throwsIOException {// The same as the read method on the server}/*** start the client test * @ throws IOException */public static void main (String [] args) throws IOException {NIOClient client = new NIOClient (); client. initClient ("localhost", 8000); client. listen ();}}


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.