使用JavaNIO提高服务端程序的性能

浏阳历史网 2020-06-22 14:33:18

结合具体的Java Socket编程,讨论使用NIO提高服务端程序的性能的问题。 Java NIO增加了新的SocketChannel、ServerSocketChannel等类来提供对构建高性能的服务端程序的支持。 SocketChannel、ServerSocketChannel能够在非阻塞的模式下工作,它们都是selectable的类。在构建服务器或者中间件时,推荐使用Java NIO。 在传统的络编程中,我们通常使用一个专用线程(Thread)来处理一个Socket连接,通过使用NIO,一个或者很少几个Socket线程就可以处理成千上万个活动的Socket连接。 通常情况下,通过en()获得一个ServerSocketChannel的实例,通过en或者cept()获得一个SocketChannel实例。要使ServerSocketChannel或者SocketChannel在非阻塞的模式下操作,可以调用 nfigureBlocking (false); 或者 nfigureBlocking (false); 语句来达到目的导读:亚马逊今日公布了近期游戏软件销量Top5数据。通常情况下,服务端可以使用非阻塞的ServerSocketChannel,这样,服务端的程序就可以更容易地同时处理多个socket线程。 下面我们来看一个综合例子,这个例子使用了ServerSocketChannel、SocketChannel开发了一个非阻塞的、能处理多线程的Echo服务端程序,见示例。 【程序源代码】 1 // ==================== Program Discription =====================2 // 程序名称:示例 : va3 // 程序目的:学习Java NIO#SocketChannel4 // ==============================================================5 6 7 import teBuffer;8 import rverSocketChannel;9 import cketChannel;10 import lector;11 import lectionKey;12 import lectableChannel;13 14 import cket;15 import rverSocket;16 import etSocketAddress;17 import erator;1819 public class SocketChannelDemo20 21 {22 public static int PORT_NUMBER = 23;//监听端口23 ServerSocketChannel serverChannel;24 ServerSocket serverSocket ;25 Selector selector ;26 private ByteBuffer buffer = locateDirect (1024);27 28 public static void main (String [] args)29 throws Exception30 {31 SocketChannelDemo server=new SocketChannelDemo();32 it(args);33 artWork();34 }35 36 37 public void init (String [] argv)throws Exception38 {39 int port = PORT_NUMBER;40 41 if (ngth 0) { 42 port = rseInt (argv [0]);43 }44 45 intln ("Listening on port " + port);46 47 // 分配一个ServerSocketChannel48 serverChannel = en();49 // 从ServerSocketChannel里获得一个对应的Socket50 serverSocket = cket();51 // 生成一个Selector52 selector = en();53 54 // 把Socket绑定到端口上55 nd (new InetSocketAddress (port));56 //serverChannel为非bolck57 nfigureBlocking (false);58 59 // 通过Selector注册ServerSocetChannel60 gister (selector, _ACCEPT); 61 62 }63 64 public void startWork()throws Exception65 66 {67 while (true) {68 69 int n = lect();//获得IO准备就绪的channel数量70 71 if (n == 0) {72 continue; // 没有channel准备就绪,继续执行73 }74 75 // 用一个iterator返回Selector的selectedkeys76 Iterator it = lectedKeys().iterator();77 78 // 处理每一个SelectionKey79 while (sNext()) {80 SelectionKey key = (SelectionKey) xt();8182 // 判断是否有新的连接到达83 if (Acceptable()) {84           //返回SelectionKey的ServerSocketChannel85 ServerSocketChannel server =(ServerSocketChannel) annel();86 SocketChannel channel = cept();87 88 registerChannel (selector, channel,89 _READ);90 91 doWork (channel);92 }93 94 // 判断是否有数据在此channel里需要读取95 if (Readable()) {96 97 processData (key);98 99 }100 101 //删除 selectedkeys102 move();103 }104 }105 }106 protected void registerChannel (Selector selector,107 SelectableChannel channel运营商渠道已是“昨日黄花”?其实不然, int ops)108 throws Exception109 {110 if (channel == null) {111 return; 112 }113 114 115 nfigureBlocking (false);116 117 gister (selector, ops);118 }119 120 //处理接收的数据121 protected void processData (SelectionKey key)122 throws Exception123 {124 125 126 SocketChannel socketChannel = (SocketChannel) annel();127 int count;128 129 ear(); // 清空buffer130 131 // 读取所有的数据132 while ((count = ad (buffer)) 0) {133 ip(); 134135 // send the data, don′t assume it goes all at once136 while (sRemaining())137 {138 //如果收到回车键,则在返回的字符前增加[echo]$字样139 if(t()==(char)13)140 {141 ear();142 t("[echo]___FCKpd___0quot;.getBytes());143 ip();144 145 }146 ite (buffer);//在Socket里写数据147 }148 149 ear(); // 清空buffer150 }151 152 if (count 0) {153 // count0,说明已经读取完毕154 ose();155 }156 }157 158 159 private void doWork (SocketChannel channel)throws Exception160 {161 ear();162 t ("Hello,I am working,please input some thing,and i will echo to you![echo] ___FCKpd___0quot;.getBytes());163 ip();164 ite (buffer);165 }166 167 }

使用:运行此程序,然后在控制台输入命令telnet localhost 23。

查看本文来源

济南好的白癜风医院
四川治疗白癜风医院
慢性鼻炎用什么喷剂
友情链接