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
Ïà¹ØÎĵµ£º
< type="text/javascript">
document.body.oncopy = function() {
if (window.clipboardData) {
setTimeout(function() {
......
(ת£ºhttp://blog.csdn.net/alex197963/archive/2008/03/26/2219994.aspx)
1.»ù±¾¸ÅÄîµÄÀí½â
¡¡¡¡¾ø¶Ô·¾¶:¾ø¶Ô·¾¶¾ÍÊÇÄãµÄÖ÷Ò³ÉϵÄÎļþ»òĿ¼ÔÚÓ²ÅÌÉÏÕæÕýµÄ·¾¶,(URLºÍÎïÀí·¾¶)ÀýÈç:
C:xyz est.txt ´ú±íÁËtest.txtÎļþµÄ¾ø¶Ô·¾¶.http://www.sun.com/index.htmÒ²´ú±íÁËÒ»¸ö
URL¾ø¶Ô·¾¶.
¡¡¡¡Ïà¶Ô·¾¶:Ïà¶ÔÓëij¸ö» ......
JAVA¿ÉÒÔ˵ÊÇ´ÓC++·¢Õ¹ÆðÀ´µÄÍêÈ«ÃæÏò¶ÔÏóµÄ¿ª·¢ÓïÑÔ,Á½ÕßµÄʵÖÊÇø±ðÊÇÀûÒæ·ÖÊô²»Í¬µÄ¹«Ë¾:SUNºÍ΢Èí.¶Ô´Ë΢Èí·¢²¼ÁË.NETÓëÖ®¿¹ºâ.
´Ó×îеÄVisual Studio .NET 2005¿´,.NETÖ÷ÒªÊÇ4ÖÖÓïÑÔ:VB.NET, C#, C++.NET, J#.
VB.NET¿ÉÒÔ˵ÊÇVB6µÄÉý¼¶,µ«ÎÒÈÏΪVB.NETºÍC#ÒѾÊÇÍêȫȫеÄÓïÑÔÁË,Á½ÕßµÄÄ¿µÄ¶¼ÊÇÒª´ò°ÜJAVAÕâ¸ö¾ºÕ ......
¸ø Java SE ×¢Èë½Å±¾ÓïÑԵĻîÁ¦
Mustang µÄ½Å±¾ÓïÑÔÐÂÌØÐÔ
ÎĵµÑ¡Ïî
½«´ËÒ³×÷Ϊµç×ÓÓʼþ·¢ËÍ
½«´ËÒ³×÷Ϊµç×ÓÓʼþ·¢ËÍ
¼¶±ð£º Öм¶
Îâ 玥ò« (wuyuehao@cn.ibm.com), Èí¼þ¹¤³Ìʦ, IBM
2006 Äê 10 ÔÂ 30 ÈÕ
ÔÚ¼´½«·¢²¼µÄ Java SE6£¨Mustang£©ÖУ¬Ôö¼ÓÁ˶Խű¾ÓïÑÔµÄÖ§³Ö¡£Í¨¹ý¶Ô½Å± ......
·þÎñÆ÷¶Ë£º
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ThreadSocketServer extends Thread {
private Socket client;
&n ......