Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the IO model and Netty framework of Java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "what is Java's IO model and Netty framework". In daily operation, I believe many people have doubts about Java's IO model and Netty framework. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what is Java's IO model and Netty framework?" Next, please follow the editor to study!

What is Netty?

Asynchronous, event-driven network application framework for the rapid development of high-performance, highly reliable network IO programs

It is mainly aimed at high concurrency applications facing clients under TCP protocol.

It is essentially a NIO framework, suitable for scenarios such as server communication.

Asynchronous: sending a request does not need to wait for a response, and the program goes on.

Event driven: a connection event or disconnect event, or read event or write event, subsequent processing after it occurs.

Typical Netty applications:

The high-performance rpc framework is used for remote service (procedure) calls, such as Dubbo.

Game industry, page data exchange.

Big data areas such as Hadoop High performance Communications and Serialization components (AVRO).

IO model

The simple understanding is what channel is used to send and receive data.

BIO: connect one thread at a time, and the connection not doing anything will cause unnecessary thread overhead. It is suitable for architectures with a small and fixed number of connections.

NIO: a single thread (or multiple) on the server side to maintain a multiplexer. The IO thread is processed by the multiplexer. Suitable for architectures with large and short connections

AIO: asynchronous non-blocking, has not been widely used. It is suitable for architectures with a large number of connections and long connections.

Simple flow of BIOBIO programming

Server-side creation starts ServerSocket

The client initiates Socket to communicate with the server, and the default server creates a thread for each client.

After the client makes a request, it first asks the thread whether it is responding, and if not, it waits or refuses.

If there is a response, wait for the request to finish before continuing. (blocking)

BIO simple instance public class BIOserver {public static void main (String [] args) throws IOException {/ / create thread pool ExecutorService service = Executors.newCachedThreadPool () directly using Executors for convenience; / / specify server port ServerSocket serverSocket = new ServerSocket (6666); System.out.println ("server startup") While (true) {/ / blocking waiting for connection Socket socket = serverSocket.accept (); System.out.println ("connect to a client") / / each connection corresponds to a thread service.execute (new Runnable () {@ Override public void run () {try {handler (socket);} catch (Exception e) {e.printStackTrace () });} public static void handler (Socket socket) throws IOException {System.out.println ("Thread:" + Thread.currentThread (). GetId ()); byte [] bytes = new byte [1024]; InputStream inputStream = socket.getInputStream () While (true) {/ / blocking waiting for reading int n = inputStream.read (bytes); if (natively colored color 1) {System.out.println (new String (bytes,0,n));} else {break;}} socket.close ();}}

Testing: telnet using windows

Use ctrl+]

It can be seen in the server console that the sent data has been read.

NIO

Three core parts: Channel (comparable Socket), Buffer,Selector

It looks something like this. The client interacts with Buffer, and Buffer and Channel have an one-to-one relationship. Selector selects the action Channel (event-driven. If an event occurs in the Channel, the Selector selects the action. )

Basic use of BufferBuffer

ByteBuffer is widely used in scenarios.

Buffer is a block of memory, so nio is block / buffer oriented, and the underlying layer is an array. Data is read and written through buffer. You can use the method flip to switch between reads and writes.

Public class BufferNio {public static void main (String [] args) {/ / create a buffer capacity of 5 int IntBuffer buffer = IntBuffer.allocate (5); / / play data buffer.put (1); buffer.put (2); buffer.put (3); buffer.put (4); buffer.put (5); / / read / write switch buffer.flip () / / fetch data / / maintain an index internally. Each time the get index moves while (buffer.hasRemaining ()) {System.out.println (buffer.get ());} Buffer four main attributes / / Invariants: mark

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report