上海品牌网站建设公司排名,网站统计怎么做,网站排行首页怎么做,网络销售工作怎么样nio三大组件
channel#xff0c;buffer#xff0c;selector
channel为双向输入输出通道#xff0c;buffer为缓存#xff0c;selector为选择器#xff0c;通过selector来选择线程对出现io操作的channel服务#xff0c;可有有效的增加线程的工作效率#xff0c;不用等待…nio三大组件
channelbufferselector
channel为双向输入输出通道buffer为缓存selector为选择器通过selector来选择线程对出现io操作的channel服务可有有效的增加线程的工作效率不用等待某个连接断开才释放线程 bytebuffer //申请空间ByteBuffer byteBuffer ByteBuffer.allocate(100);//获取通道FileChannel channel new FileInputStream().getChannel();//循环读取while (true){int read channel.read(byteBuffer);if (read -1) break;//切换读模式byteBuffer.flip();while (byteBuffer.hasRemaining()){byte b byteBuffer.get();}//切换写模式byteBuffer.clear();}
buffer写入数据可以使用
调用 channel 的 read 调用 buffer 自己的 put
buffer读数据有write和get
可以通过这样的方式填充多个bufferabc为三个buffer对象
channel.read(new ByteBuffer[]{a, b, c});
同理也可以使用put和write读出写入channel