Java提供了哪些IO方式❓NIO如何实现多路复用❓

I/O 方式🍑

1)同步阻塞 I/O( BIO )✨

  • 定义与特点:在BIO模型中,当一个线程执行I/O操作如读写数据时,该线程会被阻塞,直到I/O操作完成。这意味着在I/O操作进行期间,该线程不能执行其他任务。
  • 实现:主要通过java.io包实现,如使用FileInputStream和FileOutputStream进行文件操作,或者使用Socket进行网络操作时,都是采用同步阻塞的方式。

2)同步非阻塞I/O(NIO)✨

  • 定义与特点:NIO 允许一个线程从多个通道(Channel)读写数据,通过使用选择器(Selector)实现多路复用。这种方式下,线程可以在等待某个通道准备好进行 I/O 操作时,同时检查其他通道的就绪状态,而不是阻塞在单个 I/O 操作上。
  • 实现:通过 java.nio 包实现,核心概念包括通道(Channel)、缓冲区(Buffer)、选择器(Selector)等。Selector 可以监控多个通道的 I/O 状态(如读、写就绪),实现了同步非阻塞 I/O。
  • 多路复用实现:通过 Selector,一个单独的线程可以管理多个通道,当通道准备好进行 I/O 操作时,Selector 能够检测到并允许线程处理这些事件。应用程序通过注册通道到 Selector 并指定感兴趣的事件(如读、写),Selector 轮询注册的通道,当通道就绪时,线程即可处理这些事件,实现了高效的多路复用。

3)异步非阻塞I/O(AIO/NIO.2)✨

  • 定义与特点:AIO 引入了真正的异步非阻塞 I/O 操作,应用程序可以直接返回,不需要等待 I/O 操作完成。I/O 操作完成后,系统会自动通知应用程序,应用程序再进行相应的处理。
  • 实现:通过java.nio.channels包下的异步通道(如 AsynchronousFileChannel、AsynchronousSocketChannel)实现。AIO 提供了基于事件和回调机制的 I/O 操作,极大提高了程序的性能和响应速度。

NIO如何实现多路复用❓

1)创建一个 Selector 实例🐾

Selector 是一个多路复用器,可以监控多个通道的 I/O 事件(如连接请求、数据到达等)。
Selectorselector= Selector.open();

2)配置非阻塞模式🍑

channel.configureBlocking(false);

3)注册通道到 Selector 🚆

channel.register(selector, SelectionKey.OP_READ);

4)选择就绪的通道🍒

int readyChannels= selector.select();

5)处理就绪的事件🚀

Set<SelectionKey> selectedKeys = selector.selectedKeys(); 
Iterator<SelectionKey> keyIterator = selectedKeys.iterator(); 
while(keyIterator.hasNext()) { 
	SelectionKeykey= keyIterator.next(); 
	if(key.isAcceptable()) { // 处理接受事件 } 
	elseif (key.isReadable()) { // 处理读事件} 
	keyIterator.remove(); 
}
  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是基于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 NioMultiplexingServer { private Selector selector; public void init(int port) throws IOException { // 创建selector selector = Selector.open(); // 创建ServerSocketChannel ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.configureBlocking(false); // 绑定端口 serverSocketChannel.socket().bind(new InetSocketAddress(port)); // 将ServerSocketChannel注册到selector上,并监听OP_ACCEPT事件 serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); System.out.println("Server started at port " + port); // 循环处理selector上的事件 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()) { // 处理OP_ACCEPT事件 handleAccept(key); } else if (key.isReadable()) { // 处理OP_READ事件 handleRead(key); } keyIterator.remove(); } } } private void handleAccept(SelectionKey key) throws IOException { // 获取ServerSocketChannel ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel(); // 接受连接 SocketChannel socketChannel = serverSocketChannel.accept(); socketChannel.configureBlocking(false); // 将SocketChannel注册到selector上,并监听OP_READ事件 socketChannel.register(selector, SelectionKey.OP_READ); System.out.println("Connection accepted: " + socketChannel.getRemoteAddress()); } private void handleRead(SelectionKey key) throws IOException { // 获取SocketChannel SocketChannel socketChannel = (SocketChannel) key.channel(); // 读取数据 ByteBuffer buffer = ByteBuffer.allocate(1024); int bytesRead = socketChannel.read(buffer); if (bytesRead == -1) { // 连接已关闭 socketChannel.close(); System.out.println("Connection closed: " + socketChannel.getRemoteAddress()); return; } // 处理数据 buffer.flip(); byte[] bytes = new byte[buffer.remaining()]; buffer.get(bytes); String message = new String(bytes); System.out.println("Message received from " + socketChannel.getRemoteAddress() + ": " + message); // 回复客户端 ByteBuffer responseBuffer = ByteBuffer.wrap(("Received message: " + message).getBytes()); socketChannel.write(responseBuffer); } public static void main(String[] args) throws IOException { NioMultiplexingServer server = new NioMultiplexingServer(); server.init(8080); } } ``` 上述代码实现了一个简单的nio多路复用服务器,其中: 1. 初始化方法中创建了一个Selector和一个ServerSocketChannel,并将ServerSocketChannel注册到selector上,监听OP_ACCEPT事件; 2. 循环处理selector上的事件,阻塞等待事件发生; 3. 对于每个事件,根据事件类型进行处理,处理完毕后从selector的selectedKeys集合中移除该事件。 在handleAccept和handleRead方法中,分别处理了OP_ACCEPT和OP_READ事件: 1. handleAccept方法中,获取ServerSocketChannel并接受连接,将SocketChannel注册到selector上,监听OP_READ事件; 2. handleRead方法中,获取SocketChannel并读取数据,处理数据并回复客户端。 通过以上代码,我们实现了一个简单的nio多路复用服务器,可以同时处理多个连接请求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值