zoukankan      html  css  js  c++  java
  • 反应器(Reactor)模式

    反应器(Reactor)模式

    http://blog.csdn.net/linxcool/article/details/7771952

    概述

    Java NIO非堵塞技术实际是采取反应器模式,或者说是观察者(observer)模式为我们监察I/O端口,如果有内容进来,会自动通知我们,这样,我们就不必开启多个线程死等,从外界看,实现了流畅的I/O读写,不堵塞了。

    同步和异步区别:有无通知(是否轮询)
    堵塞和非堵塞区别:操作结果是否等待(是否马上有返回值),只是设计方式的不同

    NIO 有一个主要的类Selector,这个类似一个观察者,只要我们把需要探知的socketchannel告诉Selector,我们接着做别的事情,当有事件发生时,他会通知我们,传回一组SelectionKey,我们读取这些Key,就会获得我们刚刚注册过的socketchannel,然后,我们从这个Channel中读取数据,接着我们可以处理这些数据。

    反应器模式与观察者模式在某些方面极为相似:当一个主体发生改变时,所有依属体都得到通知。不过,观察者模式与单个事件源关联,而反应器模式则与多个事件源关联 。

    一般模型

    我们想象以下情形:长途客车在路途上,有人上车有人下车,但是乘客总是希望能够在客车上得到休息。

    传统的做法是:每隔一段时间(或每一个站),司机或售票员对每一个乘客询问是否下车。

    反应器模式做法是:汽车是乘客访问的主体(Reactor),乘客上车后,到售票员(acceptor)处登记,之后乘客便可以休息睡觉去了,当到达乘客所要到达的目的地后,售票员将其唤醒即可。

    代码实现

    [java] view plain copy
     
    1. package com.linxcool.reactor;  
    2.   
    3. import java.io.IOException;  
    4. import java.net.InetAddress;  
    5. import java.net.InetSocketAddress;  
    6. import java.nio.channels.SelectionKey;  
    7. import java.nio.channels.Selector;  
    8. import java.nio.channels.ServerSocketChannel;  
    9. import java.util.Iterator;  
    10. import java.util.Set;  
    11.   
    12. /** 
    13.  * 反应器模式 
    14.  * 用于解决多用户访问并发问题 
    15.  *  
    16.  * 举个例子:餐厅服务问题 
    17.  *  
    18.  * 传统线程池做法:来一个客人(请求)去一个服务员(线程) 
    19.  * 反应器模式做法:当客人点菜的时候,服务员就可以去招呼其他客人了,等客人点好了菜,直接招呼一声“服务员” 
    20.  *  
    21.  * @author linxcool 
    22.  */  
    23. public class Reactor implements Runnable{  
    24.     public final Selector selector;  
    25.     public final ServerSocketChannel serverSocketChannel;  
    26.   
    27.     public Reactor(int port) throws IOException{  
    28.         selector=Selector.open();  
    29.         serverSocketChannel=ServerSocketChannel.open();  
    30.         InetSocketAddress inetSocketAddress=new InetSocketAddress(InetAddress.getLocalHost(),port);  
    31.         serverSocketChannel.socket().bind(inetSocketAddress);  
    32.         serverSocketChannel.configureBlocking(false);  
    33.           
    34.         //向selector注册该channel    
    35.         SelectionKey selectionKey=serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);  
    36.   
    37.         //利用selectionKey的attache功能绑定Acceptor 如果有事情,触发Acceptor   
    38.         selectionKey.attach(new Acceptor(this));  
    39.     }  
    40.   
    41.     @Override  
    42.     public void run() {  
    43.         try {  
    44.             while(!Thread.interrupted()){  
    45.                 selector.select();  
    46.                 Set<SelectionKey> selectionKeys= selector.selectedKeys();  
    47.                 Iterator<SelectionKey> it=selectionKeys.iterator();  
    48.                 //Selector如果发现channel有OP_ACCEPT或READ事件发生,下列遍历就会进行。  
    49.                 while(it.hasNext()){  
    50.                     //来一个事件 第一次触发一个accepter线程    
    51.                     //以后触发SocketReadHandler  
    52.                     SelectionKey selectionKey=it.next();  
    53.                     dispatch(selectionKey);  
    54.                     selectionKeys.clear();  
    55.                 }  
    56.             }  
    57.         } catch (IOException e) {  
    58.             e.printStackTrace();  
    59.         }  
    60.     }  
    61.       
    62.     /** 
    63.      * 运行Acceptor或SocketReadHandler 
    64.      * @param key 
    65.      */  
    66.     void dispatch(SelectionKey key) {  
    67.         Runnable r = (Runnable)(key.attachment());    
    68.         if (r != null){    
    69.             r.run();  
    70.         }    
    71.     }    
    72.   
    73. }  
    [java] view plain copy
     
    1. package com.linxcool.reactor;  
    2.   
    3. import java.io.IOException;  
    4. import java.nio.channels.SocketChannel;  
    5.   
    6. public class Acceptor implements Runnable{  
    7.     private Reactor reactor;  
    8.     public Acceptor(Reactor reactor){  
    9.         this.reactor=reactor;  
    10.     }  
    11.     @Override  
    12.     public void run() {  
    13.         try {  
    14.             SocketChannel socketChannel=reactor.serverSocketChannel.accept();  
    15.             if(socketChannel!=null)//调用Handler来处理channel  
    16.                 new SocketReadHandler(reactor.selector, socketChannel);  
    17.         } catch (IOException e) {  
    18.             e.printStackTrace();  
    19.         }  
    20.     }  
    21. }  
    [java] view plain copy
     
    1. package com.linxcool.reactor;  
    2.   
    3. import java.io.IOException;  
    4. import java.nio.ByteBuffer;  
    5. import java.nio.channels.SelectionKey;  
    6. import java.nio.channels.Selector;  
    7. import java.nio.channels.SocketChannel;  
    8.   
    9. public class SocketReadHandler implements Runnable{  
    10.     private SocketChannel socketChannel;  
    11.     public SocketReadHandler(Selector selector,SocketChannel socketChannel) throws IOException{  
    12.         this.socketChannel=socketChannel;  
    13.         socketChannel.configureBlocking(false);  
    14.           
    15.         SelectionKey selectionKey=socketChannel.register(selector, 0);  
    16.           
    17.         //将SelectionKey绑定为本Handler 下一步有事件触发时,将调用本类的run方法。    
    18.         //参看dispatch(SelectionKey key)    
    19.         selectionKey.attach(this);  
    20.           
    21.         //同时将SelectionKey标记为可读,以便读取。    
    22.         selectionKey.interestOps(SelectionKey.OP_READ);    
    23.         selector.wakeup();  
    24.     }  
    25.       
    26.     /** 
    27.      * 处理读取数据 
    28.      */  
    29.     @Override  
    30.     public void run() {  
    31.         ByteBuffer inputBuffer=ByteBuffer.allocate(1024);  
    32.         inputBuffer.clear();  
    33.         try {  
    34.             socketChannel.read(inputBuffer);  
    35.             //激活线程池 处理这些request  
    36.             //requestHandle(new Request(socket,btt));   
    37.         } catch (IOException e) {  
    38.             e.printStackTrace();  
    39.         }  
    40.     }  
    41. }  
  • 相关阅读:
    Intellij IDEA 关闭阿里编码规约“请不要使用行尾注释”提醒
    VS 进行了无法编译的编辑
    IntelliJ IDEA lombok log 报红
    SQL Server 事务执行SQL
    Linux 检查应用不在现就重启
    Java入门5.2---String类、StringBuffer类、StringBuilder类、System类、Data类、SimpleDataFormat类、Calendar类、Math类、BigInteger类与BigDecimal类
    为什么要进行补偿
    解决flex布局中 space-between方法的排版问题
    桑基图的使用场景
    清蒸罗非鱼的做法和步骤
  • 原文地址:https://www.cnblogs.com/handsome1013/p/7762612.html
Copyright © 2011-2022 走看看