{"id":4086,"date":"2019-04-04T17:46:59","date_gmt":"2019-04-04T09:46:59","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=4086"},"modified":"2019-04-04T17:46:59","modified_gmt":"2019-04-04T09:46:59","slug":"%e8%a2%ab%e8%92%99%e5%9c%a8%e9%bc%93%e9%87%8c%e7%9a%84%e9%ab%98%e6%80%a7%e8%83%bd-reactor-%e7%9a%84%e7%bb%8f%e5%85%b8%e6%a8%a1%e5%bc%8f%e3%80%81%e5%a4%9a%e5%b7%a5%e4%bd%9c%e7%ba%bf%e7%a8%8b%e6%a8%a1","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=4086","title":{"rendered":"\u88ab\u8499\u5728\u9f13\u91cc\u7684\u9ad8\u6027\u80fd Reactor \u7684\u7ecf\u5178\u6a21\u5f0f\u3001\u591a\u5de5\u4f5c\u7ebf\u7a0b\u6a21\u5f0f\u3001\u591aReactor\u6a21\u5f0f"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u8bf4\u8d77 Reactor\uff0c\u76f8\u4fe1\u5927\u90e8\u5206\u4eba\u90fd\u5f88\u964c\u751f\u3002\u4f46\u662f\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u4f60\u4eec\u53ef\u80fd\u90fd\u7528\u5230\u4e86\u5b83\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9996\u5148\uff0c\u5728\u4e00\u4e9b\u9ad8\u6027\u80fd NIO \u6846\u67b6\u4e2d\u6709\u4f7f\u7528\uff0c\u6bd4\u5982\uff1anetty\u3002\u518d\u6bd4\u5982\uff0cRedis \u4e2d\u4e5f\u6709\u4f7f\u7528\u3002\u6240\u4ee5\u8bf4 Reactor \u5bf9\u6211\u4eec\u6765\u8bf4\u662f\u719f\u6089\u800c\u53c8\u964c\u751f\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u5199\u4e86\u4e00\u5927\u5806\u7684 WebFlux \u6559\u7a0b\uff0c\u5f88\u591a\u4eba\u53ef\u80fd\u8fd8\u4e0d\u77e5\u9053\u3002\u4f46\u5176\u5b9e\u5b83\u7684\u5e95\u5c42\u4e5f\u662f Reactor\u3002Reactor \u662f\u672a\u6765\u7684\u4e00\u79cd\u8d8b\u52bf\uff0c\u672a\u6765\u5df2\u6765\uff0c\u800c\u4f60\u8fd8\u6ca1\u505a\u597d\u51c6\u5907\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Reactor \u662f\u4e00\u79cd\u5e94\u7528\u5728\u670d\u52a1\u5668\u7aef\u7684\u5f00\u53d1\u6a21\u5f0f\uff0c\u76ee\u7684\u662f\u63d0\u9ad8\u670d\u52a1\u7aef\u7a0b\u5e8f\u7684\u5e76\u53d1\u80fd\u529b\uff0c\u5176\u5b9e\u5c31\u662f\u5b9e\u73b0\u4e86 I\/O \u591a\u8def\u590d\u7528\u8fd9\u79cd I\/O \u6a21\u578b\u3002\u4e3a\u4e86\u5b66\u4e60\u5b83\uff0c\u6211\u4eec\u6765\u770b\u4e00\u770b\u4f20\u7edf\u7684\u591a\u7ebf\u7a0b\u5b9e\u73b0\u9910\u5385\u70b9\u9910\u7684\u65b9\u5f0f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1g1qqelbljaj20lz0bojsi.jpg\" alt=\"\" \/><figcaption>\u4f20\u7edf IO \u6a21\u578b<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\n\n\u4e0a\u56fe\uff0c\u53ef\u80fd\u662f\u6211\u4eec\u53bb\u9910\u9986\u5403\u996d\u7ecf\u5e38\u9047\u5230\u7684\u573a\u666f\u3002\u8fd9\u4e2a\u6d41\u7a0b\u53ef\u4ee5\u5f52\u7eb3\u5982\u4e0b\uff1a\n<br><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u670d\u52a1\u5458 X \u7ed9\u51fa\u83dc\u5355\uff0c\u5e76\u7b49\u5f85\u70b9\u83dc\n<\/li><li>\u987e\u5ba2 Y \u67e5\u770b\u83dc\u5355\uff0c\u5e76\u70b9\u83dc\n<\/li><li>\u670d\u52a1\u5458 X \u628a\u83dc\u5355\u4ea4\u7ed9\u53a8\u5e08\uff0c\u53a8\u5e08\u7167\u7740\u505a\u83dc\n<\/li><li>\u53a8\u5e08 N \u505a\u597d\u83dc\uff0c\u4e0a\u9910\u684c\n<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u6a21\u5f0f\uff0c\u770b\u4f3c\u4e0d\u9519\uff0c\u4f46\u5b9e\u9645\u4e0a\u6548\u7387\u5e76\u4e0d\u9ad8\u3002\u5982\u679c\u9910\u5385\u751f\u610f\u8d8a\u6765\u8d8a\u597d\uff0c\u90a3\u4e48\u987e\u5ba2\u4eba\u6570\u5c31\u4f1a\u4e0d\u65ad\u589e\u52a0\uff0c\u8fd9\u65f6\u670d\u52a1\u5458\u5c31\u6709\u70b9\u5904\u7406\u4e0d\u8fc7\u6765\u4e86\u3002\u9700\u8981\u8001\u677f\u589e\u52a0\u4eba\u6570\u4e86\uff0c\u4f46\u662f\u7cbe\u660e\u7684\u8001\u677f\u53d1\u73b0\uff0c\u6bcf\u4e2a\u670d\u52a1\u5458\u5728\u670d\u52a1\u5b8c\u5ba2\u4eba\u540e\uff0c\u90fd\u8981\u53bb\u4f11\u606f\u4e00\u4e0b\uff0c \u56e0\u6b64\u8001\u677f\u5c31\u8bf4\uff0c\u201c\u4f60\u4eec\u90fd\u522b\u4f11\u606f\u4e86\uff0c\u5728\u65c1\u8fb9\u5f85\u547d\u201d\u3002\u8fd9\u6837\u53ef\u80fd 10 \u4e2a\u670d\u52a1\u5458\u4e5f\u6765\u5f97\u53ca\u670d\u52a1 20 \u4e2a\u987e\u5ba2\u4e86\u3002\u8fd9\u4e5f\u662f\u201c\u7ebf\u7a0b\u6c60\u201d\u7684\u65b9\u5f0f\uff0c\u901a\u8fc7\u91cd\u7528\u7ebf\u7a0b\u6765\u51cf\u5c11\u7ebf\u7a0b\u7684\u521b\u5efa\u548c\u9500\u6bc1\u65f6\u95f4\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u3002 <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f46\u662f\u968f\u7740\u751f\u610f\u7684\u7ee7\u7eed\u53d8\u597d\uff0c\u987e\u5ba2\u4eba\u6570\u53c8\u589e\u52a0\u4e86\u3002\u8001\u677f\u60f3\u5230\u4ec5\u4ec5\u9760\u5265\u524a\u670d\u52a1\u5458\u7684\u4f11\u606f\u65f6\u95f4\u4e5f\u6ca1\u6709\u529e\u6cd5\u670d\u52a1\u8fd9\u4e48\u591a\u987e\u5ba2\u3002\u4e8e\u662f\u7cbe\u660e\u7684\u8001\u677f\u53c8\u53d1\u73b0\u6bcf\u4e2a\u670d\u52a1\u5458\u5e76\u4e0d\u662f\u4e00\u76f4\u5728\u5e72\u6d3b\u7684\uff0c\u5927\u90e8\u5206\u65f6\u95f4\u4ed6\u4eec\u53ea\u662f\u7ad9\u5728\u9910\u684c\u65c1\u8fb9\u7b49\u5ba2\u4eba\u70b9\u83dc\u3002 <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\uff0c\u8001\u677f\u5c31\u53c8\u53d8\u62db\u4e86\u3002\u8981\u6c42\u5404\u4e2a\u670d\u52a1\u5458\uff0c\u5ba2\u4eba\u70b9\u83dc\u7684\u65f6\u5019\u4f60\u4eec\u5c31\u522b\u50bb\u7ad9\u7740\u4e86\uff0c\u5148\u53bb\u670d\u52a1\u5176\u5b83\u5ba2\u4eba\uff0c\u6709\u5ba2\u4eba\u70b9\u597d\u7684\u65f6\u5019\u558a\u4f60\u4eec\u518d\u8fc7\u53bb\u3002 <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6309\u7167\u8fd9\u4e2a\u6a21\u5f0f\uff0c\u8fd0\u884c\u4e86\u4e24\u5929\uff0c\u8001\u677f\u53d1\u73b0\u6839\u672c\u5c31\u4e0d\u9700\u8981\u90a3\u4e48\u591a\u7684\u670d\u52a1\u5458\uff0c\u4e8e\u662f\u88c1\u4e86\u4e00\u6ce2\u5458\uff0c\u751a\u81f3\u53ef\u4ee5\u53ea\u6709\u4e00\u4e2a\u670d\u52a1\u5458\u3002\u6700\u7ec8\u53ef\u4ee5\u7528\u4e0b\u56fe\u8fdb\u884c\u5f52\u7eb3\u603b\u7ed3\u3002 <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1g1qqijkyehj20mg0amdh7.jpg\" alt=\"\" \/><figcaption>\u6539\u8fdb\u7684 IO \u6a21\u578b<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u8fd9\u4e2a\u56fe\u5c31\u975e\u5e38\u7684\u7b26\u5408 Reactor \u6a21\u5f0f\u7684\u6838\u5fc3\u601d\u60f3\uff1a\u51cf\u5c11\u7b49\u5f85\u3002\u5f53\u9047\u5230\u9700\u8981\u7b49\u5f85 IO \u65f6\uff0c\u5148\u91ca\u653e\u8d44\u6e90\uff0c\u800c\u5728 IO \u5b8c\u6210\u65f6\uff0c\u518d\u901a\u8fc7\u4e8b\u4ef6\u9a71\u52a8 (event driven) \u7684\u65b9\u5f0f\uff0c\u7ee7\u7eed\u63a5\u4e0b\u6765\u7684\u5904\u7406\u3002\u4ece\u6574\u4f53\u4e0a\u51cf\u5c11\u4e86\u8d44\u6e90\u7684\u6d88\u8017\u3002 <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Java \u4e2d Reactor \u53c8\u5206\u4e3a 3 \u79cd\u6a21\u5f0f\uff1a\u7ecf\u5178\u6a21\u5f0f\u3001\u591a\u5de5\u4f5c\u7ebf\u7a0b\u6a21\u5f0f\u3001\u591a Reactor \u6a21\u5f0f\u3002\u4e0b\u9762\u6211\u4eec\u5148\u6765\u8bf4\u7b2c\u4e00\u79cd Reactor \u7ecf\u5178\u6a21\u5f0f\u3002 <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1g1qqjhrb2zj20im07q75u.jpg\" alt=\"\" \/><figcaption>Reactor \u7ecf\u5178\u6a21\u5f0f<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Reactor \u7ecf\u5178\u6a21\u5f0f\u53c8\u79f0\u4e3a\u5355\u7ebf\u7a0b\u6a21\u5f0f\u3002\u8fd9\u79cd\u6a21\u5f0f\u7684\u7279\u70b9\u5c31\u662f\uff0c\u5f53\u591a\u4e2a client \u8fde\u63a5\u65f6\uff0c\u6709\u4e00\u4e2a\u4e13\u95e8\u7684 ServerSocketChannel \u8d1f\u8d23 OP_ACCEPT \u4e8b\u4ef6\u3002\u5bf9\u4e8e\u5177\u4f53\u7684 I\/O \u64cd\u4f5c\uff0c\u5206\u914d\u7ed9\u53e6\u5916\u4e00\u4e2a ServerSocketChannel \u7684 OP_READ \u4e8b\u4ef6\u3002\u7b80\u5355\u7684\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Selector selector = Selector.open();\nServerSocketChannel serverSocketChannel = ServerSocketChannel.open();\nserverSocketChannel.configureBlocking(false);\nserverSocketChannel.bind(new InetSocketAddress(1234));\nserverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);\nwhile (selector.select() &gt; 0) {\n  Set&lt;SelectionKey&gt; keys = selector.selectedKeys();\n  Iterator&lt;SelectionKey&gt; iterator = keys.iterator();\n  while (iterator.hasNext()) {\n    SelectionKey key = iterator.next();\n    iterator.remove();\n    if (key.isAcceptable()) {\n      ServerSocketChannel acceptServerSocketChannel = (ServerSocketChannel) key.channel();\n      SocketChannel socketChannel = acceptServerSocketChannel.accept();\n      socketChannel.configureBlocking(false);\n      LOGGER.info(\"Accept request from {}\", socketChannel.getRemoteAddress());\n      socketChannel.register(selector, SelectionKey.OP_READ);\n    } else if (key.isReadable()) {\n      SocketChannel socketChannel = (SocketChannel) key.channel();\n      ByteBuffer buffer = ByteBuffer.allocate(1024);\n      int count = socketChannel.read(buffer);\n      if (count &lt;= 0) {\n        socketChannel.close();\n        key.cancel();\n        continue;\n      }\n    }\n    keys.remove(key);\n  }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u79cd\u5355\u7ebf\u7a0b\u6a21\u5f0f\uff0c\u6709\u4e00\u4e2a\u95ee\u9898\uff0c\u4e0d\u77e5\u9053\u7ec6\u5fc3\u7684\u7f51\u53cb\u6709\u6ca1\u6709\u53d1\u73b0\u3002\u8fd9\u4e2a selector.select() \u662f\u963b\u585e\u7684\uff0c\u5f53\u6709\u81f3\u5c11\u4e00\u4e2a\u901a\u9053\u53ef\u7528\u65f6\u8be5\u65b9\u6cd5\u8fd4\u56de\u53ef\u7528\u901a\u9053\u4e2a\u6570\u3002\u540c\u65f6\u8be5\u65b9\u6cd5\u53ea\u6355\u83b7 Channel \u6ce8\u518c\u65f6\u6307\u5b9a\u7684\u6240\u5173\u6ce8\u7684\u4e8b\u4ef6\u3002\u6240\u4ee5\uff0c\u6548\u7387\u8fd8\u6709\u63d0\u5347\u7a7a\u95f4\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b9e\u9645\u4e0a\uff0c\u5728\u591a\u6838 CPU \u7684\u65f6\u4ee3\uff0c\u5b83\u5e76\u4e0d\u80fd\u91cd\u590d\u7684\u5229\u7528 CPU \u7684\u591a\u6838\u4f18\u52bf\uff0c\u6240\u4ee5\uff0c\u8fd9\u79cd\u6a21\u5f0f\u7528\u7684\u4e0d\u591a\u3002\u4e8e\u662f\uff0c\u57fa\u4e8e\u591a\u7ebf\u7a0b\u6a21\u5f0f\u7684 Reactor \u5c31\u51fa\u73b0\u4e86\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1g1qqlk2yewj20j90dp40q.jpg\" alt=\"\" \/><figcaption>Reactor \u591a\u7ebf\u7a0b\u6a21\u5f0f<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u79cd\u6a21\u5f0f\u5c31\u662f\u5728 read \u7684\u65f6\u5019\uff0c\u5229\u7528\u591a\u7ebf\u7a0b\u6a21\u578b\u3002\u800c acceptor \u8fd8\u662f\u6709\u4e00\u4e2a\u7ebf\u7a0b\u6765\u8d1f\u8d23\u3002\u5177\u4f53\u7684\u7b80\u5316\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Selector selector = Selector.open();\nServerSocketChannel serverSocketChannel = ServerSocketChannel.open();\nserverSocketChannel.configureBlocking(false);\nserverSocketChannel.bind(new InetSocketAddress(1234));\nserverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);\nwhile (true) {\n  if(selector.selectNow() &lt; 0) {\n    continue;\n  }\n  Set&lt;SelectionKey&gt; keys = selector.selectedKeys();\n  Iterator&lt;SelectionKey&gt; iterator = keys.iterator();\n  while(iterator.hasNext()) {\n    SelectionKey key = iterator.next();\n    iterator.remove();\n    if (key.isAcceptable()) {\n      ServerSocketChannel acceptServerSocketChannel = (ServerSocketChannel) key.channel();\n      SocketChannel socketChannel = acceptServerSocketChannel.accept();\n      socketChannel.configureBlocking(false);\n      SelectionKey readKey = socketChannel.register(selector, SelectionKey.OP_READ);\n      readKey.attach(new Processor());\n    } else if (key.isReadable()) {\n      Processor processor = (Processor) key.attachment();\n      processor.process(key);\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5177\u4f53\u7684\u8bfb\u8bf7\u6c42\u5904\u7406\u5728\u5982\u4e0b\u6240\u793a\u7684 Processor \u7c7b\u4e2d\u3002\u8be5\u7c7b\u4e2d\u8bbe\u7f6e\u4e86\u4e00\u4e2a\u9759\u6001\u7684\u7ebf\u7a0b\u6c60\u5904\u7406\u6240\u6709\u8bf7\u6c42\u3002\u800c process \u65b9\u6cd5\u5e76\u4e0d\u76f4\u63a5\u5904\u7406 I\/O \u8bf7\u6c42\uff0c \u800c\u662f\u628a\u8be5 I\/O \u64cd\u4f5c\u63d0\u4ea4\u7ed9\u4e0a\u8ff0\u7ebf\u7a0b\u6c60\u53bb\u5904\u7406\uff0c\u8fd9\u6837\u5c31\u5145\u5206\u5229\u7528\u4e86\u591a\u7ebf\u7a0b\u7684\u4f18\u52bf\uff0c\u540c\u65f6\u5c06\u5bf9\u65b0\u8fde\u63a5\u7684\u5904\u7406\u548c\u8bfb\/\u5199\u64cd\u4f5c\u7684\u5904\u7406\u653e\u5728\u4e86\u4e0d\u540c\u7684\u7ebf\u7a0b\u4e2d\uff0c \u8bfb\/\u5199\u64cd\u4f5c\u4e0d\u518d\u963b\u585e\u5bf9\u65b0\u8fde\u63a5\u8bf7\u6c42\u7684\u5904\u7406\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Processor {\n  private static final ExecutorService service = Executors.newFixedThreadPool(16);\n  public void process(SelectionKey selectionKey) {\n    service.submit(() -&gt; {\n      ByteBuffer buffer = ByteBuffer.allocate(1024);\n      SocketChannel socketChannel = (SocketChannel) selectionKey.channel();\n      int count = socketChannel.read(buffer);\n      if (count &lt; 0) {\n        socketChannel.close();\n        selectionKey.cancel();\n        return null;\n      } else if(count == 0) {\n        return null;\n      }\n      return null;\n    });\n  }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u7684\u6a21\u5f0f\u770b\u4f3c\u5b8c\u7f8e\uff0c\u4f46\u5b9e\u9645\u4e0a\u8fd8\u6709\u6539\u8fdb\u7a7a\u95f4\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1g1qqnfypx4j20j90dp40q.jpg\" alt=\"\" \/><figcaption>\u591a Reactor \u6a21\u5f0f<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5176\u5b9e\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06 Reactor \u5206\u6210\u4e24\u90e8\u5206\uff0c\u4e00\u90e8\u5206\u65f6 mainReactor\uff0c\u4e00\u90e8\u5206\u662f subReactor\uff0c\u8fd9\u5c31\u662f\u591a Reactor \u6a21\u5f0f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">mainReactor \u8d1f\u8d23\u76d1\u542c\u5e76 accept \u65b0\u8fde\u63a5\uff0c\u7136\u540e\u5c06\u5efa\u7acb\u7684 socket \u901a\u8fc7\u591a\u8def\u590d\u7528\u5668\uff08Acceptor\uff09\u5206\u6d3e\u7ed9 subReactor\u3002subReactor \u8d1f\u8d23\u591a\u8def\u5206\u79bb\u5df2\u8fde\u63a5\u7684 socket\uff0c\u8bfb\u5199\u7f51\u7edc\u6570\u636e\uff1b\u4e1a\u52a1\u5904\u7406\u529f\u80fd\uff0c\u5176\u4ea4\u7ed9 worker \u7ebf\u7a0b\u6c60\u5b8c\u6210\u3002\u901a\u5e38\uff0csubReactor \u4e2a\u6570\u4e0a\u53ef\u4e0e CPU \u4e2a\u6570\u7b49\u540c\u3002\u591a Reactor \u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Selector selector = Selector.open();\nServerSocketChannel serverSocketChannel = ServerSocketChannel.open();\nserverSocketChannel.configureBlocking(false);\nserverSocketChannel.bind(new InetSocketAddress(1234));\nserverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);\nint coreNum = Runtime.getRuntime().availableProcessors();\nProcessor[] processors = new Processor[coreNum];\nfor (int i = 0; i &lt; processors.length; i++) {\n  processors[i] = new Processor();\n}\nint index = 0;\nwhile (selector.select() &gt; 0) {\n  Set&lt;SelectionKey&gt; keys = selector.selectedKeys();\n  for (SelectionKey key : keys) {\n    keys.remove(key);\n    if (key.isAcceptable()) {\n      ServerSocketChannel acceptServerSocketChannel = (ServerSocketChannel) key.channel();\n      SocketChannel socketChannel = acceptServerSocketChannel.accept();\n      socketChannel.configureBlocking(false);\n      Processor processor = processors[(int) ((index++) % coreNum)];\n      processor.addChannel(socketChannel);\n      processor.wakeup();\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Processor \u7684\u76f8\u5173\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Processor {\n  private static final ExecutorService service =\n      Executors.newFixedThreadPool(2 * Runtime.getRuntime().availableProcessors());\n  private Selector selector;\n  public Processor() throws IOException {\n    this.selector = SelectorProvider.provider().openSelector();\n    start();\n  }\n  public void addChannel(SocketChannel socketChannel) throws ClosedChannelException {\n    socketChannel.register(this.selector, SelectionKey.OP_READ);\n  }\n  public void wakeup() {\n    this.selector.wakeup();\n  }\n  public void start() {\n    service.submit(() -&gt; {\n      while (true) {\n        if (selector.select(500) &lt;= 0) {\n          continue;\n        }\n        Set&lt;SelectionKey&gt; keys = selector.selectedKeys();\n        Iterator&lt;SelectionKey&gt; iterator = keys.iterator();\n        while (iterator.hasNext()) {\n          SelectionKey key = iterator.next();\n          iterator.remove();\n          if (key.isReadable()) {\n            ByteBuffer buffer = ByteBuffer.allocate(1024);\n            SocketChannel socketChannel = (SocketChannel) key.channel();\n            int count = socketChannel.read(buffer);\n            if (count &lt; 0) {\n              socketChannel.close();\n              key.cancel();\n              continue;\n            } else if (count == 0) {\n              continue;\n            }\n          }\n        }\n      }\n    });\n  }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u591a Reactor \u7684\u6a21\u5f0f\u5176\u5b9e\u5c31\u662f Netty \u7684\u6a21\u5f0f\uff0c\u6a21\u4eff Netty \u7684\u5173\u952e\u4ee3\u7801\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=4086\">\u88ab\u8499\u5728\u9f13\u91cc\u7684\u9ad8\u6027\u80fd Reactor \u7684\u7ecf\u5178\u6a21\u5f0f\u3001\u591a\u5de5\u4f5c\u7ebf\u7a0b\u6a21\u5f0f\u3001\u591aReactor\u6a21\u5f0f<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u8bf4\u8d77 Reactor\uff0c\u76f8\u4fe1\u5927\u90e8\u5206\u4eba\u90fd\u5f88\u964c\u751f\u3002\u4f46\u662f\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u4f60\u4eec\u53ef\u80fd\u90fd\u7528\u5230\u4e86\u5b83\u3002 \u9996\u5148\uff0c\u5728\u4e00\u4e9b\u9ad8\u6027\u80fd NIO  [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1656,252,1711,1701,77],"class_list":["post-4086","post","type-post","status-publish","format-standard","hentry","category-java","tag-netty","tag-nio","tag-reactor","tag-webflux","tag-77"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4086","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4086"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4086\/revisions"}],"predecessor-version":[{"id":4087,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4086\/revisions\/4087"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}