都是NIO是面向缓冲区的非阻塞的io,面向缓冲区倒是好理解,非阻塞到底体现在什么地方?莫不是selector使得NIO是非阻塞的?像下面代码:
RandomAccessFile aFile = new RandomAccessFile("data/nio-data.txt", "rw");
FileChannel inChannel = aFile.getChannel();
ByteBuffer buf = ByteBuffer.allocate(48);
int bytesRead = inChannel.read(buf);
while (bytesRead != -1) {
System.out.println("Read " + bytesRead);
buf.flip();
while(buf.hasRemaining()){
System.out.print((char) buf.get());
}
buf.clear();
bytesRead = inChannel.read(buf);
}
aFile.close();
在int bytesRead = inChannel.read(buf); 的时候岂不是也是阻塞的?
跃然一笑
交互式爱情
慕田峪7331174
相关分类