自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java解讀NIO Socket非阻塞模式

開發(fā) 后端
NIO 有一個主要的類Selector,這個類似一個觀察者,只要我們把需要探知的socketchannel告訴Selector,我們接著做別的事情,當有 事件發(fā)生時,他會通知我們,傳回一組SelectionKey,我們讀取這些Key,就會獲得我們剛剛注冊過的socketchannel,然后,我們從 這個Channel中讀取數(shù)據(jù),放心,包準能夠讀到,接著我們可以處理這些數(shù)據(jù)。

NIO主要原理和適用

NIO 有一個主要的類Selector,這個類似一個觀察者,只要我們把需要探知的socketchannel告訴Selector,我們接著做別的事情,當有 事件發(fā)生時,他會通知我們,傳回一組SelectionKey,我們讀取這些Key,就會獲得我們剛剛注冊過的socketchannel,然后,我們從 這個Channel中讀取數(shù)據(jù),放心,包準能夠讀到,接著我們可以處理這些數(shù)據(jù)。

Selector內(nèi)部原理實際是在做一個對所注冊的channel的輪詢訪問,不斷的輪詢(目前就這一個算法),一旦輪詢到一個channel有所注冊的事情發(fā)生,比如數(shù)據(jù)來了,他就會站起來報告,交出一把鑰匙,讓我們通過這把鑰匙來讀取這個channel的內(nèi)容。

jdk供的無阻塞I/O(NIO)有效解決了多線程服務(wù)器存在的線程開銷問題,但在使用上略顯得復雜一些。在NIO中使用多線程,主要目的已不是為了應(yīng)對 每個客戶端請求而分配獨立的服務(wù)線程,而是通過多線程充分使用用多個CPU的處理能力和處理中的等待時間,達到提高服務(wù)能力的目的。

這段時間在研究NIO,寫篇博客來記住學過的東西。還是從最簡單的Hello World開始,client多線程請求server端,server接收client的名字,并返回Hello! +名字的字符格式給client。當然實際應(yīng)用并不這么簡單,實際可能是訪問文件或者數(shù)據(jù)庫獲取信息返回給client。非阻塞的NIO有何神秘之處?

代 碼:

1)server端代碼

  1. public class HelloWorldServer {   
  2.  
  3.     static int BLOCK = 1024;   
  4.     static String name = "";   
  5.     protected Selector selector;   
  6.     protected ByteBuffer clientBuffer = ByteBuffer.allocate(BLOCK);   
  7.     protected CharsetDecoder decoder;   
  8.     static CharsetEncoder encoder = Charset.forName("GB2312").newEncoder();   
  9.  
  10.     public HelloWorldServer(int port) throws IOException {   
  11.         selector = this.getSelector(port);   
  12.         Charset charset = Charset.forName("GB2312");   
  13.         decoder = charset.newDecoder();   
  14.     }   
  15.  
  16.     // 獲取Selector   
  17.     protected Selector getSelector(int port) throws IOException {   
  18.         ServerSocketChannel server = ServerSocketChannel.open();   
  19.         Selector sel = Selector.open();   
  20.         server.socket().bind(new InetSocketAddress(port));   
  21.         server.configureBlocking(false);   
  22.         server.register(sel, SelectionKey.OP_ACCEPT);   
  23.         return sel;   
  24.     }   
  25.  
  26.     // 監(jiān)聽端口   
  27.     public void listen() {   
  28.         try {   
  29.             for (;;) {   
  30.                 selector.select();   
  31.                 Iterator iter = selector.selectedKeys().iterator();   
  32.                 while (iter.hasNext()) {   
  33.                     SelectionKey key = (SelectionKey) iter.next();   
  34.                     iter.remove();   
  35.                     process(key);   
  36.                 }   
  37.             }   
  38.         } catch (IOException e) {   
  39.             e.printStackTrace();   
  40.         }   
  41.     }   
  42.  
  43.     // 處理事件   
  44.     protected void process(SelectionKey key) throws IOException {   
  45.         if (key.isAcceptable()) { // 接收請求   
  46.             ServerSocketChannel server = (ServerSocketChannel) key.channel();   
  47.             SocketChannel channel = server.accept();   
  48.             //設(shè)置非阻塞模式   
  49.             channel.configureBlocking(false);   
  50.             channel.register(selector, SelectionKey.OP_READ);   
  51.         } else if (key.isReadable()) { // 讀信息   
  52.             SocketChannel channel = (SocketChannel) key.channel();   
  53.             int count = channel.read(clientBuffer);   
  54.             if (count > 0) {   
  55.                 clientBuffer.flip();   
  56.                 CharBuffer charBuffer = decoder.decode(clientBuffer);   
  57.                 name = charBuffer.toString();   
  58.                 // System.out.println(name);   
  59.                 SelectionKey sKey = channel.register(selector,   
  60.                         SelectionKey.OP_WRITE);   
  61.                 sKey.attach(name);   
  62.             } else {   
  63.                 channel.close();   
  64.             }   
  65.  
  66.             clientBuffer.clear();   
  67.         } else if (key.isWritable()) { // 寫事件   
  68.             SocketChannel channel = (SocketChannel) key.channel();   
  69.             String name = (String) key.attachment();   
  70.                
  71.             ByteBuffer block = encoder.encode(CharBuffer   
  72.                     .wrap("Hello !" + name));   
  73.                
  74.  
  75.             channel.write(block);   
  76.  
  77.             //channel.close();   
  78.  
  79.         }   
  80.     }   
  81.  
  82.     public static void main(String[] args) {   
  83.         int port = 8888;   
  84.         try {   
  85.             HelloWorldServer server = new HelloWorldServer(port);   
  86.             System.out.println("listening on " + port);   
  87.                
  88.             server.listen();   
  89.                
  90.         } catch (IOException e) {   
  91.             e.printStackTrace();   
  92.         }   
  93.     }   
  94. }  

