¡¾×ª¡¿ÔÚJavaÖÐʹÓÃNIO½øÐÐÍøÂç±à³Ì
ÔÚJDKÖУ¬ÓÐÒ»¸ö·Ç³£ÓÐÒâ˼µÄ¿â£ºNIO£¨New I/O£©¡£Õâ¸ö¿âÖÐÓÐ3¸öÖØÒªµÄÀ࣬·Ö±ðÊÇjava.nio.channelsÖÐSelectorºÍChannel£¬ÒÔ¼°java.nioÖеÄBuffer¡£
±¾ÆªÎÄÕÂÎÒÃÇÊ×ÏÈÁ˽âÒ»ÏÂΪʲôÐèÒªNIOÀ´½øÐÐÍøÂç±à³Ì£¬È»ºó¿´¿´Ò»²½Ò»²½À´½²½âÈçºÎÔÚÍøÂç±à³ÌÖÐʹÓÃNIO¡£
ΪʲôÐèÒªNIO
ʹÓÃJava±àд¹ýSocket³ÌÐòµÄͬѧһ¶¨¶¼ÖªµÀSocketºÍSocketServer¡£µ±µ÷ÓÃij¸öµ÷ÓõÄʱºò£¬µ÷Óõĵط½¾Í»á×èÈû£¬µÈ´ýÏìÓ¦¡£ÕâÖÖ·½Ê½¶ÔÓÚС¹æÄ£µÄ³ÌÐò·Ç³£·½±ã£¬µ«ÊǶÔÓÚ´óÐ͵ijÌÐò¾ÍÓеãÁ¦²»´ÓÐÄÁË£¬µ±ÓдóÁ¿µÄÁ¬½ÓµÄʱºò£¬ÎÒÃÇ¿ÉÒÔΪÿһ¸öÁ¬½Ó½¨Á¢Ò»¸öÏß³ÌÀ´²Ù×÷¡£µ«ÊÇÕâÖÖ×ö·¨´øÀ´µÄȱÏÝÒ²ÊÇÏÔ¶øÒ×¼ûµÄ£º
Ó²¼þÄܹ»Ö§³Ö´óÁ¿µÄ²¢·¢¡£
²¢·¢µÄÊýÁ¿Ê¼ÖÕÓÐÒ»¸öÉÏÏÞ¡£
¸÷¸öÏß³ÌÖ®¼äµÄÓÅÏȼ¶²»ºÃ¿ØÖÆ¡£
¸÷¸öClientÖ®¼äµÄ½»»¥Óëͬ²½À§ÄÑ¡£
ÎÒÃÇÒ²¿ÉÒÔʹÓÃÒ»¸öÏß³ÌÀ´´¦ÀíËùÓеÄÇëÇó£¬Ê¹Óò»×èÈûµÄIO£¬ÂÖѯ²éѯËùÓеÄClient¡£ÕâÖÖ×ö·¨Í¬ÑùÒ²ÓÐȱÏÝ£ºÎÞ·¨Ñ¸ËÙÏìÓ¦Client¶Ë£¬Í¬Ê±»áÏûºÄ´óÁ¿ÂÖѯ²éѯµÄʱ¼ä¡£
ËùÒÔ£¬ÎÒÃÇÐèÒªÒ»ÖÖpollµÄģʽÀ´´¦ÀíÕâÖÖÇé¿ö£¬´Ó´óÁ¿µÄÍøÂçÁ¬½ÓÖÐÕÒ³öÀ´ÕæÕýÐèÒª·þÎñµÄClient¡£ÕâÕýÊÇNIOµ®ÉúµÄÔÒò£ºÌṩһÖÖPollµÄģʽ£¬ÔÚËùÓеÄClientÖÐÕÒµ½ÐèÒª·þÎñµÄClient¡£
»Øµ½ÎÒÃǸոÕ˵µ½µÄ3¸ö×î×îÖØÒªµÄClass£ºjava.nio.channelsÖÐSelectorºÍChannel£¬ÒÔ¼°java.nioÖеÄBuffer¡£
Channel´ú±íÒ»¸ö¿ÉÒÔ±»ÓÃÓÚPoll²Ù×÷µÄ¶ÔÏ󣨿ÉÒÔÊÇÎļþÁ÷Ò²¿ÉÒÔʹÍøÂçÁ÷£©£¬ChannelÄܹ»±»×¢²áµ½Ò»¸öSelectorÖС£Í¨¹ýµ÷ÓÃSelectorµÄselect·½·¨¿ÉÒÔ´ÓËùÓеÄChannelÖÐÕÒµ½ÐèÒª·þÎñµÄʵÀý£¨Accept£¬read ..£©¡£Buffer¶ÔÏóÌṩ¶ÁдÊý¾ÝµÄ»º´æ¡£Ïà¶ÔÓÚÎÒÃÇÊìϤµÄStream¶ÔÏó£¬BufferÌṩ¸üºÃµÄÐÔÄÜÒÔ¼°¸üºÃµÄ±à³Ì͸Ã÷ÐÔ£¨ÈËΪ¿ØÖÆ»º´æµÄ´óСÒÔ¼°¾ßÌåµÄ²Ù×÷£©¡£
ÅäºÏBufferʹÓÃChannel
Ó봫ͳģʽµÄ±à³Ì²»Óã¬Channel²»Ê¹ÓÃStream£¬¶øÊÇBuffer¡£ÎÒÃÇÀ´ÊµÏÖÒ»¸ö¼òµ¥µÄ·Ç×èÈûEcho Client£º
package com.cnblogs.gpcuster;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
public class TCPEchoClientNonblocking {
public static void main(String args[]) throws Exception {
if ((args.length < 2) || (args.length > 3))// Testforcorrect#ofargs
throw new IllegalArgumentException(
"Param
Ïà¹ØÎĵµ£º
java¾µäÊé¼®»ã×Ü£¨²¿·Ö£©
Ò»¡¢ÈëÃÅ
¡¶Java 2´ÓÈëÃŵ½¾«Í¨¡·£ ÍƼö
¡¶Thinking in Java¡·£ Ç¿ÁÒÍƼö*
O’reillyµÄJava±à³Ì»ù´¡ÏµÁÐ £ ²Î¿¼*
¶þ¡¢½ø½×
¡¶Java Cook Book¡·£ ·Ç³£ÍƼö* £¨°üº¬ÁËJava±à³ÌµÄTips£¬Êʺϵ± ......
Ŀ¼
¡¡¡¡Ç°ÑÔ
¡¡¡¡Ò». Java ÎĵµºÍ javadoc
¡¡¡¡¶þ. Îĵµ×¢Ê͵ĸñʽ
¡¡¡¡¡¡¡¡1. Îĵµ×¢Ê͵ĸñʽ»¯
¡¡¡¡¡¡¡¡2. Îĵµ×¢Ê͵ÄÈý²¿·Ö
¡¡¡¡Èý. ʹÓà javadoc ±ê¼Ç
¡¡¡¡¡¡¡¡1. @see µÄʹÓÃ
¡¡¡¡¡¡¡¡2. ʹÓà @author¡¢@version ˵Ã÷Àà
¡¡¡¡¡¡¡¡3. ʹÓà @param¡¢@return ºÍ @exception ˵Ã÷·½·¨
¡¡¡ ......
·â×°ÀàÒ²½Ð°ü×°À࣬ÊÇÕë¶Ô°ËÖÖ»ù±¾¶¨ÒåÏàÓ¦µÄÒýÓÃÀàÐÍ¡£
»ù±¾Êý¾ÝÀàÐÍ ·â×°Àà
boolean &nbs ......
Compiling with the Java Compiler API
µ÷ÓÃJava±àÒëÆ÷API±àÒëJava
´ÓµÚÒ»Ì쿪ʼ,±ê×¼Javaƽ̨¾ÍȱÉÙÄܹ»±»µ÷ÓÃ,È¥²úÉúJava×Ö½ÚÂëµÄ±àÒëÆ÷½Ó¿Ú. ʹÓÃSunʵÏÖµÄƽ̨,Ò»¸öÓû§¿ÉÒÔͨ¹ý·Ç±ê×¼µÄ com.sun.tools.javac °üÖеÄMain class È¥±àÒëÄãµÄ´úÂë (Äã¿ÉÒÔÔÚlib×ÓĿ¼Ï嵀 tools.jar ÎļþÀïÕÒµ½Ëü). È»¶øÕâ¸ö° ......
Java¶¯Ì¬´úÀíÀàλÓÚJava.lang.reflect°üÏ£¬Ò»°ãÖ÷ÒªÉæ¼°µ½ÒÔÏÂÁ½¸öÀࣺ
(1). Interface InvocationHandler£º¸Ã½Ó¿ÚÖнö¶¨ÒåÁËÒ»¸ö·½·¨Object£ºinvoke(Object obj,Method method, Object[] args)¡£ÔÚʵ¼ÊʹÓÃʱ£¬µÚÒ»¸ö²ÎÊýobjÒ»°ãÊÇÖ¸´úÀíÀ࣬methodÊDZ»´úÀíµÄ·½·¨£¬ÈçÉÏÀýÖеÄdothing()£¬argsΪ¸Ã·½·¨µÄ²ÎÊýÊý×é¡£ ......