成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Java NIO之選擇就緒模式

開發 后端
Java NIO出現不只是一個技術性能的提高,你會發現網絡上到處在介紹它,因為它具有里程碑意義,從JDK1.4開始,Java開始提高性能相關的功能,從而使得Java在底層或者并行分布式計算等操作上已經可以和C或Perl等語言并駕齊驅。

Java NIO非堵塞應用通常適用用在I/O讀寫等方面,我們知道,系統運行的性能瓶頸通常在I/O讀寫,包括對端口和文件的操作上,過去,在打開一個I/O通道后,read()將一直等待在端口一邊讀取字節內容,如果沒有內容進來,read()也是傻傻的等,這會影響我們程序繼續做其他事情,那么改進做法就是開設線程,讓線程去等待,但是這樣做也是相當耗費資源的。

Java NIO非堵塞技術實際是采取Reactor模式,或者說是Observer模式為我們監察I/O端口,如果有內容進來,會自動通知我們,這樣,我們就不必開啟多個線程死等,從外界看,實現了流暢的I/O讀寫,不堵塞了。

Java NIO出現不只是一個技術性能的提高,你會發現網絡上到處在介紹它,因為它具有里程碑意義,從JDK1.4開始,Java開始提高性能相關的功能,從而使得Java在底層或者并行分布式計算等操作上已經可以和C或Perl等語言并駕齊驅。


