In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you the Java NIO principle diagram and text analysis and how to use code to achieve, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.
Foreword:
Recently, I have been analyzing hadoop's RPC (Remote Procedure Call Protocol, remote procedure call protocol), which is a protocol that requests services from remote computer programs over the network without knowing the underlying network technology. Can refer to: http://baike.baidu.com/view/32726.htm) mechanism, found that the implementation of hadoop's RPC mechanism mainly uses two technologies: dynamic proxy (dynamic proxy can refer to the blog: http://weixiaolu.iteye.com/blog/1477774) and java NIO. In order to correctly analyze the RPC source code of hadoop, I think it is necessary to study the principle and implementation of java NIO first.
I mainly analyze java NIO from two directions.
Specific analysis:
I. the difference between java NIO and blocking Ibank O
1. Blocking Ipaw O communication model
If you now know something about blocking iAccord O, we know that blocking InputStream.read O is blocked when calling the ServerSocket.accept () method, and it will not return until the data arrives (or times out); similarly, when you call the IMAGO () method, it will block until there is a client connection before it returns, and after each client connects, the server starts a thread to process the client's request. The schematic diagram of the communication model of blocking Icano is as follows:
If you analyze it carefully, you will find that there are some drawbacks to blocking iBlockO. According to the blocking Icano communication model, I summarize its two shortcomings:
When there are many clients, a large number of processing threads are created. And each thread takes up stack space and some CPU time.
Blocking can lead to frequent context switching, and most context switching may be meaningless.
In this case, the non-blocking iPink O has its application prospect.
2. Java NIO principle and communication model
Java NIO was first used in jdk1.4, and it can be described as either the "new Iripple O" or the non-blocking iBandO. Here's how java NIO works:
A dedicated thread handles all IO events and is responsible for distributing them.
Event-driven mechanism: triggers events when they arrive, rather than monitoring events synchronously.
Thread communication: threads communicate with each other through wait,notify and other means. Make sure that every context switch makes sense. Reduce unnecessary thread switching.
After reading some materials, the following is a diagram of how java NIO works as I understand it:
(note: the processing flow of each thread is probably to read data, decode, compute, encode, and send responses. )
The server side of Java NIO only needs to start a special thread to handle all IO events. How is this communication model implemented? Hehe, let's explore its mystery together. Java NIO uses a two-way channel (channel) for data transmission, rather than an one-way stream (stream), on which we can register events of interest. There are four types of events:
Event name corresponding value the server receives the client connection event SelectionKey.OP_ACCEPT (16) the client connection server event SelectionKey.OP_CONNECT (8) reads the event SelectionKey.OP_READ (1) writes the event SelectionKey.OP_WRITE (4)
The server and the client each maintain an object that manages the channel, which we call selector, which can detect events on one or more channels (channel). Let's take the server as an example. If a read event is registered on the selector of the server, the client sends some data to the server at some time. Blocking IBO will call the read () method to read the data, and the server of NIO will add a read event to the selector. The server-side processing thread will pollly access the selector, and if it finds that an event of interest arrives when accessing the selector, it handles these events, and if no event of interest arrives, the processing thread blocks until the event of interest arrives. The following is a schematic diagram of the communication model of java NIO that I understand:
II. Java NIO server and client code implementation
To better understand java NIO, simple code implementations for both the server side and the client side are posted below.
Server:
Package cn.nio; 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; / * * NIO server * @ author path * / public class NIOServer {/ / Channel manager private Selector selector / * get a ServerSocket channel and do some initialization work on it * @ param port bound port number * @ throws IOException * / public void initServer (int port) throws IOException {/ / get 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 port serverChannel.socket () .bind (new InetSocketAddress (port)); / / get a channel manager this.selector = Selector.open () / / bind the channel manager to the channel and register the SelectionKey.OP_ACCEPT event for the channel. After registering the event, / / when the event arrives, selector.select () will return, and will block if the event does not reach selector.select (). ServerChannel.register (selector, SelectionKey.OP_ACCEPT);} / * uses polling to listen for events on the selector that need to be handled, and if so, deal with * @ throws IOException * / @ SuppressWarnings ("unchecked") public void listen () throws IOException {System.out.println ("server starts successfully!") ; / / Poll access selector while (true) {/ / when the registered event arrives, the method returns Otherwise, the method will always block selector.select (); / / get the iterator of the selected item in selector, and 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 () / / get the channel connected to the client SocketChannel channel = server.accept (); / / set to non-blocking channel.configureBlocking (false) / / you can send a message to the client here, oh, channel.write (ByteBuffer.wrap (new String ("sent a message to the client"). GetBytes ()); / / after a successful connection with the client, in order to receive the information from the client, you need to set the read permission to the channel. Channel.register (this.selector, SelectionKey.OP_READ); / / got a readable event} else if (key.isReadable ()) {read (key) } / * handle events that read information sent by the client * @ param key * @ throws IOException * / public void read (SelectionKey key) throws IOException {/ / Server readable message: get the Socket channel where the event occurred SocketChannel channel = (SocketChannel) key.channel () / / create a read buffer ByteBuffer buffer = ByteBuffer.allocate (10); channel.read (buffer); byte [] data = buffer.array (); String msg = new String (data). Trim (); System.out.println ("Server receives message:" + 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:
Package cn.nio; 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; / * * NIO client * @ author path * / public class NIOClient {/ / Channel Manager private Selector selector / * get a Socket channel And do some initialization work 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 {/ / get a Socket channel SocketChannel channel = SocketChannel.open () / / set the channel to non-blocking channel.configureBlocking (false); / / get a channel manager this.selector = Selector.open (); / / the client connects to the server, but the method execution does not implement the connection, so you need to call / / use channel.finishConnect () in the listen () method. To complete the connection to channel.connect (new InetSocketAddress (ip,port)); / / bind the channel manager to the channel and register the SelectionKey.OP_CONNECT event for the channel. Channel.register (selector, SelectionKey.OP_CONNECT) } / * use polling to listen for events on the selector that need to be handled, if any Then deal with * @ throws IOException * / @ SuppressWarnings ("unchecked") public void listen () throws IOException {/ / Poll access selector while (true) {/ / Select a set of events that can be used for the Imax O operation, and put it in selector, the client's method will not block. / / the method here is different from that of the server. Looking at the api comments, you can see that when at least one channel is selected, the wakeup method of / / selector is called and the method returns, while for the client, the channel is always the selected selector.select () / / get the iterator Iterator ite = this.selector.selectedKeys () .iterator () of the selected item in selector; while (ite.hasNext ()) {SelectionKey key = (SelectionKey) ite.next (); / / delete the selected key to prevent repeated processing of ite.remove () / / connection event occurs if (key.isConnectable ()) {SocketChannel channel = (SocketChannel) key .channel () / / if you are connecting, complete the connection if (channel.isConnectionPending ()) {channel.finishConnect ();} / / set to non-blocking channel.configureBlocking (false) / / you can send a message to the server, oh, channel.write (ByteBuffer.wrap (new String ("sent a message to the server"). GetBytes ()); / / after a successful connection with the server, in order to receive the information from the server, you need to set the read permission to the channel. Channel.register (this.selector, SelectionKey.OP_READ); / / got a readable event} else if (key.isReadable ()) {read (key) } / * handle the event of reading information sent by the server * @ param key * @ throws IOException * / public void read (SelectionKey key) throws IOException {/ / same as the read method on the server} / * start client test * @ throws IOException * / public static void main (String [] args) throws IOException {NIOClient client = new NIOClient () Client.initClient ("localhost", 8000); client.listen ();}} the above content is Java NIO schematic analysis and how to implement it in code. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.