bio与nio与多路复用(补充)

bio在发送请求后就立刻阻塞自己,等待系统层的返回结果

nio发送请求后不会进入阻塞,但是因为请求一版都是配合while(true)实现,所以nio会不断在用户层和系统层间切换查询结果,知道系统层将结果传给用户层,这样虽然不像bio一样一旦被阻塞只能等待当前事件完成才能执行别的事件,但频繁的在两个层级间切换开销大,性能不高。

多路复用模式采用两者结合,在没有事件触发时会阻塞等待,当有事件触发后则会执行事件,这样不仅避免了bio在等待数据时的浪费效率,又避免了nio在等待事件时的不断切换带来的性能开销。

因为java不具备读写的能力需要调用内核态的读写功能

读:磁盘->内核缓冲区->用户缓冲区

写:用户缓冲区->socket缓冲区->网卡

加粗的过程都涉及用户态和内核态间的切换,每个过程都涉及到数据的复制

ByteBuffer.allocate():使用的是java的内存。

ByteBuffer.allocateDirect():使用的是操作系统的内存,这样可以避免一次内核缓冲区到用户缓冲区的数据复制。

除了对缓冲区的优化还有对数据转移的优化:

java的transferTo的方法可以不将数据拷贝到java内存中直接拷贝到socket缓冲区,减少一次内核态到用户态的切换。

在之后的优化中可以将数据直接拷贝到网卡中,只有少量数据经由socket缓冲区,又减少一次数据的拷贝

 

参考内容中未直接给出BIONIO、IO多路复用的代码示例。不过可知Java已将多路复用器用Selector类封装起来,可基于Selector进行NIO服务器开发,且Netty是优秀的NIO框架 [^2][^4]。 下面给出简单的BIONIO的代码示例: ### BIO代码示例 ```java import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; public class BIOServer { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(8888); System.out.println("服务器启动,监听端口 8888"); while (true) { Socket socket = serverSocket.accept(); System.out.println("有客户端连接"); InputStream inputStream = socket.getInputStream(); byte[] bytes = new byte[1024]; int read; while ((read = inputStream.read(bytes)) != -1) { System.out.println(new String(bytes, 0, read)); } inputStream.close(); socket.close(); } } } ``` ### NIO代码示例 ```java import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.util.Iterator; import java.util.Set; public class NIOServer { public static void main(String[] args) throws IOException { ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.socket().bind(new InetSocketAddress(8888)); serverSocketChannel.configureBlocking(false); Selector selector = Selector.open(); serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); while (true) { int readyChannels = selector.select(); if (readyChannels == 0) continue; Set<SelectionKey> selectedKeys = selector.selectedKeys(); Iterator<SelectionKey> keyIterator = selectedKeys.iterator(); while (keyIterator.hasNext()) { SelectionKey key = keyIterator.next(); if (key.isAcceptable()) { SocketChannel socketChannel = serverSocketChannel.accept(); socketChannel.configureBlocking(false); socketChannel.register(selector, SelectionKey.OP_READ); } else if (key.isReadable()) { SocketChannel socketChannel = (SocketChannel) key.channel(); ByteBuffer buffer = ByteBuffer.allocate(1024); int bytesRead = socketChannel.read(buffer); if (bytesRead > 0) { buffer.flip(); byte[] bytes = new byte[buffer.remaining()]; buffer.get(bytes); System.out.println(new String(bytes)); } } keyIterator.remove(); } } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值