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

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
分享到:
评论

相关推荐

    tmio寄存器说明,TC6391BX

    TMIO,全称Time Multiplexed Input/Output,是一种在嵌入式系统中常见的I/O管理技术,主要用于高效地处理系统的输入输出操作。在Toshiba的TC6391BX模块中,TMIO扮演着关键的角色,为系统提供了灵活、高效的I/O控制...

    mc8051_overview.pdf

    MC8051 IP Core Key Features ...- No multiplexed I/O ports - 256 bytes internal RAM - Up to 64 kbyte ROM, up to 64 kbyte RAM - Source code available free of charge under the GNU LGPL license

    Zynq7000的EMIO使用方法

    而EMIO(Extended Multiplexed I/O)是Zynq7000系列处理器中的一个重要特性,它扩展了I/O功能,使得处理器能够通过FPGA部分控制更多的引脚。 EMIO是Zynq处理器中两种I/O引脚类型之一,另一种是MIO(Multiplexed I/O...

    windows下多路复用IO(select,WSAAsyncSelect,WSAEventSelect)

    在Windows操作系统中,多路复用I/O(Multiplexed Input/Output)是一种高效地管理多个网络连接的技术,它允许程序同时处理多个套接字事件,而无需为每个连接创建单独的线程或进程。本篇文章将深入探讨三种在Windows...

    Zynq7000的GPIO原理和控制实现

    2. MIO概念:MIO(Multiplexed I/O)是Zynq7000系列SoC中ARM处理器部分的外围设备接口。MIO端口支持多种不同电压标准和功能,可以进行不同的配置以满足特定的硬件接口需求。 3. 片内外设:片内外设指的是芯片内部的...

    步进电机英文文献‘.docx

    Port 2 is another 8-bit bidirectional I/O port with internal pull-ups. It can be used as an input port without requiring external pull-ups. Port 2 has additional functions as well. It serves as the ...

    易语言Socket编程之Select模型

    Select模型是多路复用I/O(Multiplexed I/O)的一种方法,广泛用于处理多个并发连接,是网络编程中一种常见的技术。 Select模型的核心在于一个叫做`select`的函数,它允许程序监控多个文件描述符(包括Socket),...

    http server&client;(select模型)

    在多路复用I/O(Multiplexed I/O)模型中,`select`是最基础的一种。它允许程序同时监视多个文件描述符(FDs),等待它们就绪以便进行读写操作。`select`函数会阻塞,直到至少有一个FD准备好读、写或出现错误。这种...

    java的nio的使用示例分享

    Java NIO(New Input/Output)是Java 1.4版本引入的一套全新的I/O API,旨在提升Java程序在处理I/O密集型任务时的性能。NIO与传统的 Blocking I/O 不同,它采用非阻塞模式,允许单线程处理多个通道(channels),...

    zcu102试验emio

    EMIO(External Multiplexed I/O)是Xilinx FPGA中的一种特性,允许用户在不使用内部引脚资源的情况下,通过开发板的外部接口与外围设备通信。这种功能在设计时特别有用,尤其是当FPGA内部资源紧张或者需要连接大量...

    最新JDK教程(CHM版)

    ##### 1.3 非阻塞I/O(NON-BLOCKING)和多路I/O(MULTIPLEXED) - **概念**:传统的阻塞式I/O会阻塞线程,而新的非阻塞式I/O可以在没有数据可读或写时返回,从而提高效率。 - **多路I/O**:通过`Selector`和`...

    HT82V38芯片完整手册

    · 16-Bit 30 MSPS A/D converter · 3 .3V digital I/O compatibility · Guaranteed won¢t miss codes · 3-Channel operation up to 30 MSPS · 1~5.85x programmable gain · 2-Channel (even-odd) operation ...

    单片机-外文翻译-英文文献-中英版-at89s51概述.doc

    Port 2 is another 8-bit bi-directional I/O port. Similar to Port 1, it can drive or receive four TTL loads. In its input mode, Port 2 pins have internal pull-ups, eliminating the need for external ...

    unix里面java开发的详细介绍

    - **输入输出(I/O)**:I/O设备的性能也会影响计算机的整体效率,尤其是硬盘驱动器和网络接口卡的速度。 - **总线(BUS)**:总线带宽决定了数据在各组件之间传输的速度,对于提升整体性能至关重要。 总之,Unix作为一...

    xSocket-multiplexed-2.1.5-sources.jar

    xSocket-multiplexed-2.1.5-sources.jarxSocket-multiplexed-2.1.5-sources.jarxSocket-multiplexed-2.1.5-sources.jarxSocket-multiplexed-2.1.5-sources.jarxSocket-multiplexed-2.1.5-sources.jarxSocket-...

    SEC_K3RG2G20BM-MGCH000_DRAM_366F 15x15 for All Customers(LPDDR4).pdf

    • Ultra-low-voltage core and I/O power supplies • Frequency range – 933–10 MHz (data rate range: 1866–20 Mb/s/pin) • 8n prefetch DDR architecture • 8 internal banks for concurrent operation • ...

    Amp-async-chatter-bot.zip

    相反,异步模式允许程序在等待I/O操作(如网络请求或数据库查询)时,可以继续执行其他任务,显著提高了系统性能。AMP框架提供了关键的异步编程工具,包括: 1. **事件循环(Event Loop)**:这是异步编程的核心,它...

    MT29F4G08ABAWP

    Micron NAND Flash devices include an asynchronous data interface for high-performance I/O operations. These devices use a highly multiplexed 8-bit bus (I/Ox) to transfer commands, address, and data. ...

Global site tag (gtag.js) - Google Analytics