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/03 Report--
This article will explain in detail the example analysis of the Istroke O model in Java. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Related concepts
Synchronous and asynchronous
Describes how the user thread interacts with the kernel:
Synchronization means that the user thread needs to wait or poll the kernel Iploo operation after initiating the Iramo request before it can continue execution.
Asynchronism means that the user thread continues to execute after initiating the Ithumb O request, and notifies the user thread when the kernel Ithumb O operation is completed, or calls the callback function registered by the user thread.
Blocking and non-blocking
Describes the way in which the user thread invokes the kernel Ithumb O operation:
Blocking means that the Imax O operation needs to be completely completed before returning to user space.
Non-blocking means that the Iswap O operation returns a status value to the user immediately after it is called, without having to wait for the Iswab O operation to be completed.
An Icano operation is actually divided into two steps: the initiation of the Icano request and the actual Icano operation. The difference between blocking Iripple O and non-blocking Iripple O is that, in the first step, whether the request is blocked or not. If the blocking is completed, then it is the traditional blocking Iripple O, and if it is not blocked, it is the non-blocking Iripple O. The difference between synchronous and asynchronous Iripple O lies in whether the second step is blocked. If the actual Iripple O reads and writes block the request process, then it is synchronous Iripple O.
Unix I-paw O model
There are five Istroke O models under Unix:
Blocking IPUBO
Non-blocking IPUBO
IPot O multiplexing (select and poll)
Signal-driven Icano (SIGIO)
Asynchronous aio_ O (POSIX's series of functions)
Blocking IPUBO
If the request cannot be completed immediately, it remains blocked.
Phase 1: wait for the data to be ready. In the case of network Icano, it is waiting for the remote data to arrive one after another; in the case of disk Icando O, it is waiting for the disk data to be read from the disk to the kernel memory.
Phase 2: data is copied from the kernel to the process. For the sake of system security, the user-mode program does not have the permission to read the kernel-state memory directly, so the kernel is responsible for copying the data from the kernel-state memory into the user-mode memory.
Non-blocking IPUBO
Setting socket to NONBLOCK (non-blocking) tells the kernel not to sleep the process when the requested Imax O operation cannot be completed, but to return an error code (EWOULDBLOCK) so that the request does not block.
The Icano operation function will constantly test whether the data is ready, and if not, continue to test until the data is ready. Although the user thread can return immediately after each request, it still needs to poll and repeat the request constantly in order to wait for the data, which consumes a lot of CPU resources.
The data is ready to be copied from the kernel to user space.
In general, this model is rarely used directly, but the feature of non-blocking Imax O is used in other Imax O models. This method does not make much sense for a single Ihammer O request, but paves the way for Imax O multiplexing.
Icano reuse (asynchronous blocking Imaco)
Select or poll functions are used in Icano multiplexing, which also block the process, but unlike blocking IWeiO, these two functions can block multiple Icano operations at the same time. Moreover, multiple read operations and multiple write operations can be detected at the same time, and the Icano operation function is not really called until there is data to read or write.
From a process point of view, there is not much difference between using the select function to make an I _ select O request and the synchronous blocking model, and it is even more inefficient to add monitoring socket and additional operations to call the select function. However, the biggest advantage after using select is that the user can process multiple socket I _ Candle requests simultaneously in a single thread. Users can register multiple socket, and then constantly call select to read the activated socket, which can achieve the purpose of simultaneously processing multiple socket requests in the same thread. In the synchronous blocking model, this can only be achieved through multi-threading.
The Reactor design pattern is used to implement this mechanism in the Ipaw O multiplexing model.
Call select / poll this method is responsible for polling multiple socket by a user-mode thread until the data of some stage 1 is ready, and then notifies the actual user thread to execute a copy of phase 2. The asynchronization of phase one is simulated by a full-time user-mode thread to perform non-blocking Ithumb O polling.
Signal-driven Icano (SIGIO)
First of all, we allow socket to do a signal-driven Ihand O and install a signal handler so that the process continues to run without blocking. When the data is ready, the process receives a SIGIO signal, which can be called in the signal processing function to process the data.
Asynchronous IPUBO
Call the aio_read function, tell the kernel the description word, buffer pointer, buffer size, file offset, and notification method, and return immediately. When the kernel copies the data to the buffer, it notifies the application.
This mechanism is implemented by using the Proactor design pattern in the asynchronous IBG O model.
Tell the kernel that when the whole process (including phases 1 and 2) is complete, notify the application to read the data.
Comparison of several Icano models
The difference between the first four models is that phase 1 is different and phase 2 is basically the same, all of which copy data from the kernel to the caller's buffer. However, the two stages of asynchronous Ipaw O are different from the first four models.
The synchronous Iswap O operation blocks the request process until the Iripple O operation completes. The asynchronous Iswap O operation does not block the request process.
Common Java I PUBO model
After learning about UNIX's Imax O model, in fact, Java's Imax O model is similar.
Blocking Ipaw O mode
The EchoServer in the Socket section in the previous section is a simple example of blocking Imax O, after the server starts, waiting for the client to connect. After the client connects to the server, the server blocks the read and write data stream.
EchoServer Code:
Public class EchoServer {public static int DEFAULT_PORT = 7 th public static void main (String [] args) throws IOException {int port;try {port = Integer.parseInt (args [0]);} catch (RuntimeException ex) {port = DEFAULT_PORT;} try (ServerSocket serverSocket = new ServerSocket (port); Socket clientSocket = serverSocket.accept (); PrintWriter out = new PrintWriter (clientSocket.getOutputStream (), true); BufferedReader in = new BufferedReader (new InputStreamReader (clientSocket.getInputStream ();) {String inputLine While ((inputLine = in.readLine ())! = null) {out.println (inputLine);}} catch (IOException e) {System.out.println ("Exception caught when trying to listen on port" + port + "or listening for a connection"); System.out.println (e.getMessage ());}
Improved to "blocking Ithumb O+ multithreading" mode
Use multithreading to support multiple clients to access the server.
Main thread MultiThreadEchoServer.java
Public class MultiThreadEchoServer {public static int DEFAULT_PORT = 7 th public static void main (String [] args) throws IOException {int port;try {port = Integer.parseInt (args [0]);} catch (RuntimeException ex) {port = DEFAULT_PORT;} Socket clientSocket = null;try (ServerSocket serverSocket = new ServerSocket (port);) {while (true) {clientSocket = serverSocket.accept (); / / MultiThreadnew Thread (new EchoServerHandler (clientSocket)). Start () } catch (IOException e) {System.out.println ("Exception caught when trying to listen on port" + port + "or listening for a connection"); System.out.println (e.getMessage ());}
Processor class EchoServerHandler.java
Public class EchoServerHandler implements Runnable {private Socket clientSocket;public EchoServerHandler (Socket clientSocket) {this.clientSocket = clientSocket;} @ Overridepublic void run () {try (PrintWriter out = new PrintWriter (clientSocket.getOutputStream (), true); BufferedReader in = new BufferedReader (new InputStreamReader (clientSocket.getInputStream ();) {String inputLine;while ((inputLine = in.readLine ())! = null) {out.println (inputLine);}} catch (IOException e) {System.out.println (e.getMessage ());}
There is a problem: every time a new connection is received, a new thread will be created, and the thread will be destroyed after processing, which is costly. When there are a large number of short connections, the performance is low.
Improved to "blocking Istroke O + thread pool" mode
In view of the overhead caused by repeated thread creation and destruction in the above multithreaded model, thread pool can be used to optimize. Each time a new connection is received, an idle thread is taken from the pool for processing, and then put back into the pool after processing is completed. The reuse thread avoids the overhead of creating and destroying threads frequently.
Main thread ThreadPoolEchoServer.java
Public class ThreadPoolEchoServer {public static int DEFAULT_PORT = 7 th public static void main (String [] args) throws IOException {int port;try {port = Integer.parseInt (args [0]);} catch (RuntimeException ex) {port = DEFAULT_PORT;} ExecutorService threadPool = Executors.newFixedThreadPool (5); Socket clientSocket = null;try (ServerSocket serverSocket = new ServerSocket (port);) {while (true) {clientSocket = serverSocket.accept (); / / Thread PoolthreadPool.submit (new Thread (new EchoServerHandler (clientSocket) } catch (IOException e) {System.out.println ("Exception caught when trying to listen on port" + port + "or listening for a connection"); System.out.println (e.getMessage ());}
The problem: performance improves in scenarios with a large number of short connections, because instead of creating and destroying threads every time, threads in the connection pool are reused. However, in scenarios with a large number of long connections, there is no advantage because threads are occupied by connections for a long time and do not need to be created and destroyed frequently.
Although this method can be applied to the number of concurrency of small to medium-sized clients, if the number of connections exceeds 100000 or more, the performance will be very poor.
Improved to "non-blocking Istroke O" mode
Although the network model of "blocking Ithreading O+ thread pool" has better performance than the "blocking Icancaneo + multithreading" network model, there is a common problem between these two models: both read and write operations are blocked synchronously. In the case of large concurrency (continuous large number of connections and simultaneous requests), it takes a lot of threads to maintain the connection. CPU frequently switches between a large number of threads, resulting in a great loss of performance. Once the connection of a single machine exceeds 10,000, or even reaches tens of thousands, the performance of the server will decline sharply.
However, the Selector of NIO solves this problem very well, using the main thread (one thread or the number of CPU threads) to maintain all connections, managing and reading the data of the client connection, and handing the read data to the later thread pool for processing. After the thread pool has finished processing the business logic, it gives the result to the main thread to send the response to the client, and a small number of threads can handle a large number of connection requests.
Java NIO consists of the following core components:
Channel
Buffer
Selector
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.
Main thread NonBlokingEchoServer.java
Public class NonBlokingEchoServer {public static int DEFAULT_PORT = 7 th public static void main (String [] args) throws IOException {int port;try {port = Integer.parseInt (args [0]);} catch (RuntimeException ex) {port = DEFAULT_PORT;} System.out.println ("Listening for connections on port" + port); ServerSocketChannel serverChannel;Selector selector;try {serverChannel = ServerSocketChannel.open (); InetSocketAddress address = new InetSocketAddress (port); serverChannel.bind (address); serverChannel.configureBlocking (false); selector = Selector.open () ServerChannel.register (selector, SelectionKey.OP_ACCEPT);} catch (IOException ex) {ex.printStackTrace (); return;} while (true) {try {selector.select ();} catch (IOException ex) {ex.printStackTrace (); break;} Set readyKeys = selector.selectedKeys (); Iterator iterator = readyKeys.iterator (); while (iterator.hasNext ()) {SelectionKey key = iterator.next (); iterator.remove (); try {if (key.isAcceptable ()) {ServerSocketChannel server = (ServerSocketChannel) key.channel () SocketChannel client = server.accept (); System.out.println ("Accepted connection from" + client); client.configureBlocking (false); SelectionKey clientKey = client.register (selector,SelectionKey.OP_WRITE | SelectionKey.OP_READ); ByteBuffer buffer = ByteBuffer.allocate (100); clientKey.attach (buffer);} if (key.isReadable ()) {SocketChannel client = (SocketChannel) key.channel (); ByteBuffer output = (ByteBuffer) key.attachment (); client.read (output) } if (key.isWritable ()) {SocketChannel client = (SocketChannel) key.channel (); ByteBuffer output = (ByteBuffer) key.attachment (); output.flip (); client.write (output); output.compact ();}} catch (IOException ex) {key.cancel (); try {key.channel (). Close ();} catch (IOException cex) {}
Improved to "Asynchronous Istroke O" mode
After the Java SE 7 version, the support of Asynchronous Izod O (NIO.2) is introduced, which provides a sharp tool for building high-performance network applications.
Main thread AsyncEchoServer.java
Public class AsyncEchoServer {public static int DEFAULT_PORT = 7 args public static void main (String [] args) throws IOException {int port;try {port = Integer.parseInt (args [0]);} catch (RuntimeException ex) {port = DEFAULT_PORT;} ExecutorService taskExecutor = Executors.newCachedThreadPool (Executors.defaultThreadFactory ()); / / create asynchronous server socket channel bound to the default grouptry (AsynchronousServerSocketChannel asynchronousServerSocketChannel = AsynchronousServerSocketChannel.open ()) {if (asynchronousServerSocketChannel.isOpen ()) {/ / set some optionsasynchronousServerSocketChannel.setOption (StandardSocketOptions.SO_RCVBUF, 4 * 1024) AsynchronousServerSocketChannel.setOption (StandardSocketOptions.SO_REUSEADDR, true); / / bind the server socket channel to local addressasynchronousServerSocketChannel.bind (new InetSocketAddress (port)); / / display a waiting message while. Waiting clientsSystem.out.println ("Waiting for connections..."); while (true) {Future asynchronousSocketChannelFuture = asynchronousServerSocketChannel.accept (); try {final AsynchronousSocketChannel asynchronousSocketChannel = asynchronousSocketChannelFuture.get (); Callable worker = new Callable () {@ Overridepublic String call () throws Exception {String host = asynchronousSocketChannel.getRemoteAddress (). ToString (); System.out.println ("Incoming connection from:" + host); final ByteBuffer buffer = ByteBuffer.allocateDirect (1024); / / transmitting datawhile (asynchronousSocketChannel.read (buffer). Get ()! =-1) {buffer.flip () AsynchronousSocketChannel.write (buffer). Get (); if (buffer.hasRemaining ()) {buffer.compact ();} else {buffer.clear ();}} asynchronousSocketChannel.close (); System.out.println (host + "was successfully served!"); return host;}}; taskExecutor.submit (worker);} catch (InterruptedException | ExecutionException ex) {System.err.println (ex); System.err.println ("\ n Server is shutting down...") / / this will make the executor accept no new threads// and finish all existing threads in the queuetaskExecutor.shutdown (); / wait until all threads are finishedwhile (! taskExecutor.isTerminated ()) {} break;}} else {System.out.println ("The asynchronous server-socket channel cannot be opened!");}} catch (IOException ex) {System.err.println (ex) } this is the end of the article on "sample Analysis of Igamot O Model in Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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