• 初识Channel


    java.nio.channels 中的接口和类。

     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.
      channel就是一个打开的连接向一个实体的连接,这些实体包括硬件设备,文件,网络socket或者程序组件,一个能够执行一个或多个I/O操作。
    Channel接口:
      channel只有两个状态open close
      所以在这个接口中只有两个方法
          public boolean isOpen();
       public boolean isClose();
    ReadableByteChannel 接口:
      实现了Channel接口
      还有     public int read(ByteBuffer dst) throws IOException; 返回从ByteBuffer中读取的字节数量。
    WritableByteChannel接口:
      public int write(ByteBuffer src) throws IOException; 写入src中,返回写入的字节数量。
    ScatteringByteChannel 接口
      继承了 ReadableByteChannel 并提供了同时往几个 ByteBuffer 中写数据的能力。
         public long read(ByteBuffer[] dsts, int offset, int length)  throws IOException;
          public long read(ByteBuffer[] dsts) throws IOException;
     GatheringByteChannel 接口
      继承了 WritableByteChannel 并提供了同时从几个 ByteBuffer 中读数据的能力。
        public long write(ByteBuffer[] srcs, int offset, int length)       throws IOException
      public long write(ByteBuffer[] srcs) throws IOException;
    InterruptibleChannel 
      A channel that can be asynchronously closed and interrupted.
      用来表现一个可以被异步关闭的 Channel 。这表现在两方面:
    1.    当一个 InterruptibleChannel 的 close() 方法被调用时,其它 block 在这个 InterruptibleChannel 的 IO 操作上的线程会接收到一个                  AsynchronousCloseException 。
    2.    当一个线程 block 在 InterruptibleChannel 的 IO 操作上时,另一个线程调用该线程的 interrupt() 方法会导致 channel 被关闭,该线程收到一个 ClosedByInterruptException ,同时线程的 interrupt 状态会被设置。
      
      介绍完了其中的接口,接下来介绍一下其中的类:


      
      NIO--非阻塞 IO 允许应用程序同时监控多个 channel 以提高性能,这一功能是通过 Selector , SelectableChannel 和 SelectionKey 这 3 个类来实现的。

      他们之间的关系是这样的(个人理解,欢迎拍砖) ---只是画出了其中的一部分 ,从其他数据源来获取数据。

      
    SelectionKey
       A selection key is created each time a channel is registered with a selector.  A key remains valid until it is canceled by call cancel method. 当使用selector 注册一个channel的时候就会创建一个SelectionKey。
      Cancelling a key does not immediately remove it from its selector,It is instead added to the selector`s cancelled-key set.

      在SelectionKey 中维护者两个keySet interest set  和 ready set

         The <i>interest set</i> determines which operation categories will   be tested for readiness the next time one of the selector's       selection  methods is invoked. The interest set is initialized with the value given   when the key is created; it may later be        changed via the  interestOps(int)  method.

        The <i>ready set</i> identifies the operation categories for which  the key's channel has been detected to be ready by the key's       selector.  The ready set is initialized to zero when the key is created; it may later  be updated by the selector during a         selection operation, but it cannot be updated directly.

        Selection keys are safe for use by multiple concurrent threads.  Selection keys是线程安全的,其中的读写操作都使用同步方法      加以控制。 

      构造方法;  他不能通过传统的构造函数的方式来获取构造实例。通常使用Selector中的方法来获取所有的可用的SelectionKey 集合,然后再在其中根据他们各自的状态选择自己真正需要的SelectionKey.

         public abstract Set<SelectionKey> selectedKeys(); 或者     public abstract Set<SelectionKey> keys();

      这其中有几个非常重要的判断方法

            public abstract boolean isValid(); // Tells whether or not this key is valid. 判断是否有效

          public final boolean isReadable();//Tests whether this key's channel is ready for reading. 是否可以读取

         public final boolean isWritable() //   Tests whether this key's channel is ready for writing.

      还有几个重要的常量,经常用来对SelectionKey的筛选,选择自己所需。

        OP_READ,OP_WRITE,OP_CONNECT,OP_ACCEPT。后面两个经常用来SocketChannel 和ServerSocketChannel

      另外还可以从中获取selector 和channel

    Selector  -- 称之为选择器

      A selector may be created by invoking the  open method of this class.e.g. selector = Selector.open();

      如果想要从channel中获取数据,首先要做的就是将channel 注册到selector上面。

    常用方法:

          public abstract boolean isOpen();  Tells whether or not this selector is open

        public abstract int select(long timeout);Selects a set of keys whose corresponding channels are ready for I/O operations;返回当前可用的selecionKeys数量,timeout 表示可以等待的时长。

       public abstract int select() throws IOException; 同上,无需等待直接返回。

    SelectableChannel

    这个抽象类是所有支持非阻塞 IO 操作的 channel (如 DatagramChannel 、 SocketChannel )的父类。 SelectableChannel 可以注册到一个或多个 Selector 上以进行非阻塞 IO 操作。

    SelectableChannel 可以是 blocking 和 non-blocking 模式(所有 channel 创建的时候都是 blocking 模式),只有 non-blocking 的 SelectableChannel 才可以参与非阻塞 IO 操作。

      SelectableChannel configureBlocking(boolean block)    设置 blocking 模式。false,为非阻塞。

            boolean isBlocking()      返回 blocking 模式。

    通过 register() 方法, SelectableChannel 可以注册到 Selector 上。

      接下来会介绍SelectableChannel的一些子类,欢迎拍砖。

  • 相关阅读:
    Golang 开发环境安装和配置
    多测师肖老师__接口测试之cms搭建(27.1)
    多测师肖老师__接口测试之fn+f12查看接口(27.2)
    认识wpf中binding类型
    认识wpf的事件
    Jquery对象与Dom对象
    AS3编程中的两种常用事件
    SQL Server 系统表简介
    Winform中ComcoBox控件设置选定项
    wpf开篇入门
  • 原文地址:https://www.cnblogs.com/plxx/p/4579107.html
Copyright © 2020-2023  润新知