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

Example Analysis of BIO, NIO and AIO in Java

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the example analysis of BIO, NIO and AIO in Java, which is very detailed and has certain reference value. Friends who are interested must finish it!

Let's take an example to understand the concept. Take bank withdrawals as an example:

Synchronization: personally take the bank card to the bank to withdraw money (when using synchronous IO, Java handles IO read and write).

Async: entrusts a younger brother to take a bank card to the bank to withdraw money, and then give it to you (when using asynchronous IO, Java entrusts IO read and write to OS, and the data buffer address and size need to be passed to OS (bank card and password). OS needs to support asynchronous IO operation API).

Blocking: ATM queued to withdraw money, you can only wait (when using blocking IO, the Java call will block until the read and write is complete).

Non-blocking: withdraw money from the counter, take a number, and then sit on the chair to do something else. The equal sign broadcast will inform you to deal with it. You can't go until the number arrives. You can keep asking the lobby manager if you haven't arrived yet. If you haven't arrived yet, you can't go. (when using non-blocking IO, if you can't read and write, the Java call will return immediately, and continue reading and writing when the IO event distributor notifies you to read and write. Continue to cycle until the read and write is complete.

Java supports BIO, NIO and AIO:

Java BIO: synchronize and block, the server implementation mode is to connect one thread at a time, that is, when the client has a connection request, the server needs to start a thread for processing. If the connection does nothing, it will cause unnecessary thread overhead, which can of course be improved through the thread pool mechanism.

Java NIO: synchronous non-blocking, the server implements one thread per request, that is, all connection requests sent by the client are registered with the multiplexer, and the multiplexer starts a thread for processing only when the multiplexer polls that the connection has an Icano request.

Java AIO (NIO.2): asynchronous and non-blocking. The server implements a valid request for a thread. OS completes all the client's Icano requests and then informs the server application to start the thread for processing.

Analysis of applicable scenarios for BIO, NIO and AIO:

The BIO method is suitable for the architecture with a small number of connections and a fixed number of connections, which requires high server resources, and concurrency is limited to the application. JDK1.4 is the only choice before, but the program is intuitive, simple and easy to understand.

NIO is suitable for architectures with a large number of connections and short connections (light operation), such as chat servers, where concurrency is limited to applications, programming is more complex, and JDK1.4 begins to support it.

AIO is used in architectures with a large number of connections and long connections (heavy operations), such as photo album server, fully calling OS to participate in concurrent operations, programming is more complex, and JDK7 begins to support it.

In addition, Imap O belongs to the bottom operation, which needs the support of the operating system, and concurrency also needs the support of the operating system, so the performance of different operating systems will be significantly different.

The above is all the contents of the article "sample Analysis of BIO, NIO and AIO in Java". Thank you for reading! Hope to share the content to help you, more related knowledge, 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.

Share To

Servers

Wechat

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

12
Report