IO和NIO的区别:
IO:通过流处理数据,仅支持阻塞IO。
核心组件:**InputStream /OutputStream用于字节的读写,Reader / Writer:用于字符流的读写。**读取过程中无法被中断,是阻塞式IO。NIO:通过管道处理数据,支持阻塞IO和非阻塞IO。
核心组件:**Channel通道、Buffer缓冲区、Selector选择器。**Channel与Buffer做直接交互,用于数据的传输,支持非阻塞IO,Buffer用于存放数据,Selector用于管理多个管道,允许单个线程处理多个IO。
Channel与Buffer联调
阻塞IO案例
ServerSocketChannel.accept():该方法用于等待客户端连接,直到有客户端连接才会返回,如果没有客户端连接则会一直阻塞。
SocketChannel.read(ByteBuffer):改方法用于接收客户端的数据,直到有数据才会返回,否则将一直阻塞线程。
javapackage com.jiawa.netty.server; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.util.List; import static com.jiawa.netty.utils.ByteBufferUtil.debugAll; public class Server { private static final Logger logger = LoggerFactory.getLogger(Server.class); public static void main(String[] args) throws IOException { //创建缓存、缓存客户端信息 ByteBuffer buffer = ByteBuffer.allocate(16); //创建服务器 ServerSocketChannel server = ServerSocketChannel.open(); //绑定端口 server.bind(new InetSocketAddress(8080)); //创建客户端存储集合 List<SocketChannel> clients = new java.util.ArrayList<>(); //接收客户端 while (true) { //阻塞线程,直到有客户端请求连接后释放 SocketChannel client = server.accept(); //如果有客户端请求,则返回客户端信息 logger.info("收到客户端请求:{}", client); clients.add(client); for (SocketChannel c : clients) { //读取客户端数据 int read = c.read(buffer); if (read > 0) { logger.info("读取客户端数据:{}", read); //打开读 buffer.flip(); //打印数据 debugAll(buffer); //清空 buffer.clear(); } } } } }
非阻塞IO案例
server.configureBlocking(false):将ServerSocketChannel设置为非阻塞模式。
client.configureBlocking(false):将每个SocketChannel设置为非阻塞模式。设置为非阻塞之后,则不会阻塞线程,不管是否有客户端连接或者接收客户端数据,都会直接返回,不会阻塞线程。
javapackage com.jiawa.netty.server; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.util.List; import static com.jiawa.netty.utils.ByteBufferUtil.debugAll; public class Server { private static final Logger logger = LoggerFactory.getLogger(Server.class); public static void main(String[] args) throws IOException { //创建缓存、缓存客户端信息 ByteBuffer buffer = ByteBuffer.allocate(16); //创建服务器 ServerSocketChannel server = ServerSocketChannel.open(); //非阻塞 if (server!= null){ server.configureBlocking(false); } //绑定端口 server.bind(new InetSocketAddress(8080)); //创建客户端存储集合 List<SocketChannel> clients = new java.util.ArrayList<>(); //接收客户端 while (true) { //阻塞线程,直到有客户端请求连接后释放 SocketChannel client = server.accept(); //将客户端设置为非阻塞 if (client != null) { client.configureBlocking(false); } //如果有客户端请求,则返回客户端信息 if (client != null) { logger.info("收到客户端请求:{}", client); clients.add(client); } for (SocketChannel c : clients) { //读取客户端数据 int read = c.read(buffer); if (read > 0){ logger.info("读取客户端数据:{}", read); //打开读 buffer.flip(); //打印数据 debugAll(buffer); //清空 buffer.clear(); } } } } }
NIO入门
小汤猿人类2025-03-25 16:22
相关推荐
Highcharts.js12 小时前
成分占比饼图代码|常用可视化图表开发示例蓝博AI13 小时前
2027基于yolov8飞机蒙皮缺陷检测,支持图像,视频和摄像实时检测,python源码西柚小萌新13 小时前
【人工智能:LangGraph】--3.控制流(Edge)金銀銅鐵14 小时前
带您看懂字节码指令(八):数组里有什么郝学胜-神的一滴15 小时前
游戏设计、原型与开发 26:四种循环r_oo_ki_e_15 小时前
牛牛与切割机Rain的Java大神之路16 小时前
Spring Boot 3 升级实战:真正吃掉工期的,是 MyBatis-Plus 坐标和 Security 6 重写泡海椒16 小时前
Java PDF 报表开发:一页简历的两种版式风格对比与折行避坑2601_9628857216 小时前
如何用 Python 计算终极振荡器(Ultimate Oscillator, UOS)?wuminyu16 小时前
OpenJDK学习笔记:Native事件队列与Java回调背压