<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    the journey is the reward...

    常用鏈接

    統計

    最新評論

    java nio在多線程環境下的惡夢之終結

    有人說java nio在多線程環境下編程簡直就是個惡夢,其實你如果能把握住java nio API的要領,你就可以將之駕馭. 

    0. 一個 channal 對應一個SelectionKey in the same selector.
    e.g:
    SelectionKey sk=sc.register(selector, SelectionKey.OP_READ, handler);
    sk==sc.register(selector, SelectionKey.OP_WRITE, handler) true?
    selector.select() 每次返回的對同一channal的sk是否相同? 

    1.channel.register(...) may block if invoked concurrently with another registration[another.register(...)] or selection operation[selector.select(...)] involving *****the same selector*****.
    這個是register方法jdk src上的原文,
    e.g:
    如果一個selection thread已經在select方法上等待ing,那么這個時候如果有另一條線程調用channal.register方法的話,那么它將被blocking.

    2.selectionKey.cancel() : The key will be removed from all of the selector's key sets during *****the next selection operation[selector.select(...)]*****.
    may block briefly if invoked concurrently with a cancellation[cancel()] or selection operation[select(...)] involving ***the same selector***.
    這個也是cancel方法jdk src上的原文,
    e.g:
    你先將一個selectionKey.cancel(),然后隨即再channel.register to the same selector,
    在cancel和register之間,如果沒有線程(包括當前線程)進行select操作的話,
    那么 throws java.nio.channels.CancelledKeyException.
    所以 cancel-->select-->re-register.  

    3.if don't remove the current selectedKey from selector.selectedKeys()[Set] 將導致 selector.select(...) not block [may be cpu 100%,specially when client cut the current channel(connection)].
    e.g:
    Iterator<SelectionKey> it=selector.selectedKeys().iterator();
    ...for/while it.hasNext()...
    it.remove();<------*****must do it. or Keys' Set.clear() finally; 

    if remove the current selectedKey from selector.selectedKeys()[Set] but don't sk.interestOps(sk.interestOps()& (~sk.readyOps()));將導致 selector.select(...) not block [select() not block several times, or excepted exception] 

    4.op_write should not be registered to the selector.   [may be cpu100%] 

    5. if involving  wakeup() before select() [wakeup called several times >=1],the next select() not block [not block just once]. 

    盡管以前有些人分析了nio的wakeup性能及not block in linux的bug,但是java nio依然是高效的,那些c/c++的牛人們去看看jre/bin目錄下的nio.dll/nio.so吧,java nio是基于select模型(這個是c/c++中常用網絡編程模型之一)的.

    基于java nio的服務器:mina,girzzly[glassfish],jetty(基于girzzly),tomcat6[可以配置Http11NioProtocol]...

    其中從本人對girzzly,tomcat6的源碼分析來看,它們都還沒有真正發揮出nio異步處理請求的優點,它們的讀寫還都是blocking的雖然使用了selectorPool,此外tomcat6要剝離出socket通信還要花費一定的功夫.而mina卻是符其實,還有bug哦.

    posted on 2010-03-02 10:12 adapterofcoms 閱讀(1863) 評論(1)  編輯  收藏 所屬分類: java bbs

    評論

    # re: java nio在多線程環境下的惡夢之終結 2010-05-29 15:54 冰河快狼

    非常不錯的總結,學習并轉載了  回復  更多評論   

    主站蜘蛛池模板: 香蕉免费在线视频| 四虎免费久久影院| 亚洲精品国产高清在线观看| 亚洲精品国产电影| 无码日韩精品一区二区三区免费| 亚洲一级视频在线观看| 波多野结衣久久高清免费| 国产精品免费观看调教网| 亚洲欧美日韩综合俺去了| 亚洲精品无码成人AAA片| 国内大片在线免费看| 大地资源网高清在线观看免费| 亚洲欧洲日韩国产一区二区三区| 亚洲一区二区精品视频| 久久笫一福利免费导航| 国产免费人成视频在线播放播| 亚洲va在线va天堂成人| 亚洲欧洲国产精品香蕉网| 国产在线19禁免费观看国产| 最近中文字幕mv免费高清视频8| 久久久久久a亚洲欧洲aⅴ| 最近免费中文字幕大全| 久久这里只精品国产免费10| 国产成人高清亚洲一区久久| 亚洲国产品综合人成综合网站| 三上悠亚亚洲一区高清| 日本高清免费网站| h在线观看视频免费网站| a级毛片高清免费视频| 福利片免费一区二区三区| 亚洲av永久综合在线观看尤物| 亚洲人成精品久久久久| 亚洲AV中文无码乱人伦在线视色| 大地资源二在线观看免费高清 | eeuss影院免费直达入口| 亚洲色大成网站www尤物| 亚洲福利电影一区二区?| 国产l精品国产亚洲区在线观看| 亚洲 小说区 图片区 都市| 亚洲中文无码永久免费| 97在线视频免费播放|