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. &nbs
Ïà¹ØÎĵµ£º
¸ø Java SE ×¢Èë½Å±¾ÓïÑԵĻîÁ¦
Mustang µÄ½Å±¾ÓïÑÔÐÂÌØÐÔ
ÎĵµÑ¡Ïî
½«´ËÒ³×÷Ϊµç×ÓÓʼþ·¢ËÍ
½«´ËÒ³×÷Ϊµç×ÓÓʼþ·¢ËÍ
¼¶±ð£º Öм¶
Îâ 玥ò« (wuyuehao@cn.ibm.com), Èí¼þ¹¤³Ìʦ, IBM
2006 Äê 10 ÔÂ 30 ÈÕ
ÔÚ¼´½«·¢²¼µÄ Java SE6£¨Mustang£©ÖУ¬Ôö¼ÓÁ˶Խű¾ÓïÑÔµÄÖ§³Ö¡£Í¨¹ý¶Ô½Å± ......
¡¡¡¡MSSQLÖÐÌṩÁ˸ödatediffº¯ÊýÓÃÀ´¶ÔÁ½¸öʱ¼ä½øÐмõ·¨²Ù×÷£¬µ«ÔÚJavaÖÐȴûÓУ¬Èç¹ûÎÒÃÇÏëÖªµÀÁ½¸öÈÕÆÚ¼äÏà¸ôÁ˶àÉÙÌ죬»òÊÇÏà¸ôÁ˶àÉÙ¸öСʱÔòÒªÊÖ¹¤¼ÆËã¡£ÏÂÃæ´úÂëÄ£·ÂMSSQLµÄdatediffº¯ÊýÌṩÁËʹÓò»Í¬Ê±¼ä¼ä¸ôÀ´¼ÆËãÁ½¸öʱ¼äÏà²îµÄʱ¼ä¼ä¸ôµÄÊýÄ¿£¬±ÈÈçtimeIntervalΪdayÔò·µ»ØÏà²îµÄÌìÊý£¬ÎªmonthÔò·µ»ØÏà²îµÄÔÂÊý ......
JavaÐéÄâ»ú²ÎÊý·ÖΪ»ù±¾²ÎÊýºÍÀ©Õ¹²ÎÊý£¬±¾ÎÄÖ÷ÒªÏêϸ½éÉÜ»ù±¾²ÎÊýÖи÷¸ö²ÎÊýµÄÒâÒåÒÔ¼°ÆäÓ÷¨¡£
Òª²é¿´JavaÐéÄâ»úÓÐÄÄЩ¿ÉÑ¡µÄ²ÎÊýÅäÖ㬿ÉÒÔÖ±½ÓÔËÐÐjavaÃüÁÏÔʾÈçÏÂЧ¹û£º
ϱ߷ֱð½éÉÜÿ¸ö²ÎÊýµÄÓ÷¨£º
1. -client£¬-server
ÕâÁ½¸ö²ÎÊýÓÃÓÚÉèÖÃÐéÄâ»úʹÓúÎÖÖÔËÐÐģʽ£¬clientģʽÆô¶¯±È½Ï¿ì£¬µ«ÔËÐÐʱÐÔÄܺÍÄÚ´ ......
¶¨ÒåÕâ¸ö¹æ·¶µÄÄ¿µÄÊÇÈÃÏîÄ¿ÖÐËùÓеÄÎĵµ¶¼¿´ÆðÀ´ÏñÒ»¸öÈËдµÄ£¬Ôö¼Ó¿É¶ÁÐÔ£¬¼õÉÙÏîÄ¿×éÖÐÒòΪ»»È˶ø´øÀ´µÄËðʧ¡££¨ÕâЩ¹æ·¶²¢²»ÊÇÒ»¶¨Òª¾ø¶Ô×ñÊØ£¬µ«ÊÇÒ»¶¨ÒªÈóÌÐòÓÐÁ¼ºÃµÄ¿É¶ÁÐÔ£©
PackageµÄÃüÃû
PackageµÄÃû×ÖÓ¦¸Ã¶¼ÊÇÓÉÒ»¸öСдµ¥´Ê×é³É¡£
ClassµÄÃüÃû
ClassµÄÃû×Ö±ØÐëÓÉ´óд×Öĸ¿ªÍ·¶øÆäËû×Öĸ¶¼Ð¡Ð´µÄµ¥´Ê×é ......