In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to use Buffer and Chanel in Java". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor learn how to use Buffer and Chanel in Java.
1. Basic concept
IO is the process of copying data from main memory and external devices (hard disk, terminal, network, etc.). IO is the underlying function of the operating system, which is completed by the Imax O instruction.
All language runtime systems provide tools at a higher level to perform Ihampo. (object-oriented encapsulation of printf scanf,java for c)
2. Review of Java standard io
The Java standard IO class library is an object-oriented abstraction of io. Based on the underlying implementation of the local method, we do not need to pay attention to the underlying implementation. InputStream\ OutputStream (byte stream): one byte at a time. Reader\ Writer (character stream): one character at a time.
3. Introduction to nio
Nio is the abbreviation of java New IO, the new api provided in jdk1.4. The official features of Sun are as follows:
Provides (Buffer) caching support for all primitive types.
-character set encoding and decoding solution.
-Channel: a new original Imax O abstraction.
-A file provider that supports locks and memory-mapped files.
-provide multi-channel (non-bloking) non-blocking and highly scalable network Icano.
This article will learn and introduce these features.
4. Buffer&Chanel
Channel and buffer are the two most basic data type abstractions of NIO.
Buffer:
-is a continuous block of memory.
-is a transit point for reading or writing NIO data.
Channel:
-Source or destination of data
-used to provide data to buffer or read buffer data, the only interface for buffer objects.
-Asynchronous IPUBO support
Package sample; import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class CopyFile {public static void main (String [] args) throws Exception {String infile = "C:\\ copy.sql"; String outfile = "C:\\ copy.txt" / / get the input and output streams of the source and target files FileInputStream fin = new FileInputStream (infile); FileOutputStream fout = new FileOutputStream (outfile); / / get the input and output channel FileChannel fcin = fin.getChannel (); FileChannel fcout = fout.getChannel (); / / create buffer ByteBuffer buffer = ByteBuffer.allocate (1024) The while (true) {/ / clear method resets the buffer so that it can accept the read data buffer.clear (); / / read the data from the input channel to the buffer int r = fcin.read (buffer) The / / read method returns the number of bytes read, which may be zero. If the channel has reached the end of the stream, it returns-1 if (r =-1) {break;} / / flip method so that the buffer can write newly read data to another channel buffer.flip () / / write data to buffer fcout.write (buffer) from the output channel;}
The internal structure of buffer is as follows (the following figure is copied from the data):
Common methods for Buffer:
Flip (): convert write mode to read mode
Rewind (): resets position to 0, which is generally used for repeat reading.
Clear (): clear buffer and prepare to be written again (position becomes 0 and limit becomes capacity).
Compact (): copies unread data to the head of the buffer.
Mark (), reset (): mark can mark a location, and reset can reset to that location.
Common types of Buffer: ByteBuffer, MappedByteBuffer, CharBuffer, DoubleBuffer, FloatBuffer, IntBuffer, LongBuffer, ShortBuffer.
Common types of channel: FileChannel, DatagramChannel (UDP), SocketChannel (TCP), ServerSocketChannel (TCP)
A simple performance test is done on this machine. The performance of my notebook is average. The specific code can be found in the attachment. See the example below in the nio.sample.filecopy package) the following is the reference data:
-scenario 1: Copy a 370m file
-scenario 2: three threads copy at the same time, each thread copies a 370m file
Common methods for Buffer:
Flip (): convert write mode to read mode
Rewind (): resets position to 0, which is generally used for repeat reading.
Clear (): clear buffer and prepare to be written again (position becomes 0 and limit becomes capacity).
Compact (): copies unread data to the head of the buffer.
Mark (), reset (): mark can mark a location, and reset can reset to that location.
Common types of Buffer: ByteBuffer, MappedByteBuffer, CharBuffer, DoubleBuffer, FloatBuffer, IntBuffer, LongBuffer, ShortBuffer.
Common types of channel: FileChannel, DatagramChannel (UDP), SocketChannel (TCP), ServerSocketChannel (TCP)
A simple performance test is done on this machine. The performance of my notebook is average. The specific code can be found in the attachment. See the example below in the nio.sample.filecopy package) the following is the reference data:
-scenario 1: Copy a 370m file
-scenario 2: three threads copy at the same time, each thread copies a 370m file
5. Nio.charset
Character encoding and decoding: the bytecode itself is just a number that is parsed correctly in the correct context. When storing data in ByteBuffer, you need to consider the encoding mode of the character set. Reading and displaying ByteBuffer data involves decoding the character set.
Java.nio.charset provides a set of solutions for encoding and decoding.
Taking our most common http request as an example, the request must be correctly encoded when it is requested. The response must be decoded correctly when it is received.
The following code makes a request to baidu and gets the result for display. The example demonstrates the use of charset.
Example 2BaiduReader.java
Package nio.readpage;import java.nio.ByteBuffer;import java.nio.channels.SocketChannel;import java.nio.charset.Charset;import java.net.InetSocketAddress;import java.io.IOException;public class BaiduReader {private Charset charset = Charset.forName ("GBK"); / / create the GBK character set private SocketChannel channel; public void readHTMLContent () {try {InetSocketAddress socketAddress = new InetSocketAddress ("www.baidu.com", 80) / / step1: open the connection channel = SocketChannel.open (socketAddress); / / step2: send the request, encoding channel.write using GBK (charset.encode ("GET" + "/ HTTP/1.1" + "\ r\ n\ r\ n")) / / step3: read data ByteBuffer buffer = ByteBuffer.allocate (1024); / / create a 1024-byte buffer while (channel.read (buffer)! =-1) {buffer.flip (); / / the flip method is called before the read buffer byte operation. System.out.println (charset.decode (buffer)); / / use the Charset.decode method to convert bytes to the string buffer.clear (); / / clear the buffer}} catch (IOException e) {System.err.println (e.toString ()) } finally {if (channel! = null) {try {channel.close () } catch (IOException e) {}} public static void main (String [] args) {new BaiduReader () .readHTMLContent ();}}
6. Non-blocking IO
Non-blocking IO will be understood from several aspects: what is blocking, what is non-blocking, the principle of non-blocking and asynchronous core API.
What is blocking?
A common network IO communication process is as follows:
The network communication process to understand what blocking is:
In the above process, if the connection has not arrived, then the accept will block, the program will have to hang at this point, and the CPU will execute other threads instead.
In the above process, if the data is not ready, read will also block.
The characteristic of blocking network IO: multi-thread handles multiple connections. Each thread has its own stack space and takes up some CPU time. Each thread is blocked when it is externally ready. The result of blocking is a large number of process context switches. And most process context switching may be meaningless. For example, suppose a thread listens on a port and only a few requests come in a day, but the cpu has to keep trying to switch contexts for that thread, and most of the switching ends in blocking.
What is non-blocking?
Here's a metaphor:
On a bus from A to B, people may get off at many points on the road. Drivers do not know at which points who will get off, for those who need to get off, how to deal with better?
1. In the process, the driver regularly asks each passenger whether he has arrived at his destination. If someone says so, the driver stops and the passenger gets off. (similar to blocking)
two。 Everyone tells the conductor his destination, then goes to bed, the driver only interacts with the conductor, and at a certain point the conductor tells the passengers to get off. (similar to non-blocking)
Obviously, everyone who wants to get to a destination can be thought of as a thread, and the driver can be thought of as CPU. In blocking, each thread needs to constantly poll and switch contexts to achieve the result of finding the destination. In the non-blocking mode, each passenger (thread) is sleeping (dormant) and wakes up only when the real external environment is ready, so the wake-up call is certainly not blocked.
Non-blocking principle
Switch the whole process into small tasks, which can be done through collaboration between tasks.
A dedicated thread handles all IO events and is responsible for distributing them.
Event-driven mechanism: triggers events when they arrive, rather than monitoring events synchronously.
Thread communication: threads communicate with each other through wait,notify and other means. Make sure that every context switch makes sense. Reduce unnecessary process switching.
The following is the structure of asynchronous IO:
Reactor is the above metaphorical role of the conductor. The processing flow of each thread is probably to read data, decode, calculate and process, encode, and send responses.
At this point, I believe you have a deeper understanding of "how to use Buffer and Chanel in Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue 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.
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.