java multiplexed I/O

上篇文章说的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。
[i]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.[/i]

The Selector, SelectableChannel, and SelectionKey Classes

[b]Selector[/b]
[i]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.[/i]
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();
}

[b]SelectableChannel[/b]
[i]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.[/i]


[b]SelectionKey[/b]
[i]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.[/i]
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类的使用。
[color=red][b]注:以上定义描述来自Oreilly java NIO[/b][/color]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值