Java NIO ѧϰ±Ê¼Ç SocketChannel
ΪÁ˽øÐзÇ×èÈûµÄsocketͨÐÅ£¬java nioÌṩChannel¸ÅÄîµÄʵÏÖ¡£Channel¸ÅÄîÒÔÇø±ðÓÚ´«Í³µÄ»ùÓÚÁ÷¸ÅÄîµÄIO¡£ÊÂʵÉÏChannel¸ÅÄîµÄIO¼È°üÀ¨·Ç×èÈûʽµÄIOÒ²°üÀ¨×èÈûʽIO¡£ÒªChannelÍê³É·Ç×èÈûʽIOÐèҪʵÏÖSelectableChannel½Ó¿Ú£¬ÓëSelector¹ØÁªÊ¹Óá£SocketChannel¾ÍÊÇÕâÑùÒ»¸öSelectableChannel¡£
ÓÉÓÚ£¬selector»úÖƱ¾ÉíÉæ¼°¶àÏ̵߳ÄÄÚÈÝ£¬ÓëChannel¹ØÁªÊ¹ÓýÏΪ¸´ÔÓ£»ËùÒÔÕâÀïÖ»ÒÔƬ¶Ï·½Ê½£¬Õë¶ÔSocketChannelµÄ¹¦ÄܽøÐÐ˵Ã÷¼òҪ˵Ã÷¡£
1¡£´´½¨·þÎñ¶ËµÄServerSocketChannel
¹ØÁªµÄServerSocket¿ÉÒÔÕìÌýÒ»¸öport¡£
ServerSocketChannel sschannel;
...
sschannel = ServerSocketChannel.open();
//ÉèÖÃΪno block·½Ê½¹¤×÷
sschannel.configureBlocking(false);
//¹ØÁªµÄserver-socket¿ªÊ¼ÕìÌý
address = new InetSocketAddress(port);
ServerSocket ss = sschannel.socket();
ss.bind(address);
2¡£½«ServerSocketChannel×¢²á(°ï¶¨)µ½Ò»¸öselectorÓÃÓڵȴýÍâÀ´socketÁ¬½Ó¡£
µ±Ò»¸öserver socket channelµÃµ½Ò»¸öÍⲿsocketÁ¬½Óʱ£¬¿ÉÒÔ½«Ò»¸öµ×²ãÁ¬½Ó´´½¨ÎªÒ»¸öSocketChannelʵÀý¡£ÔÙ½«Õâ¸öSocketChannell×¢²á(°ï¶¨)µ½Ò»¸öselectorÓÃÓÚ¾ßÌåIO¡£
//server socket channel×¢²áµ½selector
sschannel.register(selector, SelectionKey.OP_ACCEPT);
...
// µ±Ò»¸öserver socket channelµÃµ½Ò»¸öÍⲿsocketÁ¬½Óʱ
// ¿ÉÒ԰ѸõײãÁ¬½Ó´´½¨³öÒ»¸ö SocketChannel
SocketChannel sc = ssc.accept();
sc.configureBlocking(false); //ÉèÖÃΪ·Ç×èÈû·½Ê½
// ½«SocketChannel ×¢²áµ½Ò»¸öselectorÓÃÓÚIO
sc.register(selector, SelectionKey.OP_READ, request);
3¡£µ±SocketChannel¿ÉÒÔ¶ÁÈ¡»òд³öʱÔò¿ÉÒÔÓÃSocketChannel µÄ·½·¨½øÐÐIO¡£
¶ÁÈ¡£º
ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
buffer.clear();
// ¾ßÌåµÄ¶ÁÈ¡¶¯×÷£¬sc½«µ×²ãsocket»º´æÊý¾ÝдÊäÈëbytebuffer¶ÔÏó¡£
int n = sc.read(buffer);
//¶ÁÈ¡Êý¾ÝºóµÄ¼¸ÖÖÇé¿ö
if(n < 0){
//¶ÁÈ¡Êý¾Ýʧ°Ü£¬socketÁ¬½Ó¿ÉÄÜÒѾ¶Ï¿ª¡£
...
}else if(n > 0){
//¾ßÌå´¦Àíbuffer¶ÔÏó
...
}else{
//n == 0µÄÇé¿ö
}
д³ö£º
ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
...
// Ìî³äbuffer
buffer.clear();
buffer.put(data);
buffer.filp();
// ¾ßÌåµÄд³ö
sc.write(buffer);
4¡
Ïà¹ØÎĵµ£º
ËäÈ»ºÜÉÙÓõ½Í¬²½£¬³öÓÚ¸öÈËÐËȤ×ÔѧÁËһϡ£ÏÖ¶ÔJAVAÖеÄͬ²½ÊÕ¼¯Ñ§Ï°µÄ¶«Î÷×ܽáһϣ¬ÈÃ×Ô¼º¼¼ÊõµÄÌõÀí»¯£¬Äܹ»³ÁµíÏÂÀ´¡£
ͬ²½¾ÍÊÇ´®ÐÐʹÓÃһЩ×ÊÔ´.
JAVAÖÐsynchronized¹Ø¼ü×ÖÄܹ»×÷Ϊº¯ÊýµÄÐÞÊηû£¬Ò²¿É×÷Ϊº¯ÊýÄÚµÄÓï¾ä£¬Ò²¾ÍÊÇƽʱ˵µÄͬ²½·½·¨ºÍͬ²½Óï¾ä¿é¡£¼ÙÈçÔÙϸµÄ·ÖÀ࣬synchronized¿É×÷ÓÃÓÚinstance ......
ÒÔÏÂÖ»ÊǸöÈËÀí½â
½ñÌìѧϰspringÖеÄÊôÐÔ×¢È룬ÔÚ½²ÔÀíµÄʱºò£¬Ìáµ½ÁËjavaÖеÄÄÚÄÚÊ¡»úÖÆ£¬ÔÚÒÔÇ°Ö»ÊÇÁ˽âһϷ´Éä»úÖÆ¡£
·´Éä»úÖÆÊÇjavaÖеĶ¯Ì¬ÐÔÄÜ£¬¿ÉÒÔÒÔÀàÃûµÃµ½Àà¶ÔÏóµÄ¼¼Êõ£¬ÊµÏÖ¶¯Ì¬Éú³É¶ÔÏó¼¼Êõ£¬¿ÉÒÔÒÔ¶ÔÏóµÃµ½´Ë¶ÔÏóµÄÀàÐÅÏ¢£¨°üÀ¨ÊôÐÔ£¬·½·¨£©£¬ÊµÏִ˼¼ÊõµÄÊÇjavaÖеÄClass¶ÔÏ󣬴˶ÔÏóÊÇÃè ......
³õѧJAVA£¬ºÃ²»ÈÝÒ×½â¾öÁËÕâ¸öÊý¾Ý¿âÁ¬½ÓÎÊÌ⣬Õâ¸öÖ÷ÒªÊÇÓÐÁ½¸öÎÊÌ⣬һ¸öÊÇJDBC-odbcµÄÎÊÌ⣬Èç¹ûûÓнâ¾öºÃÊý¾ÝÔ´ÅäÖ㬾ø¶ÔÊÇÓÐÈÝÒ׳öÎÊÌâµÄ£¬¼ÙÉèÄãʹÓõÄÊÇaccessµÄÊý¾Ý¿â£¬ÕâÊÇÍøÉϵÄÒ»¸öÀý×ÓµÄÓï¾ä
String dburl="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=database.mdb";
Æäʵ²¢²»ÐèÒªÄÇô³¤£¬¼Ù ......
7£®1 Swing¼ò½é
7.1.1 ¼ò½é
¡¡¡¡µÚÎå½²ÖÐÎÒÃÇѧϰÁËAWT£¬AWTÊÇSwingµÄ»ù´¡¡£SwingµÄ²úÉúÖ÷ÒªÔÒò¾ÍÊÇAWT²»ÄÜÂú×ãͼÐλ¯Óû§½çÃæ·¢Õ¹µÄÐèÒª¡£
AWTÉè¼ÆµÄ³õÖÔÊÇÖ§³Ö¿ª·¢Ð¡Ó¦ÓóÌÐòµÄ¼òµ¥Óû§½çÃæ¡£ÀýÈçAWTȱÉÙ¼ôÌù°å¡¢´òÓ¡Ö§³Ö¡¢¼üÅ̵¼º½µÈÌØÐÔ£¬¶øÇÒÔÀ´µÄAWTÉõÖÁ²»°üÀ¨µ¯³öʽ²Ëµ¥»ò¹ö¶¯´°¸ñµÈ»ù±¾ÔªËØ¡£
¡¡¡¡´ËÍâAWT»¹´ ......