码迷,mamicode.com
首页 > 编程语言 > 详细

java NIO 模型(一)

时间:2014-08-31 18:38:51      阅读:308      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   java   strong   ar   for   

1. 阻塞I/O通信模型 

bubuko.com,布布扣

1.性能:一连接一线程模型导致服务端的并发接入数和系统吞吐量受到极大限制

2.可靠性:由于IO操作采用同步阻塞模式,当网络拥塞或者逻辑处理缓慢会导致IO线程被挂住,阻塞时间无法预测

3.可维护性:IO线程数无法有效控制、资源无法有效共享(多线程并发问题),系统可维护性差

 
 
2. 异步、非阻塞、基于事件驱动的NIO框架
 
bubuko.com,布布扣
 
 
 
3.selector作用
bubuko.com,布布扣
 
4.三个概念

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

and selection keys.

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

key that 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.

 

java NIO 模型(一)

标签:style   blog   http   color   io   java   strong   ar   for   

原文地址:http://www.cnblogs.com/yuyutianxia/p/3947972.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!