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 are the Java IO classifications

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces what the Java IO classification has, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Best practices for IO Operation

Use buffered IO classes and do not read bytes or characters separately

Use NIO and NIO 2 or AIO instead of BIO

Close the stream in finally

Use memory-mapped files to get faster IO

Java IO classification

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 implementation mode is to request one thread, that is, when a connection is created, it does not need a corresponding thread, and the connection will be registered with the multiplexer, so all connections can be handled by only one thread. When the multiplexer in this thread polls and finds that there is a request on the connection, a thread will be opened for processing. That is, one request, one thread mode. An important difference between BIO and NIO is that when we use BIO, we often introduce multithreading, each connection has a separate thread, while NIO uses a single thread or only a small number of multithreads, and each connection shares a thread.

Java AIO (NIO.2): asynchronous non-blocking, the server implementation mode is a valid request for a thread, and the client's Icano requests are completed by OS before informing the server application to start the thread for processing.

Name 5 best practices for IO

IO is very important to the performance of Java applications. Ideally, you should not avoid IO operations on the critical path of your application. Here are some Java IO best practices you should follow:

Use an IO class with a buffer instead of reading bytes or characters separately.

Using NIO and NIO2

Close the stream in the finally block, or use the try-with-resource statement.

Use memory-mapped files to get faster IO.

Analysis of applicable scenarios of BIO, NIO and AIO

BIO (synchronous and blocking) mode is suitable for architecture where the number of connections is relatively small and fixed. This method requires high server resources, and concurrency is limited to the application. JDK1.4 was the only choice before, but the program is intuitive, simple and easy to understand.

NIO (synchronous non-blocking) is suitable for architectures with a large number of connections and relatively 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 (Asynchronous non-blocking) method is used in architectures with a large number of connections and long connections (heavy operation), such as photo album server, fully calling OS to participate in concurrent operations, programming is more complex, JDK7 begins to support.

Main differences between Java NIO and IO

Stream-oriented and buffer-oriented. The first big difference between Java NIO and IO is that IO is stream-oriented and NIO is buffer-oriented. Java IO stream orientation means that one or more bytes are read from the stream at a time until all bytes are read, and they are not cached anywhere. In addition, it cannot move the data in the stream back and forth. If you need to move the data read from the stream back and forth, you need to cache it to a buffer first. The buffer-oriented approach of Java NIO is slightly different. The data is read into a buffer that it processes later and can be moved back and forth in the buffer as needed. This increases the flexibility in the process.

The various flows of blocked and non-blocking IO Java IO are blocked. This means that when a thread calls read () or write (), the thread is blocked until some data is read, or the data is fully written. The thread can no longer do anything during this time. Java NIO's non-blocking mode allows a thread to send a request to read data from a channel, but it can only get the data currently available, and if there is no data currently available, the thread can continue to do other things. The same is true of non-blocking writing. A thread requests to write some data to a channel, but instead of waiting for it to be fully written, the thread can do something else at the same time. Threads typically use the idle time of non-blocking IO to perform IO operations on other channels, so a single thread can now manage multiple input and output channels (channel).

Selectors Java NIO selectors allow a single thread to monitor multiple input channels, you can register multiple channels using a selector, and then use a separate thread to "select" channels: there is already input to be processed in these channels, or select channels that are ready to be written. This selection mechanism makes it easy for a single thread to manage multiple channels.

Two Design patterns of Java I Dot O Library

The overall design of the Java I Dot O library is consistent with both the decoration pattern and the adapter pattern. As mentioned earlier, the classes that deal with flows in this library are called flow classes.

Decorator: within the hierarchical structure represented by InputStream, OutputStream, Reader, and Writer, there are some stream processors that can decorate other stream processors to form new stream processors with improved functionality.

Adapter pattern (Adapter): within the hierarchy represented by InputStream, OutputStream, Reader, and Writer, some stream processors are adapted to other types of stream processors. This is the application of the adapter.

Thank you for reading this article carefully. I hope the article "what are the Java IO categories" shared by the editor will be helpful to everyone? at the same time, I also hope that you will support and pay attention to the industry information channel, and more related knowledge is waiting for you to learn!

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