圖 1 類結構圖

 

  1. package cn.chenkangxian.nioconcurrent; 
  2.  
  3. import java.io.IOException; 
  4. import java.nio.ByteBuffer; 
  5. import java.nio.channels.SelectionKey; 
  6. import java.nio.channels.SocketChannel; 
  7. import java.util.LinkedList; 
  8. import java.util.List; 
  9.  
  10. /** 
  11.  * @Project: testNio 
  12.  *  
  13.  * @Author: chenkangxian 
  14.  *  
  15.  * @Annotation: 使用線程池來處理大量channel并發 
  16.  *  
  17.  * @Date:2011-7-5 
  18.  *  
  19.  * @Copyright: 2011 chenkangxian, All rights reserved. 
  20.  *  
  21.  */ 
  22. public class SelectSocketsThreadPool extends SelectSockets { 
  23.  
  24.     private static final int MAX_THREADS = 5
  25.     private ThreadPool pool = new ThreadPool(MAX_THREADS); 
  26.  
  27.     /** 
  28.      * 從socket中讀數據 
  29.      */ 
  30.     protected void readDataFromSocket(SelectionKey key) throws Exception { 
  31.         WorkerThread worker = pool.getWorker(); 
  32.         if (worker == null) { 
  33.             return
  34.         worker.serviceChannel(key); 
  35.     } 
  36.     /** 
  37.      *   
  38.      * @Project: concurrentnio 
  39.      * 
  40.      * @Author: chenkangxian 
  41.      * 
  42.      * @Annotation:線程池 
  43.      * 
  44.      * @Date:2011-7-20 
  45.      * 
  46.      * @Copyright: 2011 chenkangxian, All rights reserved. 
  47.      * 
  48.      */ 
  49.     private class ThreadPool { 
  50.         List idle = new LinkedList(); 
  51.         /** 
  52.          * 線程池初始化 
  53.          *  
  54.          * @param poolSize 線程池大小 
  55.          */ 
  56.         ThreadPool(int poolSize) { 
  57.             for (int i = 0; i < poolSize; i++) { 
  58.                 WorkerThread thread = new WorkerThread(this); 
  59.                 thread.setName("Worker" + (i + 1)); 
  60.                 thread.start(); 
  61.                 idle.add(thread); 
  62.             } 
  63.         } 
  64.         /** 
  65.          * 獲得工作線程 
  66.          *  
  67.          * Author: chenkangxian 
  68.          * 
  69.          * Last Modification Time: 2011-7-20 
  70.          * 
  71.          * @return 
  72.          */ 
  73.         WorkerThread getWorker() { 
  74.             WorkerThread worker = null
  75.  
  76.             synchronized (idle) { 
  77.                 if (idle.size() > 0) { 
  78.                     worker = (WorkerThread) idle.remove(0); 
  79.                 } 
  80.             } 
  81.             return (worker); 
  82.         } 
  83.         /** 
  84.          * 送回工作線程 
  85.          *  
  86.          * Author: chenkangxian 
  87.          * 
  88.          * Last Modification Time: 2011-7-20 
  89.          * 
  90.          * @param worker 
  91.          */ 
  92.         void returnWorker(WorkerThread worker) { 
  93.             synchronized (idle) { 
  94.                 idle.add(worker); 
  95.             } 
  96.         } 
  97.     } 
  98.     private class WorkerThread extends Thread { 
  99.         private ByteBuffer buffer = ByteBuffer.allocate(1024); 
  100.         private ThreadPool pool; 
  101.         private SelectionKey key; 
  102.         WorkerThread(ThreadPool pool) { 
  103.             this.pool = pool; 
  104.         } 
  105.         public synchronized void run() { 
  106.             System.out.println(this.getName() + " is ready"); 
  107.             while (true) { 
  108.                 try { 
  109.                     this.wait();//等待被notify 
  110.                 } catch (InterruptedException e) { 
  111.                     e.printStackTrace(); 
  112.                     this.interrupt(); 
  113.                 } 
  114.                 if (key == null) {//直到有key 
  115.                     continue
  116.                 } 
  117.                 System.out.println(this.getName() + " has been awakened"); 
  118.                 try { 
  119.                     drainChannel(key); 
  120.                 } catch (Exception e) { 
  121. System.out.println("Caught '" + e + "' closing channel"); 
  122.                     try { 
  123. key.channel().close(); 
  124.                     } catch (IOException ex) { 
  125.     ex.printStackTrace(); 
  126.                     } 
  127.                     key.selector().wakeup(); 
  128.                 } 
  129.                 key = null
  130.                 this.pool.returnWorker(this); 
  131.             } 
  132.         } 
  133.         synchronized void serviceChannel(SelectionKey key) { 
  134.             this.key = key; 
  135.             //消除讀的關注 
  136.             key.interestOps(key.interestOps() & (~SelectionKey.OP_READ)); 
  137.             this.notify(); 
  138.         } 
  139.         void drainChannel(SelectionKey key) throws Exception { 
  140.             SocketChannel channel = (SocketChannel) key.channel(); 
  141.             int count; 
  142.             buffer.clear();  
  143.             while ((count = channel.read(buffer)) > 0) { 
  144.                 buffer.flip(); 
  145.                 while (buffer.hasRemaining()) { 
  146.                     channel.write(buffer); 
  147.                 } 
  148.                 buffer.clear(); 
  149.             } 
  150.             if (count < 0) { 
  151.                 channel.close(); 
  152.                 return
  153.             } 
  154.             //重新開始關注讀事件 
  155.             key.interestOps(key.interestOps() | SelectionKey.OP_READ); 
  156.             key.selector().wakeup(); 
  157.         } 
  158.     } 
  159.     public static void main(String[] args) throws Exception { 
  160.         new SelectSocketsThreadPool().go(args); 
  161.     } 

 

  1. package cn.chenkangxian.nioconcurrent; 
  2. import java.net.InetSocketAddress; 
  3. import java.net.ServerSocket; 
  4. import java.nio.ByteBuffer; 
  5. import java.nio.channels.SelectableChannel; 
  6. import java.nio.channels.SelectionKey; 
  7. import java.nio.channels.Selector; 
  8. import java.nio.channels.ServerSocketChannel; 
  9. import java.nio.channels.SocketChannel; 
  10. import java.util.Iterator; 
  11. /** 
  12.  *  
  13.  * @Project: concurrentnio 
  14.  * 
  15.  * @Author: chenkangxian 
  16.  * 
  17.  * @Annotation:  
  18.  * 
  19.  * @Date:2011-7-11 
  20.  * 
  21.  * @Copyright: 2011 chenkangxian, All rights reserved. 
  22.  * 
  23.  */ 
  24. public class SelectSockets { 
  25.     public static int PORT_NUMBER = 1234
  26.     private ByteBuffer buffer = ByteBuffer.allocate(1024); 
  27.     public static void main(String[] args) throws Exception { 
  28.         new SelectSockets().go(args); 
  29.     } 
  30.     public void go(String[] args) throws Exception{ 
  31.         int port = PORT_NUMBER; 
  32. //      if(args.length > 0){ 
  33. //          port = Integer.parseInt(args[0]); 
  34. //      } 
  35. //      System.out.println("Listening on port " + port); 
  36.         ServerSocketChannel serverChannel = ServerSocketChannel.open(); 
  37.         ServerSocket serverSocket = serverChannel.socket(); 
  38.          
  39.         Selector selector = Selector.open(); 
  40.         serverSocket.bind(new InetSocketAddress(port)); 
  41.         serverChannel.configureBlocking(false); 
  42.         serverChannel.register(selector, SelectionKey.OP_ACCEPT); 
  43.          
  44.         while(true){ 
  45.             int n = selector.select(); //沒有輪詢,單個selector 
  46.             if(n == 0){ 
  47.                 continue;  
  48.             } 
  49.             Iterator it = selector.selectedKeys().iterator(); 
  50.              
  51.             while(it.hasNext()){ 
  52.                 SelectionKey key = (SelectionKey)it.next(); 
  53.                 if(key.isAcceptable()){ 
  54.                     ServerSocketChannel server =                (ServerSocketChannel)key.channel(); 
  55.                     SocketChannel channel = server.accept();        registerChannel(selector,channel,SelectionKey.OP_READ); 
  56.                     sayHello(channel); 
  57.                 } 
  58.                 if(key.isReadable()){ 
  59.                     readDataFromSocket(key); 
  60.                 } 
  61.                 it.remove(); 
  62.             } 
  63.         } 
  64.     } 
  65.     /** 
  66.      * 在selector上注冊channel,并設置interest 
  67.      *  
  68.      * Author: chenkangxian 
  69.      * 
  70.      * Last Modification Time: 2011-7-11 
  71.      * 
  72.      * @param selector 選擇器 
  73.      *  
  74.      * @param channel 通道 
  75.      *  
  76.      * @param ops interest 
  77.      *  
  78.      * @throws Exception 
  79.      */ 
  80.     protected void registerChannel(Selector selector, 
  81.             SelectableChannel channel, int ops) throws Exception{ 
  82.         if(channel == null){ 
  83.             return ;  
  84.         } 
  85.         channel.configureBlocking(false); 
  86.         channel.register(selector, ops); 
  87.     } 
  88.     /** 
  89.      * 處理有可用數據的通道 
  90.      *  
  91.      * Author: chenkangxian 
  92.      * 
  93.      * Last Modification Time: 2011-7-11 
  94.      * 
  95.      * @param key 可用通道對應的key 
  96.      *  
  97.      * @throws Exception 
  98.      */ 
  99.     protected void readDataFromSocket(SelectionKey key) throws Exception{ 
  100.         SocketChannel socketChannel = (SocketChannel)key.channel(); 
  101.         int count; 
  102.         buffer.clear(); //Empty buffer 
  103.         while((count = socketChannel.read(buffer)) > 0){ 
  104.             buffer.flip();  
  105.             while(buffer.hasRemaining()){ 
  106.                 socketChannel.write(buffer); 
  107.             } 
  108.             buffer.clear();  
  109.         } 
  110.         if(count < 0){ 
  111.             socketChannel.close(); 
  112.         } 
  113.     } 
  114.     /** 
  115.      * 打招呼 
  116.      *  
  117.      * Author: chenkangxian 
  118.      * 
  119.      * Last Modification Time: 2011-7-11 
  120.      * 
  121.      * @param channel 客戶端channel 
  122.      *  
  123.      * @throws Exception 
  124.      */ 
  125.     private void sayHello(SocketChannel channel) throws Exception{ 
  126.         buffer.clear(); 
  127.         buffer.put("Hello 哈羅! \r\n".getBytes()); 
  128.         buffer.flip(); 
  129.         channel.write(buffer); 
  130.     } 

原文鏈接:http://chenkangxian.iteye.com/blog/1288246

【編輯推薦】

  1. Java代碼規范那些事
  2. Java效率真的很低嗎?Android為何要采用?
  3. 漫談Java開源5年:自由但帶著枷鎖
  4. Google提供JavaScript庫以簡化Google API的調用
  5. 解析Java語言11個主要特性
責任編輯:林師授 來源: chenkangxian的博客
相關推薦

2011-11-25 09:56:16

H3C

2015-11-09 17:28:12

戴爾云計算

2015-11-09 19:03:04

戴爾云計算

2011-12-07 17:17:02

JavaNIO

2022-12-08 09:10:11

I/O模型Java

2012-05-16 17:22:11

Java設計模式

2011-11-17 16:03:05

Java工廠模式Clojure

2012-05-16 17:15:04

Java設計模式

2011-12-15 09:40:06

Javanio

2011-12-07 14:57:44

JavaNIO

2011-12-15 09:55:47

javanio

2011-12-15 11:19:08

JavaNIO

2013-05-23 15:59:00

線程池

2011-04-06 11:41:25

Java動態代理

2023-07-28 08:23:05

選擇器Java NIO

2020-09-10 13:51:48

Kubernetes云原生容器

2020-08-21 07:23:50

工廠模式設計

2011-12-15 10:10:33

Javanio

2011-12-15 10:43:20

JavaNIO

2022-02-22 08:00:48

JavaNIOBuffer
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产一二三区精品视频 | 国产精品a久久久久 | 国产日韩欧美一区二区 | 香蕉一区 | av日韩高清| 另类亚洲视频 | 久久精品中文字幕 | 久久久久久久久久久久久久国产 | 亚洲日本免费 | 日本免费一区二区三区四区 | 久久久国产一区 | av不卡一区| av电影一区二区 | 国产99免费视频 | 成人毛片在线视频 | 久久精品久久综合 | 国产精品久久久久久久久图文区 | 毛片在线免费 | 国产成人福利在线观看 | 国产日韩精品久久 | 欧美一区二区三区四区视频 | 国产在线一区二区三区 | 91av导航| 亚洲精品久久久一区二区三区 | av毛片 | 久久精品成人一区 | 国产成人精品一区二三区在线观看 | 91色综合| 国产乱码高清区二区三区在线 | 欧美色人 | 成人3d动漫一区二区三区91 | 一区二区国产精品 | 少妇一级淫片免费放播放 | 成人免费观看男女羞羞视频 | 国产色在线 | 欧美一区二区三区的 | 日韩欧美三区 | 免费观看色 | 免费精品视频一区 | 一区二区成人 | 2019精品手机国产品在线 |