{"id":15922,"date":"2026-04-26T10:34:35","date_gmt":"2026-04-26T02:34:35","guid":{"rendered":"https:\/\/top.duoku.icu\/libs\/15922.html"},"modified":"2026-04-26T10:34:35","modified_gmt":"2026-04-26T02:34:35","slug":"java-nio-%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%e8%af%a6%e7%bb%86%e4%bd%bf%e7%94%a8%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.srclibs.com\/index.php\/2026\/04\/26\/java-nio-%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%e8%af%a6%e7%bb%86%e4%bd%bf%e7%94%a8%e6%95%99%e7%a8%8b\/","title":{"rendered":"Java NIO \u7f51\u7edc\u7f16\u7a0b\u8be6\u7ec6\u4f7f\u7528\u6559\u7a0b"},"content":{"rendered":"<div style=\"text-align:center;margin:30px 0;\"><img decoding=\"async\" src=\"https:\/\/top.duoku.icu\/wp-content\/uploads\/2026\/04\/image-88.png\" alt=\"Java NIO \u7f51\u7edc\u7f16\u7a0b\u8be6\u7ec6\u4f7f\u7528\u6559\u7a0b\" style=\"max-width:100%;height:auto;border-radius:12px;box-shadow:0 4px 12px rgba(0,0,0,0.1);\"><\/div>\n<h1>Java NIO \u7f51\u7edc\u7f16\u7a0b\u8be6\u7ec6\u4f7f\u7528\u6559\u7a0b<\/h1>\n<h2>NIO \u6982\u8ff0<\/h2>\n<p>Java NIO\uff08New I\/O\uff09\u81ea Java 1.4 \u5f15\u5165\uff0c\u662f\u5bf9\u4f20\u7edf BIO\uff08Blocking I\/O\uff09\u7684\u9769\u65b0\u3002NIO \u91c7\u7528<strong>\u975e\u963b\u585e IO<\/strong>\u548c<strong>IO \u591a\u8def\u590d\u7528<\/strong>\u6280\u672f\uff0c\u8ba9\u5355\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u9ad8\u6548\u7ba1\u7406\u6210\u5343\u4e0a\u4e07\u7684\u8fde\u63a5\uff0c\u7279\u522b\u9002\u5408\u9ad8\u5e76\u53d1\u7f51\u7edc\u5e94\u7528\u573a\u666f\u3002<\/p>\n<h3>\u6838\u5fc3\u533a\u522b\uff1aBIO vs NIO<\/h3>\n<table border=\"1\" cellpadding=\"5\">\n<tr>\n<th>\u7279\u6027<\/th>\n<th>BIO\uff08\u4f20\u7edf IO\uff09<\/th>\n<th>NIO\uff08\u65b0 IO\uff09<\/th>\n<\/tr>\n<tr>\n<td>\u963b\u585e\u6a21\u5f0f<\/td>\n<td>\u963b\u585e\u5f0f<\/td>\n<td>\u975e\u963b\u585e\/\u591a\u8def\u590d\u7528<\/td>\n<\/tr>\n<tr>\n<td>\u8fde\u63a5\u5904\u7406<\/td>\n<td>\u6bcf\u4e2a\u8fde\u63a5\u4e00\u4e2a\u7ebf\u7a0b<\/td>\n<td>\u5355\u7ebf\u7a0b\u7ba1\u7406\u591a\u4e2a\u8fde\u63a5<\/td>\n<\/tr>\n<tr>\n<td>\u9002\u7528\u573a\u666f<\/td>\n<td>\u4f4e\u5e76\u53d1\u8fde\u63a5<\/td>\n<td>\u9ad8\u5e76\u53d1\u8fde\u63a5<\/td>\n<\/tr>\n<tr>\n<td>\u6027\u80fd<\/td>\n<td>\u8fde\u63a5\u6570\u591a\u65f6\u6027\u80fd\u4e0b\u964d<\/td>\n<td>\u9ad8\u5e76\u53d1\u4e0b\u6027\u80fd\u4f18\u5f02<\/td>\n<\/tr>\n<tr>\n<td>\u7f16\u7a0b\u6a21\u578b<\/td>\n<td>\u540c\u6b65\u963b\u585e<\/td>\n<td>\u5f02\u6b65\/\u9009\u62e9\u5668\u6a21\u5f0f<\/td>\n<\/tr>\n<\/table>\n<h2>NIO \u6838\u5fc3\u7ec4\u4ef6<\/h2>\n<h3>1. Buffer\uff08\u7f13\u51b2\u533a\uff09<\/h3>\n<p>Buffer \u662f NIO \u8bfb\u5199\u6570\u636e\u65f6\u7684\u4e2d\u95f4\u5bb9\u5668\uff0c\u6240\u6709\u6570\u636e\u90fd\u5fc5\u987b\u7ecf\u8fc7 Buffer\u3002<\/p>\n<p>&#8220;`java<br \/>\n\/\/ \u521b\u5efa\u5bb9\u91cf\u4e3a 1024 \u5b57\u8282\u7684\u7f13\u51b2\u533a<br \/>\nByteBuffer buffer = ByteBuffer.allocate(1024);<\/p>\n<p>\/\/ \u5199\u5165\u6570\u636e<br \/>\nbuffer.putInt(12345);<br \/>\nbuffer.putFloat(3.14f);<\/p>\n<p>\/\/ \u5207\u6362\u4e3a\u8bfb\u53d6\u6a21\u5f0f<br \/>\nbuffer.flip();<\/p>\n<p>\/\/ \u8bfb\u53d6\u6570\u636e<br \/>\nint value = buffer.getInt();<br \/>\nfloat floatValue = buffer.getFloat();<\/p>\n<p>\/\/ \u6e05\u7a7a\u7f13\u51b2\u533a<br \/>\nbuffer.clear();<\/p>\n<pre><code>\n<strong>Buffer \u5173\u952e\u6a21\u5f0f<\/strong>\uff1a\n<ul>\n<li><strong>write \u6a21\u5f0f<\/strong>\uff1a\u5411 Buffer \u5199\u5165\u6570\u636e<\/li>\n<li><strong>flip \u6a21\u5f0f<\/strong>\uff1a\u5207\u6362\u4e3a\u8bfb\u53d6\uff0cposition \u91cd\u7f6e\u4e3a 0\uff0climit \u8bbe\u4e3a capacity<\/li>\n<li><strong>clear \u6a21\u5f0f<\/strong>\uff1a\u6e05\u7a7a Buffer\uff0c\u51c6\u5907\u4e0b\u4e00\u6b21\u5199\u5165<\/li>\n<\/ul>\n\n<h3>2. Channel\uff08\u901a\u9053\uff09<\/h3>\n\nChannel \u7c7b\u4f3c\u4e8e\u4f20\u7edf IO \u7684 Stream\uff0c\u4f46\u652f\u6301\u53cc\u5411\u8bfb\u5199\u3002\n\n<\/code><\/pre>\n<p>java<br \/>\n\/\/ ServerSocketChannel\uff1a\u76d1\u542c\u8fde\u63a5\u7684\u901a\u9053<br \/>\nServerSocketChannel serverChannel = ServerSocketChannel.open();<br \/>\nserverChannel.bind(new InetSocketAddress(8888));<br \/>\nserverChannel.configureBlocking(false); \/\/ \u975e\u963b\u585e\u6a21\u5f0f<\/p>\n<p>\/\/ SocketChannel\uff1a\u5ba2\u6237\u7aef\u8fde\u63a5\u901a\u9053<br \/>\nSocketChannel clientChannel = SocketChannel.open();<br \/>\nclientChannel.connect(new InetSocketAddress(&#8220;localhost&#8221;, 8888));<\/p>\n<p>\/\/ FileChannel\uff1a\u6587\u4ef6\u901a\u9053<br \/>\nFileChannel fileChannel = Files.newChannel(Paths.get(&#8220;file.txt&#8221;));<\/p>\n<pre><code>\n<strong>\u5e38\u89c1 Channel \u7c7b\u578b<\/strong>\uff1a\n<ul>\n<li>`ServerSocketChannel`\uff1a\u670d\u52a1\u7aef\u76d1\u542c\u901a\u9053<\/li>\n<li>`SocketChannel`\uff1a\u5ba2\u6237\u7aef\u8fde\u63a5\u901a\u9053<\/li>\n<li>`DatagramChannel`\uff1aUDP \u6570\u636e\u62a5\u901a\u9053<\/li>\n<li>`FileChannel`\uff1a\u6587\u4ef6\u8bfb\u5199\u901a\u9053<\/li>\n<\/ul>\n\n<h3>3. Selector\uff08\u9009\u62e9\u5668\uff09<\/h3>\n\nSelector \u662f NIO \u7684\u7075\u9b42\uff0c\u5141\u8bb8\u5355\u4e2a\u7ebf\u7a0b\u76d1\u542c\u591a\u4e2a Channel \u7684\u591a\u79cd\u4e8b\u4ef6\uff08\u8fde\u63a5\u3001\u8bfb\u53d6\u3001\u5199\u5165\u7b49\uff09\u3002\n\n<\/code><\/pre>\n<p>java<br \/>\n\/\/ \u521b\u5efa\u9009\u62e9\u5668<br \/>\nSelector selector = Selector.open();<\/p>\n<p>\/\/ \u6ce8\u518c Channel \u5230\u9009\u62e9\u5668<br \/>\nserverChannel.register(selector, SelectionKey.OP_ACCEPT);<\/p>\n<p>\/\/ \u963b\u585e\u7b49\u5f85\u4e8b\u4ef6\u53d1\u751f<br \/>\nselector.select();<\/p>\n<p>\/\/ \u83b7\u53d6\u6709\u4e8b\u4ef6\u53d1\u751f\u7684 SelectionKey \u96c6\u5408<br \/>\nSet<SelectionKey> keys = selector.selectedKeys();<\/p>\n<p>for (SelectionKey key : keys) {<br \/>\n    if (key.isAcceptable()) {<br \/>\n        \/\/ \u63a5\u53d7\u65b0\u8fde\u63a5<br \/>\n        SocketChannel clientChannel = serverChannel.accept();<br \/>\n        clientChannel.register(selector, SelectionKey.OP_READ);<br \/>\n    }<br \/>\n    if (key.isReadable()) {<br \/>\n        \/\/ \u5904\u7406\u8bfb\u4e8b\u4ef6<br \/>\n        SocketChannel channel = (SocketChannel) key.channel();<br \/>\n        ByteBuffer buffer = ByteBuffer.allocate(1024);<br \/>\n        int bytesRead = channel.read(buffer);<br \/>\n    }<br \/>\n    if (key.isWritable()) {<br \/>\n        \/\/ \u5904\u7406\u5199\u4e8b\u4ef6<br \/>\n    }<\/p>\n<p>    \/\/ \u4ece selectedKeys \u4e2d\u79fb\u9664\uff0c\u907f\u514d\u91cd\u590d\u5904\u7406<br \/>\n    key.cancel();<br \/>\n    keys.remove(key);<br \/>\n}<\/p>\n<pre><code>\n<h2>NIO \u963b\u585e\u4e0e\u975e\u963b\u585e\u6a21\u5f0f<\/h2>\n\n<h3>\u963b\u585e\u6a21\u5f0f\uff08Blocking Mode\uff09<\/h3>\n\n\u9ed8\u8ba4\u6a21\u5f0f\u4e0b\uff0cChannel \u64cd\u4f5c\u4f1a\u963b\u585e\u7b49\u5f85\uff0c\u76f4\u5230\u64cd\u4f5c\u5b8c\u6210\u3002\n\n<\/code><\/pre>\n<p>java<br \/>\nServerSocketChannel server = ServerSocketChannel.open();<br \/>\nserver.bind(new InetSocketAddress(8888));<\/p>\n<p>\/\/ \u963b\u585e\u7b49\u5f85\u8fde\u63a5<br \/>\nSocketChannel client = server.accept(); \/\/ \u7ebf\u7a0b\u5728\u6b64\u963b\u585e<\/p>\n<p>\/\/ \u963b\u585e\u8bfb\u53d6\u6570\u636e<br \/>\nByteBuffer buffer = ByteBuffer.allocate(1024);<br \/>\nint bytesRead = client.read(buffer); \/\/ \u7ebf\u7a0b\u5728\u6b64\u963b\u585e<\/p>\n<pre><code>\n<h3>\u975e\u963b\u585e\u6a21\u5f0f\uff08Non-blocking Mode\uff09<\/h3>\n\nChannel \u4e0d\u963b\u585e\u7b49\u5f85\uff0c\u64cd\u4f5c\u7acb\u5373\u8fd4\u56de\u3002\n\n<\/code><\/pre>\n<p>java<br \/>\nserver.configureBlocking(false); \/\/ \u8bbe\u7f6e\u4e3a\u975e\u963b\u585e<\/p>\n<p>\/\/ accept() \u7acb\u5373\u8fd4\u56de\uff0c\u6ca1\u6709\u8fde\u63a5\u8fd4\u56de null<br \/>\nSocketChannel client = server.accept();<\/p>\n<p>\/\/ read() \u7acb\u5373\u8fd4\u56de\uff0c\u6ca1\u6709\u6570\u636e\u8fd4\u56de -1<br \/>\nint bytesRead = client.read(buffer); \/\/ \u7acb\u5373\u8fd4\u56de\uff0c\u53ef\u80fd\u662f 0 \u6216 -1<\/p>\n<pre><code>\n<strong>\u975e\u963b\u585e\u4f18\u52bf<\/strong>\uff1a\n<ul>\n<li>\u5355\u7ebf\u7a0b\u53ef\u7ba1\u7406\u591a\u4e2a\u8fde\u63a5<\/li>\n<li>\u4e0d\u4f1a\u56e0\u7b49\u5f85\u800c\u6d6a\u8d39\u7ebf\u7a0b\u8d44\u6e90<\/li>\n<li>\u9002\u5408\u9ad8\u5e76\u53d1\u573a\u666f<\/li>\n<\/ul>\n\n<h2>\u5b8c\u6574\u793a\u4f8b\uff1aNIO \u670d\u52a1\u7aef<\/h2>\n\n<\/code><\/pre>\n<p>java<br \/>\nimport java.io.IOException;<br \/>\nimport java.net.InetSocketAddress;<br \/>\nimport java.nio.ByteBuffer;<br \/>\nimport java.nio.channels.*;<br \/>\nimport java.util.Iterator;<br \/>\nimport java.util.Set;<\/p>\n<p>public class NioServer {<br \/>\n    private static final int PORT = 8888;<br \/>\n    private static final int BUFFER_SIZE = 1024;<\/p>\n<p>    public static void main(String[] args) {<br \/>\n        Selector selector = null;<br \/>\n        try {<br \/>\n            \/\/ 1. \u521b\u5efa\u9009\u62e9\u5668<br \/>\n            selector = Selector.open();<\/p>\n<p>            \/\/ 2. \u521b\u5efa\u670d\u52a1\u7aef\u901a\u9053<br \/>\n            ServerSocketChannel serverChannel = ServerSocketChannel.open();<br \/>\n            serverChannel.bind(new InetSocketAddress(PORT));<\/p>\n<p>            \/\/ 3. \u8bbe\u7f6e\u4e3a\u975e\u963b\u585e\u6a21\u5f0f<br \/>\n            serverChannel.configureBlocking(false);<\/p>\n<p>            \/\/ 4. \u6ce8\u518c\u5230\u9009\u62e9\u5668\uff0c\u76d1\u542c\u8fde\u63a5\u4e8b\u4ef6<br \/>\n            serverChannel.register(selector, SelectionKey.OP_ACCEPT);<\/p>\n<p>            System.out.println(&#8220;NIO \u670d\u52a1\u7aef\u542f\u52a8\uff0c\u76d1\u542c\u7aef\u53e3\uff1a&#8221; + PORT);<\/p>\n<p>            \/\/ 5. \u5faa\u73af\u5904\u7406\u4e8b\u4ef6<br \/>\n            while (true) {<br \/>\n                \/\/ \u963b\u585e\u7b49\u5f85\u81f3\u5c11\u4e00\u4e2a\u4e8b\u4ef6\u53d1\u751f<br \/>\n                selector.select();<\/p>\n<p>                \/\/ \u83b7\u53d6\u6709\u4e8b\u4ef6\u53d1\u751f\u7684 Key \u96c6\u5408<br \/>\n                Set<SelectionKey> selectedKeys = selector.selectedKeys();<br \/>\n                Iterator<SelectionKey> iterator = selectedKeys.iterator();<\/p>\n<p>                while (iterator.hasNext()) {<br \/>\n                    SelectionKey key = iterator.next();<br \/>\n                    iterator.remove(); \/\/ \u91cd\u8981\uff1a\u5904\u7406\u540e\u79fb\u9664<\/p>\n<p>                    if (!key.isValid()) continue;<\/p>\n<p>                    \/\/ \u5904\u7406\u8fde\u63a5\u8bf7\u6c42<br \/>\n                    if (key.isAcceptable()) {<br \/>\n                        handleAccept((ServerSocketChannel) key.channel(), selector);<br \/>\n                    }<\/p>\n<p>                    \/\/ \u5904\u7406\u8bfb\u4e8b\u4ef6<br \/>\n                    if (key.isReadable()) {<br \/>\n                        handleRead((SocketChannel) key.channel());<br \/>\n                    }<\/p>\n<p>                    \/\/ \u5904\u7406\u5199\u4e8b\u4ef6<br \/>\n                    if (key.isWritable()) {<br \/>\n                        handleWrite((SocketChannel) key.channel());<br \/>\n                    }<br \/>\n                }<br \/>\n            }<\/p>\n<p>        } catch (IOException e) {<br \/>\n            e.printStackTrace();<br \/>\n        } finally {<br \/>\n            \/\/ \u8d44\u6e90\u6e05\u7406<br \/>\n            if (selector != null) {<br \/>\n                try {<br \/>\n                    selector.close();<br \/>\n                } catch (IOException e) {<br \/>\n                    e.printStackTrace();<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u5904\u7406\u65b0\u8fde\u63a5<br \/>\n    private static void handleAccept(ServerSocketChannel serverChannel, Selector selector) throws IOException {<br \/>\n        SocketChannel clientChannel = serverChannel.accept();<br \/>\n        if (clientChannel != null) {<br \/>\n            clientChannel.configureBlocking(false);<br \/>\n            \/\/ \u6ce8\u518c\u8bfb\u4e8b\u4ef6<br \/>\n            clientChannel.register(selector, SelectionKey.OP_READ);<br \/>\n            System.out.println(&#8220;\u65b0\u5ba2\u6237\u7aef\u8fde\u63a5\uff1a&#8221; + clientChannel.getRemoteAddress());<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u5904\u7406\u8bfb\u4e8b\u4ef6<br \/>\n    private static void handleRead(SocketChannel channel) throws IOException {<br \/>\n        ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);<br \/>\n        int bytesRead = channel.read(buffer);<\/p>\n<p>        if (bytesRead > 0) {<br \/>\n            buffer.flip();<br \/>\n            byte[] data = new byte[buffer.remaining()];<br \/>\n            buffer.get(data);<br \/>\n            String message = new String(data, &#8220;UTF-8&#8221;);<br \/>\n            System.out.println(&#8220;\u6536\u5230\u6d88\u606f\uff1a&#8221; + message);<\/p>\n<p>            \/\/ \u56de\u663e\u54cd\u5e94<br \/>\n            channel.write(ByteBuffer.wrap((&#8220;\u670d\u52a1\u5668\u56de\u590d\uff1a&#8221; + message).getBytes(&#8220;UTF-8&#8221;)));<br \/>\n        } else if (bytesRead == -1) {<br \/>\n            \/\/ \u5ba2\u6237\u7aef\u65ad\u5f00\u8fde\u63a5<br \/>\n            channel.close();<br \/>\n            System.out.println(&#8220;\u5ba2\u6237\u7aef\u65ad\u5f00\u8fde\u63a5&#8221;);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u5904\u7406\u5199\u4e8b\u4ef6<br \/>\n    private static void handleWrite(SocketChannel channel) throws IOException {<br \/>\n        \/\/ \u5904\u7406\u5199\u5b8c\u6210\u4e8b\u4ef6<br \/>\n        System.out.println(&#8220;\u5199\u64cd\u4f5c\u5b8c\u6210&#8221;);<br \/>\n    }<br \/>\n}<\/p>\n<pre><code>\n<h2>\u5b8c\u6574\u793a\u4f8b\uff1aNIO \u5ba2\u6237\u7aef<\/h2>\n\n<\/code><\/pre>\n<p>java<br \/>\nimport java.io.IOException;<br \/>\nimport java.net.InetSocketAddress;<br \/>\nimport java.nio.ByteBuffer;<br \/>\nimport java.nio.channels.SelectionKey;<br \/>\nimport java.nio.channels.Selector;<br \/>\nimport java.nio.channels.SocketChannel;<br \/>\nimport java.util.Set;<\/p>\n<p>public class NioClient {<br \/>\n    private static final String HOST = &#8220;localhost&#8221;;<br \/>\n    private static final int PORT = 8888;<br \/>\n    private static final int BUFFER_SIZE = 1024;<\/p>\n<p>    public static void main(String[] args) {<br \/>\n        try {<br \/>\n            \/\/ 1. \u521b\u5efa\u901a\u9053<br \/>\n            SocketChannel channel = SocketChannel.open();<br \/>\n            channel.configureBlocking(false);<\/p>\n<p>            \/\/ 2. \u8fde\u63a5\u5230\u670d\u52a1\u7aef\uff08\u975e\u963b\u585e\uff0c\u9700\u8981\u624b\u52a8\u5b8c\u6210\u8fde\u63a5\uff09<br \/>\n            channel.connect(new InetSocketAddress(HOST, PORT));<\/p>\n<p>            \/\/ 3. \u521b\u5efa\u9009\u62e9\u5668<br \/>\n            Selector selector = Selector.open();<\/p>\n<p>            \/\/ 4. \u6ce8\u518c\u9009\u62e9\u4e8b\u4ef6<br \/>\n            SelectionKey key = channel.register(selector, SelectionKey.OP_CONNECT | SelectionKey.OP_READ);<\/p>\n<p>            System.out.println(&#8220;NIO \u5ba2\u6237\u7aef\u542f\u52a8\uff0c\u6b63\u5728\u8fde\u63a5\u670d\u52a1\u7aef&#8230;&#8221;);<\/p>\n<p>            \/\/ 5. \u7b49\u5f85\u8fde\u63a5\u5b8c\u6210<br \/>\n            while (!key.isConnectable()) {<br \/>\n                selector.select();<br \/>\n            }<\/p>\n<p>            if (channel.finishConnect()) {<br \/>\n                System.out.println(&#8220;\u8fde\u63a5\u6210\u529f\uff01&#8221;);<\/p>\n<p>                \/\/ \u53d1\u9001\u6d88\u606f<br \/>\n                String message = &#8220;\u4f60\u597d\uff0cNIO \u670d\u52a1\u5668\uff01&#8221;;<br \/>\n                channel.write(ByteBuffer.wrap(message.getBytes(&#8220;UTF-8&#8221;)));<br \/>\n                System.out.println(&#8220;\u53d1\u9001\uff1a&#8221; + message);<\/p>\n<p>                \/\/ \u6ce8\u518c\u8bfb\u4e8b\u4ef6<br \/>\n                key.interestOps(SelectionKey.OP_READ);<\/p>\n<p>                \/\/ \u5904\u7406\u54cd\u5e94<br \/>\n                ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);<br \/>\n                while (true) {<br \/>\n                    selector.select();<br \/>\n                    Set<SelectionKey> keys = selector.selectedKeys();<br \/>\n                    for (SelectionKey key1 : keys) {<br \/>\n                        if (key1.isReadable()) {<br \/>\n                            int bytesRead = channel.read(buffer);<br \/>\n                            if (bytesRead > 0) {<br \/>\n                                buffer.flip();<br \/>\n                                byte[] data = new byte[buffer.remaining()];<br \/>\n                                buffer.get(data);<br \/>\n                                System.out.println(&#8220;\u6536\u5230\uff1a&#8221; + new String(data, &#8220;UTF-8&#8221;));<br \/>\n                            }<br \/>\n                        }<br \/>\n                        keys.remove(key1);<br \/>\n                    }<br \/>\n                }<br \/>\n            }<\/p>\n<p>        } catch (IOException e) {<br \/>\n            e.printStackTrace();<br \/>\n        }<br \/>\n    }<br \/>\n}<br \/>\n&#8220;`<\/p>\n<h2>\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h2>\n<h3>\u573a\u666f\u4e00\uff1a\u9ad8\u5e76\u53d1\u804a\u5929\u5ba4<\/h3>\n<p>\u4f7f\u7528 NIO \u9009\u62e9\u5668\u5b9e\u73b0\u5355\u7ebf\u7a0b\u7ba1\u7406\u6570\u5343\u4e2a\u5728\u7ebf\u7528\u6237\uff0c\u652f\u6301\u7fa4\u53d1\u6d88\u606f\u3001\u79c1\u804a\u7b49\u529f\u80fd\u3002\u76f8\u6bd4 BIO\uff0c\u53ef\u8282\u7701\u5927\u91cf\u7ebf\u7a0b\u8d44\u6e90\u3002<\/p>\n<h3>\u573a\u666f\u4e8c\uff1a\u7f51\u7edc\u6e38\u620f\u670d\u52a1\u5668<\/h3>\n<p>MMORPG \u7b49\u5728\u7ebf\u6e38\u620f\u9700\u8981\u540c\u65f6\u5904\u7406\u6210\u5343\u4e0a\u4e07\u4e2a\u73a9\u5bb6\u7684\u5b9e\u65f6\u64cd\u4f5c\uff0cNIO \u975e\u963b\u585e IO \u662f\u7406\u60f3\u9009\u62e9\uff0c\u53ef\u5927\u5e45\u964d\u4f4e\u670d\u52a1\u5668\u8d1f\u8f7d\u3002<\/p>\n<h3>\u573a\u666f\u4e09\uff1a\u6d88\u606f\u4e2d\u95f4\u4ef6<\/h3>\n<p>\u5982 RabbitMQ\u3001Kafka \u7b49\u6d88\u606f\u961f\u5217\u7cfb\u7edf\u91c7\u7528 NIO \u5904\u7406\u5927\u91cf\u8fde\u63a5\uff0c\u5b9e\u73b0\u9ad8\u6548\u7684\u6d88\u606f\u8def\u7531\u548c\u4f20\u8f93\u3002<\/p>\n<h3>\u573a\u666f\u56db\uff1aWeb \u670d\u52a1\u5668\u6846\u67b6<\/h3>\n<p>Netty\u3001Mina \u7b49\u57fa\u4e8e NIO \u7684\u7f51\u7edc\u6846\u67b6\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8e Web \u670d\u52a1\u5668\uff0c\u5982 Netty \u652f\u6491\u4e86\u652f\u4ed8\u5b9d\u3001\u6dd8\u5b9d\u7b49\u6838\u5fc3\u7cfb\u7edf\u3002<\/p>\n<h3>\u573a\u666f\u4e94\uff1a\u5fae\u670d\u52a1\u901a\u4fe1<\/h3>\n<p>Spring Cloud\u3001Dubbo \u7b49\u5fae\u670d\u52a1\u6846\u67b6\u4f7f\u7528 NIO \u5b9e\u73b0\u670d\u52a1\u95f4\u7684\u8fdc\u7a0b\u8c03\u7528\uff0c\u63d0\u5347\u7cfb\u7edf\u541e\u5410\u80fd\u529b\u3002<\/p>\n<h2>\u6700\u4f73\u5b9e\u8df5\u5efa\u8bae<\/h2>\n<ol>\n<li><strong>\u5408\u7406\u8bbe\u7f6e Buffer \u5927\u5c0f<\/strong>\uff1a\u6839\u636e\u5b9e\u9645\u4e1a\u52a1\u9700\u6c42\u8bbe\u7f6e\uff0c\u8fc7\u5927\u6d6a\u8d39\u5185\u5b58\uff0c\u8fc7\u5c0f\u5f71\u54cd\u6027\u80fd<\/li>\n<li><strong>\u6ce8\u610f\u8d44\u6e90\u6e05\u7406<\/strong>\uff1a\u4f7f\u7528 try-finally \u786e\u4fdd Selector \u548c Channel \u5173\u95ed<\/li>\n<li><strong>\u907f\u514d\u9891\u7e41\u521b\u5efa\u5bf9\u8c61<\/strong>\uff1a\u590d\u7528 Buffer \u7b49\u5bf9\u8c61\u51cf\u5c11 GC \u538b\u529b<\/li>\n<li><strong>\u5904\u7406\u5f02\u5e38\u8fde\u63a5<\/strong>\uff1a\u53ca\u65f6\u68c0\u6d4b\u5e76\u6e05\u7406\u65ad\u5f00\u6216\u5f02\u5e38\u7684\u8fde\u63a5<\/li>\n<li><strong>\u9009\u62e9\u5668\u4e8b\u4ef6\u4f18\u5316<\/strong>\uff1a\u5408\u7406\u914d\u7f6e interestOps\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u8f6e\u8be2<\/li>\n<li><strong>\u8003\u8651\u4f7f\u7528 Netty<\/strong>\uff1a\u5bf9\u4e8e\u590d\u6742\u573a\u666f\uff0c\u5efa\u8bae\u4f7f\u7528\u6210\u719f\u7684 Netty \u6846\u67b6\uff0c\u5b83\u57fa\u4e8e NIO \u4e8c\u6b21\u5c01\u88c5\uff0c\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u529f\u80fd<\/li>\n<\/ul>\n<h2>\u5e38\u89c1\u95ee\u9898<\/h2>\n<h3>1. \u8fde\u63a5\u72b6\u6001\u5904\u7406<\/h3>\n<p>\u975e\u963b\u585e connect \u9700\u8981\u7b49\u5f85\u8fde\u63a5\u5b8c\u6210\uff0c\u901a\u8fc7 `finishConnect()` \u68c0\u6d4b\u3002<\/p>\n<h3>2. \u6570\u636e\u7c98\u5305\/\u62c6\u5305<\/h3>\n<p>TCP \u662f\u6d41\u5f0f\u534f\u8bae\uff0c\u9700\u8981\u81ea\u5b9a\u4e49\u534f\u8bae\u5904\u7406\u6d88\u606f\u8fb9\u754c\uff0c\u5982\u6dfb\u52a0\u957f\u5ea6\u5b57\u6bb5\u6216\u5206\u9694\u7b26\u3002<\/p>\n<h3>3. Selector \u5355\u7ebf\u7a0b\u95ee\u9898<\/h3>\n<p>\u867d\u7136 NIO \u652f\u6301\u591a\u7ebf\u7a0b\uff0c\u4f46\u5355\u4e2a Selector \u5e94\u7531\u4e00\u4e2a\u7ebf\u7a0b\u72ec\u5360\uff0c\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8c03\u7528 select() \u4f1a\u5bfc\u81f4\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002<\/p>\n<p>&#8212;<\/p>\n<p><strong>\u603b\u7ed3<\/strong>\uff1aJava NIO \u901a\u8fc7 Selector \u673a\u5236\u5b9e\u73b0\u4e86\u9ad8\u5e76\u53d1\u7f51\u7edc\u7f16\u7a0b\uff0c\u662f\u6784\u5efa\u9ad8\u6027\u80fd\u7f51\u7edc\u5e94\u7528\u7684\u57fa\u7840\u3002\u638c\u63e1 NIO \u7684\u6838\u5fc3\u539f\u7406\u548c\u7f16\u7a0b\u6280\u5de7\uff0c\u5c06\u4e3a\u4f60\u7684\u7f51\u7edc\u5e94\u7528\u5f00\u53d1\u6253\u5f00\u65b0\u7684\u5927\u95e8\u3002\u5bf9\u4e8e\u590d\u6742\u573a\u666f\uff0c\u5efa\u8bae\u7ed3\u5408 Netty \u7b49\u6210\u719f\u6846\u67b6\u4f7f\u7528\uff0c\u53ef\u8fdb\u4e00\u6b65\u63d0\u5347\u5f00\u53d1\u6548\u7387\u548c\u7cfb\u7edf\u7a33\u5b9a\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java NIO \u7f51\u7edc\u7f16\u7a0b\u8be6\u7ec6\u4f7f\u7528\u6559\u7a0b NIO \u6982\u8ff0 Java NIO\uff08New I\/O\uff09\u81ea Java 1.4 &#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-15922","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/posts\/15922","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/comments?post=15922"}],"version-history":[{"count":0,"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/posts\/15922\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/media?parent=15922"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/categories?post=15922"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/tags?post=15922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}