2)client端代碼

  1. public class HelloWorldClient {   
  2.  
  3.     static int SIZE = 10;   
  4.     static InetSocketAddress ip = new InetSocketAddress("localhost"8888);   
  5.     static CharsetEncoder encoder = Charset.forName("GB2312").newEncoder();   
  6.  
  7.     static class Message implements Runnable {   
  8.         protected String name;   
  9.         String msg = "";   
  10.  
  11.         public Message(String index) {   
  12.             this.name = index;   
  13.         }   
  14.  
  15.         public void run() {   
  16.             try {   
  17.                 long start = System.currentTimeMillis();   
  18.                 //打開Socket通道   
  19.                 SocketChannel client = SocketChannel.open();   
  20.                 //設(shè)置為非阻塞模式   
  21.                 client.configureBlocking(false);   
  22.                 //打開選擇器   
  23.                 Selector selector = Selector.open();   
  24.                 //注冊連接服務(wù)端socket動作   
  25.                 client.register(selector, SelectionKey.OP_CONNECT);   
  26.                 //連接   
  27.                 client.connect(ip);   
  28.                 //分配內(nèi)存   
  29.                 ByteBuffer buffer = ByteBuffer.allocate(8 * 1024);   
  30.                 int total = 0;   
  31.  
  32.                 _FOR: for (;;) {   
  33.                     selector.select();   
  34.                     Iterator iter = selector.selectedKeys().iterator();   
  35.  
  36.                     while (iter.hasNext()) {   
  37.                         SelectionKey key = (SelectionKey) iter.next();   
  38.                         iter.remove();   
  39.                         if (key.isConnectable()) {   
  40.                             SocketChannel channel = (SocketChannel) key   
  41.                                     .channel();   
  42.                             if (channel.isConnectionPending())   
  43.                                 channel.finishConnect();   
  44.                             channel   
  45.                                     .write(encoder   
  46.                                             .encode(CharBuffer.wrap(name)));   
  47.  
  48.                             channel.register(selector, SelectionKey.OP_READ);   
  49.                         } else if (key.isReadable()) {   
  50.                             SocketChannel channel = (SocketChannel) key   
  51.                                     .channel();   
  52.                             int count = channel.read(buffer);   
  53.                             if (count > 0) {   
  54.                                 total += count;   
  55.                                 buffer.flip();   
  56.  
  57.                                 while (buffer.remaining() > 0) {   
  58.                                     byte b = buffer.get();   
  59.                                     msg += (char) b;   
  60.                                        
  61.                                 }   
  62.  
  63.                                 buffer.clear();   
  64.                             } else {   
  65.                                 client.close();   
  66.                                 break _FOR;   
  67.                             }   
  68.                         }   
  69.                     }   
  70.                 }   
  71.                 double last = (System.currentTimeMillis() - start) * 1.0 / 1000;   
  72.                 System.out.println(msg + "used time :" + last + "s.");   
  73.                 msg = "";   
  74.             } catch (IOException e) {   
  75.                 e.printStackTrace();   
  76.             }   
  77.         }   
  78.     }   
  79.  
  80.     public static void main(String[] args) throws IOException {   
  81.        
  82.         String names[] = new String[SIZE];   
  83.  
  84.         for (int index = 0; index < SIZE; index++) {   
  85.             names[index] = "jeff[" + index + "]";   
  86.             new Thread(new Message(names[index])).start();   
  87.         }   
  88.        
  89.     }   
  90. }  

原文鏈接:http://itemdetail.iteye.com/blog/787149

【編輯推薦】

  1. 利用NIO建立Socket服務(wù)器
  2. 用Java.nio.* 進行網(wǎng)絡(luò)編程
  3. Java NIO的wakeup剖析
  4. Java NIO類庫關(guān)系圖解
  5. 淺析Tomcat NIO 配置
責任編輯:林師授 來源: 虛客_zZ的博客
相關(guān)推薦

2011-12-08 10:12:34

JavaNIO

2023-07-31 08:55:01

Java NIO非阻塞阻塞

2011-12-14 10:31:43

2020-05-08 10:34:30

Spring非阻塞編程

2021-02-04 10:50:11

網(wǎng)絡(luò)安全非阻塞模Winsock編程

2021-03-04 08:34:55

同步阻塞非阻塞

2021-06-04 18:14:15

阻塞非阻塞tcp

2011-12-13 12:32:54

JavaNIO

2011-12-02 13:16:14

JavaNIO

2019-10-18 08:22:43

BIONIOAIO

2019-05-05 08:50:42

阻塞非阻塞BIO

2021-02-27 16:08:17

Java異步非阻塞

2019-07-23 11:01:57

Python同步異步

2017-03-01 16:40:12

Linux驅(qū)動技術(shù)設(shè)備阻塞

2011-12-07 17:05:45

JavaNIO

2012-10-10 10:00:27

同步異步開發(fā)Java

2016-11-28 09:08:43

java系統(tǒng)異步非阻塞

2011-03-11 09:51:47

Java NIO

2012-02-22 21:15:41

unixIO阻塞

2018-03-28 08:52:53

阻塞非阻塞I
點贊
收藏

51CTO技術(shù)棧公眾號