Calvin's Tech Space

          成于堅忍,毀于浮躁

             :: 首頁 :: 聯系 :: 聚合  :: 管理

          參見
          The Rox Java NIO Tutorial

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

          2.Use a single selecting thread
          NIO底層實現在不同的平臺上是不一樣的,如果你編寫的程序可能要在不同的平臺上運行,那么必須遵循這一原則。
          更改行為包括:修改一個select key感興趣的ops,向這個selector注冊新的channel或者從這個selector取消一個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 閱讀(422) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 绥棱县| 甘孜| 吉首市| 廉江市| 玛纳斯县| 宣威市| 平阳县| 临泉县| 鄄城县| 平昌县| 金塔县| 沙河市| 崇阳县| 搜索| 昌乐县| 淮南市| 江川县| 博白县| 长沙县| 齐齐哈尔市| 泸西县| 荣成市| 赣榆县| 沁水县| 莎车县| 峨眉山市| 阳曲县| 松潘县| 榆树市| 镇康县| 卫辉市| 临漳县| 卢氏县| 化隆| 莱阳市| 库尔勒市| 如东县| 东阳市| 三门峡市| 合水县| 延川县|