java中IO演变史

涉及到io主要涉及以下两个方面:
1)同步和异步
2)阻塞和非阻塞

BIO

BIO ,全称 Block-IO ,是一种阻塞 + 同步的通信模式。基于流(Stream)的模式。

每连接对应一个线程:线程数与客户端连接数成1:1的关系,当并发大量来时,会导致系统线程被大量占用,性能急剧下降。

NIO

NIO,Non-Block IO ,从 Java 1.4 版本开始引入的非阻塞 IO ,是一种非阻塞 + 同步的通信模式,也是 I/O 多路复用的基础,基于缓冲区(Buffer)的模式。

NIO主要有三大核心

通过下图简单展示三大核心之间的关系:

Channel简介

jdk中定义了一个java.nio.channels.channel接口,主要提供了开启连接关闭连接的方法。

package java.nio.channels;

import java.io.IOException;
import java.io.Closeable;


/**
 * A nexus for I/O operations.
 *
 * <p> A channel represents an open connection to an entity such as a hardware
 * device, a file, a network socket, or a program component that is capable of
 * performing one or more distinct I/O operations, for example reading or
 * writing.
 *
 * <p> A channel is either open or closed.  A channel is open upon creation,
 * and once closed it remains closed.  Once a channel is closed, any attempt to
 * invoke an I/O operation upon it will cause a {@link ClosedChannelException}
 * to be thrown.  Whether or not a channel is open may be tested by invoking
 * its {@link #isOpen isOpen} method.
 *
 * <p> Channels are, in general, intended to be safe for multithreaded access
 * as described in the specifications of the interfaces and classes that extend
 * and implement this interface.
 *
 *
 * @author Mark Reinhold
 * @author JSR-51 Expert Group
 * @since 1.4
 */

public interface Channel extends Closeable {

    /**
     * Tells whether or not this channel is open.
     *
     * @return <tt>true</tt> if, and only if, this channel is open
     */
    public boolean isOpen();

    /**
     * Closes this channel.
     *
     * <p> After a channel is closed, any further attempt to invoke I/O
     * operations upon it will cause a {@link ClosedChannelException} to be
     * thrown.
     *
     * <p> If this channel is already closed then invoking this method has no
     * effect.
     *
     * <p> This method may be invoked at any time.  If some other thread has
     * already invoked it, however, then another invocation will block until
     * the first invocation is complete, after which it will return without
     * effect. </p>
     *
     * @throws  IOException  If an I/O error occurs
     */
    public void close() throws IOException;

在jdk中对于channel接口提供了很多实现类,最主要的是以下四个:
1)SocketChannel :一个客户端用来发起 TCP 的 Channel 。
2)ServerSocketChannel :一个服务端用来监听新进来的连接的 TCP 的 Channel 。对于每一个新进来的连接,都会创建一个对应的 SocketChannel 。
3)DatagramChannel :通过 UDP 读写数据。
4)FileChannel :从文件中,读写数据。

Buffer简介

jdk提供java.nio.buffer抽象类,其属性和构造如下代码所示:

package java.nio;

import java.util.Spliterator;

public abstract class Buffer {

    /**
     * The characteristics of Spliterators that traverse and split elements
     * maintained in Buffers.
     */
    static final int SPLITERATOR_CHARACTERISTICS =
        Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.ORDERED;

    // Invariants: mark <= position <= limit <= capacity
    private int mark = -1;
    private int position = 0;
    private int limit;
    private int capacity;

    // Used only by direct buffers
    // NOTE: hoisted here for speed in JNI GetDirectBufferAddress
    long address;

    // Creates a new buffer with the given mark, position, limit, and capacity,
    // after checking invariants.
    //
    Buffer(int mark, int pos, int lim, int cap) {       // package-private
        if (cap < 0)
            throw new IllegalArgumentException("Negative capacity: " + cap);
        this.capacity = cap;
        limit(lim);
        position(pos);
        if (mark >= 0) {
            if (mark > pos)
                throw new IllegalArgumentException("mark > position: ("
                                                   + mark + " > " + pos + ")");
            this.mark = mark;
        }
    }
}

如上代码所示,Buffer有四个属性,他们在Buffer读和写的情况下具有不同的含义

mark:记录当前读或写的位置
position:下一个位置
limit:范围
capacity:Buffer的容量,创建时候指定,不能修改。

写模式下:

读模式下:

主要方法:
每个buffer的实现类都实现了以下主要方法,下面以ByteBuffer的源码举例:
1)创建 Buffer:

/**
 * 创建并指定大小
 **/
public static ByteBuffer allocate(int capacity) {
        if (capacity < 0)
            throw new IllegalArgumentException();
        return new HeapByteBuffer(capacity, capacity);
    }

/**
 * 数组转成Buffer对象
 **/
public static ByteBuffer wrap(byte[] array, int offset, int length)
    {
        try {
            return new HeapByteBuffer(array, offset, length);
        } catch (IllegalArgumentException x) {
            throw new IndexOutOfBoundsException();
        }
    }

2)写入:

// 写入 byte
public abstract ByteBuffer put(byte b); 
public abstract ByteBuffer put(int index, byte b);
// 写入 byte 数组
public final ByteBuffer put(byte[] src) { ... }
public ByteBuffer put(byte[] src, int offset, int length) {...}

从channel将数据写入buffer,该方法返回数据大小:

int num = channel.read(buffer);

3)读取:

// 读取 byte
public abstract byte get();
public abstract byte get(int index);
// 读取 byte 数组
public ByteBuffer get(byte[] dst, int offset, int length) {...}
public ByteBuffer get(byte[] dst) {...}

从buffer中的数据写入到buffer中,该方法返回数据大小:

int num = channel.write(buffer);
Selector简介

Selector被称为多路复用器,用于轮询NIO的channel是否处于可读或者可写的状态,其位于jdk的java.nio.channels.Selector。

轮询步骤:
1)每个channel需要注册到selector上。
2)selector轮询每个channel,当有channel发生读写操作,这个channel处于就绪状态,会被轮询到,等到就绪状态的channel集合,进行后续的IO操作。

代码举例:

//创建selector
Selector selector = Selector.open();
//注册channel
channel.configureBlocking(false); // 必须是非阻塞
/*
第二个参数有以下四种类型:
Connect :连接完成事件( TCP 连接 ),仅适用于客户端,对应 SelectionKey.OP_CONNECT 
Accept:接受新连接事件,仅适用于服务端,对应 SelectionKey.OP_ACCEPT 。
Read :读事件,适用于两端,对应SelectionKey.OP_READ ,表示 Buffer 可读。
Write :写时间,适用于两端,对应SelectionKey.OP_WRITE ,表示 Buffer 可写。
*/
SelectionKey key = channel.register(selector, SelectionKey.OP_READ);

AIO简介

参考https://blog.csdn.net/qq_19642249/article/details/108383643

AIO是java中IO模型的一种,作为NIO的改进和增强随JDK1.7版本更新被集成在JDK的nio包中,因此AIO也被称作是NIO2.0。AIO提供了从建立连接到读、写的全异步操作。AIO可用于异步的文件读写和网络通信。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章