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

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

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

    Dict.CN 在線詞典, 英語學習, 在線翻譯

    都市淘沙者

    荔枝FM Everyone can be host

    統計

    留言簿(23)

    積分與排名

    優秀學習網站

    友情連接

    閱讀排行榜

    評論排行榜

    java nio在多線程環境需注意的問題

    原帖:http://www.tkk7.com/adapterofcoms/archive/2010/03/02/314242.html

    有人說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-05-29 15:53 都市淘沙者 閱讀(2287) 評論(0)  編輯  收藏 所屬分類: 多線程并發編程

    主站蜘蛛池模板: 久久爰www免费人成| 国产高清视频免费在线观看| 色欲国产麻豆一精品一AV一免费 | 青柠影视在线观看免费| 亚洲色偷偷狠狠综合网| 一级特黄录像视频免费| 国产精品V亚洲精品V日韩精品| 青青草97国产精品免费观看| 亚洲第一成人影院| 国产成人精品免费视频大全| 337p日本欧洲亚洲大胆裸体艺术| 久久久久女教师免费一区| 精品亚洲一区二区| 精品久久8x国产免费观看| 亚洲国产成人久久综合一区| 亚洲中文无码永久免费| 亚洲AV综合色区无码一二三区| 午夜国产羞羞视频免费网站| 一级白嫩美女毛片免费| 亚洲av无码国产精品色午夜字幕| 国产精品爱啪在线线免费观看| 亚洲精品人成网线在线播放va | 一二三四影视在线看片免费 | 中文亚洲AV片不卡在线观看| 国产午夜无码精品免费看 | 美女被羞羞网站免费下载| 亚洲宅男天堂在线观看无病毒| 嫩草在线视频www免费观看| 亚洲制服丝袜精品久久| 日韩一级在线播放免费观看| 久久99精品免费一区二区| 亚洲精品自产拍在线观看动漫| 毛片基地免费观看| 国产免费区在线观看十分钟 | 亚洲免费黄色网址| 精品久久久久久亚洲中文字幕 | 亚洲成亚洲乱码一二三四区软件| 18禁美女黄网站色大片免费观看| 亚洲成av人片天堂网无码】| 日本红怡院亚洲红怡院最新| 久久WWW免费人成人片|