概念
NIO中非阻塞IO采用了基于Reactor的模式。会在Selector中注册某个事件,然后监听通道Channel,当有事件到达才对该事件进行处理。也就是延迟IO
核心Selectors、channel、buffers
1. channel
类比InputStream、OutputStream。是一个层面,面对流的,只不过后者channel是双向的,即可写又可读,所以才是通道。
并且双向的前提是,基于随机访问文件RandomAccessFile可读可写访问指针。
在Java中Channel的实现
package java.nio.channels;
import java.io.IOException;
import java.io.Closeable;
public interface Channel extends Closeable {
public boolean isOpen();
public void close() throws IOException;
}
channel不能直接传输数据,需要依赖buffer缓存区实现。
重要的实现类:
- FileChannel 从文件中读写数据
- DatagramChannel,使用UDP从网络中读写数据
- SocketChannel,TCP
- ServerSocketChannel,监听TCP连接,为每一个连接创建一个3
//示例:读取文件
public static void main(String[] args) throws Exception {
RandomAccessFile file = new RandomAccessFile("D:\\makedowm笔记\\服务器部署有关.txt", "r");//只读模式打开
FileChannel channel = file.getChannel();
ByteBuffer allocate = ByteBuffer.allocate(1024);
int read = channel.read(allocate);
long l = System.currentTimeMillis();
System.out.println("开始时间:" + l);
while (read != -1) {
System.out.println("读取了:" + read);
allocate.flip();
System.out.println(new String(allocate.array(), StandardCharsets.UTF_8));
allocate.clear();
read = channel.read(allocate);
}
channel.close();
System.out.println("over");
long l1 = System.currentTimeMillis();
System.out.println("结束时间:" + l1);
System.out.println("用时:" + (l1 - l) + "ms");
}
//示例:写文件
public static void main(String[] args) throws Exception {
RandomAccessFile file = new RandomAccessFile("D:\\makedowm笔记\\a.txt", "rw");
FileChannel channel = file.getChannel();
ByteBuffer allocate = ByteBuffer.allocate(1024);
String data = "123abc";
allocate.clear();
allocate.put(data.getBytes(StandardCharsets.UTF_8));
allocate.flip();
while(allocate.hasRemaining()) {
channel.write(allocate);
}
channel.close();
}
package nio;
//示例:接收TCP连接
public static void main(String[] args) throws Exception {
ServerSocketChannel open = ServerSocketChannel.open();
open.bind(new InetSocketAddress(8888));
open.configureBlocking(false);
while(true) {
System.out.println("等待");
SocketChannel accept = open.accept();
if(accept == null) {
System.out.println("无连接");
Thread.sleep(2000);
} else {
System.out.println(accept.socket().getRemoteSocketAddress());
accept.close();
}
}
}
接下来是buffer缓存区的学习。