In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "brief introduction of Netty NIO". In daily operation, I believe many people have doubts about the brief introduction of Netty NIO. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "brief introduction of Netty NIO"! Next, please follow the editor to study!
1. Problems of synchronous blocking in traditional IO (BIO)
That is, the classes under the IO package in the traditional IO (that is, InputStream, OutputStream and other Java) and some network API provided under the java.net, such as Socket, ServerSocket and HttpURLConnection, are also classified as synchronous blocking IO class libraries, because network communication is also an IO behavior. Stops the current thread during a read-write operation (calling the read/write method), leaving the current thread in a blocking state until the read-write operation ends before the thread continues to execute
The synchronization blocking problem of traditional IO leads to its great performance defect, because each thread can only manage (run) one IO stream at the same time, especially for network applications, if the traditional IO method is adopted, then only one thread can manage to hold one IO stream, which is too big a performance bottleneck for the system in the case of concurrency, as shown below.
Import java.io.IOException;import java.io.InputStream;import java.net.ServerSocket;import java.net.Socket;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;/** * traditional socket server * * / public class ioServer {@ SuppressWarnings ("resource") public static void main (String [] args) throws Exception {ExecutorService newCachedThreadPool = Executors.newCachedThreadPool () / / create a socket service to listen on port 10101 ServerSocket server=new ServerSocket (10101); System.out.println ("Server starts!") ; while (true) {/ / get a socket (blocking) final Socket socket = server.accept (); System.out.println ("get a new client!") ; newCachedThreadPool.execute (new Runnable () {@ Override public void run () {/ / Business processing handler (socket)) }) }} / * read data * @ param socket * @ throws Exception * / public static void handler (Socket socket) {try {byte [] bytes = new byte [1024] InputStream inputStream = socket.getInputStream (); while (true) {/ / read data (blocking) int read = inputStream.read (bytes) If (read! =-1) {System.out.println (new String (bytes, 0, read));} else {break Catch (Exception e) {e.printStackTrace () } finally {try {System.out.println ("socket off"); socket.close () } catch (IOException e) {e.printStackTrace ();}
If you use the traditional IO method, you must set up a thread for each client connected to the server to deal with IO, which is fine when the concurrency is low, but once the concurrency is extremely high, resulting in the creation of a large number of threads, it will lead to very frequent inter-thread switching, which consumes a lot of system performance, and thread switching is useless consumption.
There is a difference between synchronization and blocking, and their modifiers are different.
Blocking and non-blocking refers to whether the process needs to wait if the data accessed by the process is not ready. To put it simply, this is equivalent to the difference between the implementation within the function, that is, whether to return directly or wait to be ready when it is not ready.
Synchronization and asynchronism refer to the mechanism of accessing data. Synchronization generally refers to the way of actively requesting and waiting for the operation of Imax O to finish. When the data is ready, it must be blocked when reading and writing, while async means that you can continue to process other tasks after actively requesting data, and then wait for the notification of the completion of the operation, so that the process does not block when reading and writing data.
2. NIO (AIO) Asynchronous non-blocking
In order to improve the problem of traditional IO, NIO framework (java.nio package) is introduced in Java 1.4. new classes such as Channel, Selector, Buffer and so on are provided, which can build multiplex, synchronous non-blocking IO programs, and provide a high-performance data operation mode closer to the bottom of the operating system. In Java 7, NIO has been further improved, that is, NIO 2, which introduces asynchronous non-blocking IO, and many people call it AIO (Asynchronous IO). Asynchronous IO operation is based on event and callback mechanism, which can be simply understood as: read and write operations return directly without blocking there. When background processing is completed, the operating system will notify the corresponding thread for follow-up work and send performance improvement again. For
NIO core implementation class
Java provides three core implementation classes for NIO, mainly buffer (Buffer), channel (Channel), selector (Selector).
1. Channel (Channel)
1. Channel: originally in traditional IO, streams are used for read and write operations, but in NIO, Channel is used for read and write operations, and Channel replaces streams in traditional IO.
2. Several concrete implementation classes of Channel are provided in Java. These channels cover UDP and TCP network IO, as well as file IO.
FileChannel: file IO
DatagramChannel:UDP Network IO
SocketChannel:TCP client Network IO
ServerSocketChannel:TCP server network IO
3. Channel has three characteristics:
Channel is readable and writable, but a Channel can only write or read
Channel can read and write asynchronously
Data is always read from Channel to Buffer, or written from Buffer to Channel
two。 Buffer (Buffer)
1. Channel is responsible for reading and writing data, while buffer Buffer is responsible for temporarily storing Channel read and write data, that is, cached data. All data will be written to Channel through Buffer or read and stored from Channel to Buffer. JavaNIO provides implementation classes for all basic data types for Buffer, covering the basic data types you can send through IO: byte, short, int, long, float, double and char. There is another MappedByteBuffer.
ByteBuffer
CharBuffer
DoubleBuffer
FloatBuffer
IntBuffer
LongBuffer
ShortBuffer
3. Selector (Selector)
1. This selection mechanism makes it easy for a single thread to manage multiple channels.
To use Selector, register Channel with Selector and then call its select () method. This method blocks until some registered channel has an event ready. Once this method returns, the thread can handle these events, such as new connections, data reception, and so on.
At this point, the study of "brief introduction to Netty NIO" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.