Implementing the IO model and Selector of Java underlying technology
In Java programming, the IO (Input-Output) model and Selector are very important underlying technologies. They are very important for Handling network communications and file operations efficiently is critical. In this article, we will delve into the implementation principles of the IO model and Selector in Java, and provide specific code examples to help readers better understand these concepts.
1. IO model
2. Use of Selector
Selector is an important component in the Java NIO library. Through Selector, single-threaded management of multiple Channel IO operations can be achieved. Selector provides an efficient IO multiplexing mechanism that can greatly improve the efficiency of network communication.
The basic steps for using Selector are as follows:
Through the above steps, we can see that when using the Selector, you need to create the object first, then register the channel for IO operations to the Selector, and then perform the IO event Selection and processing.
Let’s look at a specific example to implement a simple Selector-based server and client communication program.
Server code example:
public class Server { public static void main(String[] args) throws IOException { ServerSocketChannel serverSocket = ServerSocketChannel.open(); serverSocket.socket().bind(new InetSocketAddress(8888)); serverSocket.configureBlocking(false); Selector selector = Selector.open(); serverSocket.register(selector, SelectionKey.OP_ACCEPT); while (true) { int readyChannels = selector.select(); if (readyChannels == 0) continue; Set<SelectionKey> selectedKeys = selector.selectedKeys(); Iterator<SelectionKey> keyIterator = selectedKeys.iterator(); while (keyIterator.hasNext()) { SelectionKey key = keyIterator.next(); if (key.isAcceptable()) { SocketChannel client = serverSocket.accept(); client.configureBlocking(false); client.register(selector, SelectionKey.OP_READ); System.out.println("客户端连接:" + client.getRemoteAddress()); } else if (key.isReadable()) { SocketChannel client = (SocketChannel) key.channel(); ByteBuffer buffer = ByteBuffer.allocate(1024); int bytesRead = client.read(buffer); while (bytesRead > 0) { buffer.flip(); while (buffer.hasRemaining()) { System.out.print((char) buffer.get()); } System.out.println(); bytesRead = client.read(buffer); } } keyIterator.remove(); } } } }
Client code example:
public class Client { public static void main(String[] args) throws IOException { SocketChannel socket = SocketChannel.open(); socket.configureBlocking(false); socket.connect(new InetSocketAddress("localhost", 8888)); Selector selector = Selector.open(); socket.register(selector, SelectionKey.OP_CONNECT); while (true) { int readyChannels = selector.select(); if (readyChannels == 0) continue; Set<SelectionKey> selectedKeys = selector.selectedKeys(); Iterator<SelectionKey> keyIterator = selectedKeys.iterator(); while (keyIterator.hasNext()) { SelectionKey key = keyIterator.next(); if (key.isConnectable()) { if (socket.finishConnect()) { socket.register(selector, SelectionKey.OP_WRITE); System.out.println("客户端连接成功"); } } else if (key.isWritable()) { ByteBuffer buffer = ByteBuffer.allocate(1024); buffer.put("Hello, Server".getBytes()); buffer.flip(); while (buffer.hasRemaining()) { socket.write(buffer); } System.out.println("发送数据到服务端"); } keyIterator.remove(); } } } }
Through the above code example, we can see how to use Selector to communicate between the server and the client. In the server, we first create a ServerSocketChannel and register it on the Selector, then select IO events in the loop and handle the client's connection request and data reading; in the client, we create a SocketChannel and register it on the Selector, and then in the loop Select IO events and handle connections and data sending.
Summary
Through the introduction and sample code of this article, I hope readers can better understand the implementation principles and usage of the IO model and Selector in Java. In-depth study and mastery of these underlying technologies is crucial to writing efficient network communication and file operation programs. It should be noted that in actual development, appropriate IO models and technologies need to be selected according to specific needs and scenarios to better meet the requirements of the project.
In the process of learning, readers can also deepen their understanding and mastery of these underlying technologies by reading more relevant materials and referring to more practical application cases. At the same time, continuous practice and experimentation will help readers understand these concepts more deeply and be able to apply them skillfully to actual project development.
The above is the detailed content of How to implement the IO model and Selector of Java underlying technology. For more information, please follow other related articles on the PHP Chinese website!