java nio模型_java NIO 模型(一)

Multiplexed, non-blocking I/O, which is much more scalable than thread-oriented, blocking I/O, is provided by selectors, selectable channels,

and selection keys.

A selector is a multiplexor of selectable channels, which in turn are a special type of channel that can be put into non-blocking mode.

To perform multiplexed I/O operations, one or more selectable channels are first created, put into non-blocking mode, and registered with

a selector. Registering a channel specifies the set of I/O operations that will be tested for readiness by the selector, and returns a selection

keythat represents the registration.

Once some channels have been registered with a selector, a selection operation can be performed in order to discover which channels, if

any, have become ready to perform one or more of the operations in which interest was previously declared. If a channel is ready then the

key returned when it was registered will be added to the selector's selected-key set. The key set, and the keys within it, can be examined

in order to determine the operations for which each channel is ready. From each key one can retrieve the corresponding channel in order to

perform whatever I/O operations are required.

That a selection key indicates that its channel is ready for some operation is a hint, but not a guarantee, that such an operation can be

performed by a thread without causing the thread to block. It is imperative that code that performs multiplexed I/O be written so as to

ignore these hints when they prove to be incorrect.

5.线程安全

Selection keys are safe for use by multiple concurrent threads. The operations of reading and writing the interest set will, in general, be

synchronized with certain operations of the selector. Exactly how this synchronization is performed is implementation-dependent:

In a naive implementation, reading or writing the interest set may block indefinitely if a selection operation is already in progress;

in a high-performance implementation, reading or writing the interest set may block briefly, if at all.

In any case, a selection operation will always use the interest-set value that was current at the moment that the operation began.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值