美文网首页Nio
NIO之一--Overview

NIO之一--Overview

作者: AlanKim | 来源:发表于2019-02-23 00:45 被阅读6次

    基本上所有内容都来源于 http://tutorials.jenkov.com/java-nio/index.html

    Java NIO Overview

    Java NIO consist of the following core components:

    • Channels
    • Buffers
    • Selectors

    Java NIO has more classes and components than these, but the Channel, Buffer and Selector forms the core of the API, in my opinion. The rest of the components, like Pipe and FileLock are merely utility classes to be used in conjunction with the three core components. Therefore, I'll focus on these three components in this NIO overview. The other components are explained in their own texts elsewhere in this tutorial. See the menu at the top corner of this page.

    JavaNIO中的核心概念,就是Channels、Buffers、Selectors,其他的都是围绕着这三个概念的工具。

    Channels and Buffers

    Typically, all IO in NIO starts with a Channel. A Channel is a bit like a stream. From the Channel data can be read into a Buffer. Data can also be written from a Buffer into a Channel. Here is an illustration of that:

    注意这里:From channel READ data into a Buffer.

    ​ WRITE from a Buffer into a channel.

    overview-channels-buffers.png

    Java NIO: Channels read data into Buffers, and Buffers write data into Channels

    There are several Channel and Buffer types. Here is a list of the primary Channel implementations in Java NIO:

    • FileChannel
    • DatagramChannel
    • SocketChannel
    • ServerSocketChannel

    As you can see, these channels cover UDP + TCP network IO, and file IO.

    There are a few interesting interfaces accompanying these classes too, but I'll keep them out of this Java NIO overview for simplicity's sake. They'll be explained where relevant, in other texts of this Java NIO tutorial.

    Here is a list of the core Buffer implementations in Java NIO:

    • ByteBuffer
    • CharBuffer
    • DoubleBuffer
    • FloatBuffer
    • IntBuffer
    • LongBuffer
    • ShortBuffer

    These Buffer's cover the basic data types that you can send via IO: byte, short, int, long, float, double and characters.

    Java NIO also has a MappedByteBuffer which is used in conjunction with memory mapped files. I'll leave this Buffer out of this overview though.

    上面列举了一些常见的 Channel类型和Buffer类型。不过注意MappedByteBuffer,这个是基于mmap协议的,将磁盘文件映射到内存中操作,提高处理性能。FQueue就是这个的产物。

    Selectors

    A Selector allows a single thread to handle multiple Channel's. This is handy if your application has many connections (Channels) open, but only has low traffic on each connection. For instance, in a chat server.

    Selector允许一个线程处理多个Channels。

    Here is an illustration of a thread using a Selector to handle 3 Channel's:

    overview-selectors.png

    Java NIO: A Thread uses a Selector to handle 3 Channel's

    To use a Selector you register the Channel's with it. Then you call it's select() method. This method will block until there is an event ready for one of the registered channels. Once the method returns, the thread can then process these events. Examples of events are incoming connection, data received etc.

    如过需要使用Selector,要将Channel注册到Selector中。

    之后可以调用Selector的select()方法,这个方法会一直阻塞,直到某个注册的通道有事件就绪。

    一旦这个方法返回,线程就可以处理这些事件,事件的例子有如新连接进来,数据接收等。

    相关文章

      网友评论

        本文标题:NIO之一--Overview

        本文链接:https://www.haomeiwen.com/subject/uanoeqtx.html