Calvin's Tech Space

          成于堅(jiān)忍,毀于浮躁

             :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理

          參見
          The Rox Java NIO Tutorial

          一些principles
          1.Use a single selecting thread
          雖然selector本身是線程安全的,但是key集合卻不是線程安全的,使用多線程容易導(dǎo)致死鎖。

          2.Use a single selecting thread
          NIO底層實(shí)現(xiàn)在不同的平臺(tái)上是不一樣的,如果你編寫的程序可能要在不同的平臺(tái)上運(yùn)行,那么必須遵循這一原則。
          更改行為包括:修改一個(gè)select key感興趣的ops,向這個(gè)selector注冊(cè)新的channel或者從這個(gè)selector取消一個(gè)channel等。

          3.Set OP_WRITE only when you have data ready
          A common mistake is to enable OP_WRITE on a selection key and leave it set. This results in the selecting thread spinning because 99% of the time a socket channel is ready for writing. In fact the only times it's not going to be ready for writing is during connection establishment or if the local OS socket buffer is full. The correct way to do this is to enable OP_WRITE only when you have data ready to be written on that socket channel. And don't forget to do it from within the selecting thread.

          4.Alternate between OP_READ and OP_WRITE



          posted on 2009-12-27 13:55 calvin 閱讀(421) 評(píng)論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 小金县| 靖西县| 定边县| 故城县| 达尔| 乌拉特中旗| 邹平县| 炉霍县| 界首市| 阳山县| 宣汉县| 合川市| 昌都县| 安顺市| 建水县| 通江县| 揭西县| 德兴市| 镇江市| 林口县| 民和| 贵港市| 原阳县| 贺州市| 遂昌县| 逊克县| 民勤县| 辽宁省| 鞍山市| 万山特区| 余姚市| 微博| 桦甸市| 五莲县| 兴宁市| 称多县| 洛川县| 巴塘县| 石屏县| 镇安县| 夹江县|