NIO主要原理和适用

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

Selector内部原理实际是在做一个对所注册的channel的轮询访问,不断的轮询(目前就这一个算法),一旦轮询到一个channel有所注册的事情发生,比如数据来了,他就会站起来报告,交出一把钥匙,让我们通过这把钥匙来读取这个channel的内容。

jdk供的无阻塞I/O(NIO)有效解决了多线程服务器存在的线程开销问题,但在使用上略显得复杂一些。在NIO中使用多线程,主要目的已不是为了应对 每个客户端请求而分配独立的服务线程,而是通过多线程充分使用用多个CPU的处理能力和处理中的等待时间,达到提高服务能力的目的。

这段时间在研究NIO,写篇博客来记住学过的东西。还是从最简单的Hello World开始,client多线程请求server端,server接收client的名字,并返回Hello! +名字的字符格式给client。当然实际应用并不这么简单,实际可能是访问文件或者数据库获取信息返回给client。非阻塞的NIO有何神秘之处?

代 码:

1)server端代码

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

2)client端代码

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

NIO Socket非阻塞模式的更多相关文章

  1. Java NIO Socket 非阻塞通信

    相对于非阻塞通信的复杂性,通常客户端并不需要使用非阻塞通信以提高性能,故这里只有服务端使用非阻塞通信方式实现 客户端: package com.test.client; import java.io. ...

  2. 看到关于socket非阻塞模式设置方式记录一下。

    关于socket的阻塞与非阻塞模式以及它们之间的优缺点,这已经没什么可言的:我打个很简单的比方,如果你调用socket send函数时: 如果是阻塞模式下: send先比较待发送数据的长度len和套接 ...

  3. JAVA NIO使用非阻塞模式实现高并发服务器

    参考:http://blog.csdn.net/zmx729618/article/details/51860699  https://zhuanlan.zhihu.com/p/23488863 ht ...

  4. JAVA基础知识之网络编程——-基于NIO的非阻塞Socket通信

    阻塞IO与非阻塞IO 通常情况下的Socket都是阻塞式的, 程序的输入输出都会让当前线程进入阻塞状态, 因此服务器需要为每一个客户端都创建一个线程. 从JAVA1.4开始引入了NIO API, NI ...

  5. IO通信模型(二)同步非阻塞模式NIO(NonBlocking IO)

    同步非阻塞模式(NonBlocking IO) 在非阻塞模式中,发出Socket的accept()和read()操作时,如果内核中的数据还没有准备好,那么它并不会阻塞用户进程,而是立刻返回一个信息.也 ...

  6. UDP socket 设置为的非阻塞模式

    UDP socket 设置为的非阻塞模式 Len = recvfrom(SocketFD, szRecvBuf, sizeof(szRecvBuf), MSG_DONTWAIT, (struct so ...

  7. socket异步通信-如何设置成非阻塞模式、非阻塞模式下判断connect成功(失败)、判断recv/recvfrom成功(失败)、判断send/sendto

    socket异步通信-如何设置成非阻塞模式.非阻塞模式下判断connect成功(失败).判断recv/recvfrom成功(失败).判断send/sendto 博客分类: Linux Socket s ...

  8. Socket 阻塞模式和非阻塞模式

    阻塞I/O模型: 简介:进程会一直阻塞,直到数据拷贝 完成 应用程序调用一个IO函数,导致应用程序阻塞,等待数据准备好. 如果数据没有准备好,一直等待….数据准备好了,从内核拷贝到用户空间,IO函数返 ...

  9. Socket阻塞模式和非阻塞模式的区别

    简单点说: 阻塞就是干不完不准回来,    非组赛就是你先干,我现看看有其他事没有,完了告诉我一声 我们拿最常用的send和recv两个函数来说吧... 比如你调用send函数发送一定的Byte,在系 ...

随机推荐

  1. My.Ioc 代码示例——注册项的注销和更新

    当您需要从 Ioc 容器中注销/删除一个注册项的时候,您会怎么做呢? 有人曾经在 stackoverflow 上提问“如何从 Unity 中注销一个注册项”.对于这个问题,有人的回答是“有趣.你为什么 ...

  2. 关于uploadify 没有显示进度条!!!!

    如果你也弄了很久不知道为什么不出现上传进度条!,那就一定要看这里了! 我注释了 queueID 属性后 就出现了!!!!! 就是这么简答! //添加界面的附件管理 $('#file_upload'). ...

  3. jquery get checkbox inside element(td).

    <td id="skill"><input name="skill" type="checkbox" value=&quo ...

  4. hdu 3371 Connect the Cities (最小生成树Prim)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目不难 稍微注意一下 要把已经建好的城市之间的花费定义为0,在用普通Prim算法就可以了:我没 ...

  5. CentOS6.5安装LAMP环境APACHE的安装

    1.卸载apr.apr-util [root@centos6 LAMP]# yum remove apr apr-util 2.编译安装apr-1.5.1.tar.gz [root@centos6 L ...

  6. html form <label>标签基础语法结构与使用案例教程(转载)

    在表单布局中会遇到label标签的使用,label没有任何样式效果,有触发对应表单控件功能.比如我们点击单选按钮或多选框前文字对应选项就能被选中,这个就是对文字加了<label>标签实现. ...

  7. jqgrid设置单元格数据

    $("#gridid").jqGrid('setCell',rowid,icol,data); rowid为行ID,jqgrid内置的那个,从1开始 icol为列索引,从0开始, ...

  8. 2.2.5 NIO.2 Path 和 Java 已有的 File 类

    NIO与IO交互 toPath() File -- Path toFile() Path -- File Demo: import java.io.File; import java.nio.file ...

  9. [转]JS继承的5种实现方式

    参考链接: http://yahaitt.iteye.com/blog/250338 虽说书上都讲过继承的方式方法,但这些东西每看一遍都多少有点新收获,所以单独拿出来放着. 1. 对象冒充 funct ...

  10. iPhone 被同步到 Mac上后 如果不希望更新到Mac上在哪里删除?

    前往文件夹   /Users/用户名/Library/Application Support/MobileSync  直接删除  就行了(同时要倾倒废纸篓). 目前iPhone链接Mac 后  不让 ...