Java non blocking I / O usage

Most of the knowledge and examples come from O'Reilly's Java network programming, Fourth Edition, by elliotte rusty Harold (O'Reilly).

Introduction to non blocking I / O

Non blocking I / O (NiO) is a means to deal with high concurrency. In the case of high concurrency, the overhead of creating and recycling threads and switching between threads can not be ignored. At this time, non blocking I / O technology can be used. The core idea of this technology is to select a prepared connection every time, fill in as much data as possible that the connection can manage as soon as possible, and then move to the next standard Ready connection.

Client using non blocking I / O

In general, the client does not need to handle a high number of concurrent connections. In fact, non blocking I / O is mainly designed for servers, but it can also be used on clients. Because the design of the client is easier than that of the server, let's use the client for a simple demonstration.

First, the channel and buffer are introduced. The socketchannel class is used to create connections in non blocking I / O. to obtain the socketchannel object, a socketaddress object (usually its subclass inetsocketaddress) needs to be passed into its static factory method open(). Here is an example:

The open () method is blocked, so the code after that will not be executed until the connection is established. If the connection cannot be established, an IOException will be thrown.

After the connection is established, you need to obtain input and output. Unlike the traditional getinputstream () and getoutputstream (), you can write directly to the channel itself by using the channel. Instead of writing a byte array, write a ByteBuffer object. ByteBuffer object through ByteBuffer Allocate (int capacity) is obtained. Capacity is the buffer size in bytes:

After obtaining the ByteBuffer object, pass it to the read () method of the socketchannel object. The socketchannel object will fill the buffer with the data read from the socket. The read () method returns the number of bytes successfully read and stored in the buffer. By default, it reads at least one byte, or returns - 1 to indicate the end of the data and blocks when no bytes are available. This is roughly the same behavior as InputStream. However, if it is set to non blocking mode, it will immediately return 0 when no bytes are available and will not block.

Now assume that there is some data in the buffer, and then you need to extract them. You can use the traditional way to write data to a byte array first, and then to an output stream. Here is a completely channel based method: encapsulate the output stream into a channel using the channels tool class:

The above code will system Out is encapsulated into a channel. After that, you can output. Before each output, the ByteBuffer object needs to call its flip () method to make the channel read from the beginning. After reading and writing, you also need to call its clear () method to reset the state of the buffer. The following is the code for data output:

Example 1: chargenerator client implemented with non blocking I / O

Server code:

Client code:

Enable non blocking mode

The above program is not very different from the traditional way of using input / output streams. However, you can call the configureblocking (false) method of ServerSocket to set it to non blocking mode. In this mode, if no data is available, the read () method will return immediately, which allows the client to do other things. However, since the read () method will return 0 when it cannot read data, the loop for reading data needs to be changed:

summary

The above is all about the use of Java non blocking I / O in this article. I hope it will be helpful to you. Welcome to: one way communication in the basic chapter of Java network programming, examples of Java using agents for network connection, etc. if you have any questions, you can leave a message at any time. Xiaobian will reply to you in time. Thank you for your support!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>