`
rain2005
  • 浏览: 14477 次
  • 来自: 武汉->大连->武汉
文章分类
社区版块
存档分类
最新评论

java multiplexed I/O

    博客分类:
  • java
阅读更多
上篇文章说的java的异步IO,熟悉unix IO调度方式的朋友应该清楚unix操作系统在非阻塞的调用形式结合selectors(选择器)select系统调用(感兴趣的朋友可以看看steven unix高级系统编程或者unix网络编程相当经典)提供了IO多路复用调用,这也是高并发服务器的IO调用方式(现在最新linux内核也提供了epoll形式的非阻塞调用这个留在以后讨论)。

以下就是nonblocking的socket
   SocketChannel sc = SocketChannel.open();
   sc.configureBlocking (false);// nonblocking
   ...
   if ( ! sc.isBlocking()) {
   doSomething (cs);
}


下面介绍选择器
我们上面提到到的SocketChannel继承了SelectableChannel接口,也就是说SocketChannel是可选择的,selectors可以理解为它提供询问操作系统内核Socket是否准备好进行I/O操作的能力,如果已经准备好他就返回准备好的SocketChannel列表。当然这要求有大量的SocketChannel已经与selectors关联上,也就是SocketChannel已经注册到selectors。
For example, a SocketChannel object
could be asked if it has any bytes ready to read, or we may want to know if a
ServerSocketChannel has any incoming connections ready to accept.


The Selector, SelectableChannel, and SelectionKey Classes

Selector
The Selector class manages information about a set of registered channels and
their readiness states. Channels are registered with selectors, and a selector can be
asked to update the readiness states of the channels currently registered with it.

public abstract class Selector
{
    public static Selector open() throws IOException
    public abstract boolean isOpen();
    public abstract void close() throws IOException;
    public abstract SelectionProvider provider();
    public abstract int select() throws IOException;
    public abstract int select (long timeout) throws IOException;
    public abstract int selectNow() throws IOException;
    public abstract void wakeup();
    public abstract Set keys();
    public abstract Set selectedKeys();
}

SelectableChannel
All the socket channel classes are selectable,
as well as the channels obtained from a Pipe object. SelectableChannel objects
can be registered with Selector objects, along with an indication of which
operations on that channel are of interest for that selector. A channel can be
registered with multiple selectors, but only once per selector.



SelectionKey
A SelectionKey encapsulates the registration relationship between a specific
channel and a specific selector. A SelectionKey object is returned from
SelectableChannel.register() and serves as a token representing the registration.
SelectionKey objects contain two bit sets (encoded as integers) indicating which
channel operations the registrant has an interest in and which operations the
channel is ready to perform.

public abstract class SelectionKey
{
    public static final int OP_READ
    public static final int OP_WRITE
    public static final int OP_CONNECT
    public static final int OP_ACCEPT
    public abstract SelectableChannel channel();
    public abstract Selector selector();
    public abstract void cancel();
    public abstract boolean isValid();
    public abstract int interestOps();
    public abstract void interestOps (int ops);
    public abstract int readyOps();
    public final boolean isReadable()
    public final boolean isWritable()
    public final boolean isConnectable()
    public final boolean isAcceptable()
    public final Object attach (Object ob)
    public final Object attachment()
}


这篇将了几个关于多路复用IO类的概念,下篇文章从实例讲解多路复用IO类的使用。
注:以上定义描述来自Oreilly java NIO
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics