Java NIO(New IO)是一个可以替代标准 Java IO 的 API(从Java 1.4开始),提供了与标准IO 不同的工作方式。
Java NIO: Channels and Buffers(通道和缓冲区)
标准的 IO 基于字节流和字符流进行操作的,而NIO是基于通道(Channel)和缓冲区(Buffer)进行操作,数据总是从通道读取到缓冲区中,或者从缓冲区写入到通道中。
Java NIO: Non-blocking IO(非阻塞IO)
Java NIO 可以让你非阻塞的使用 IO,例如:当线程从通道(Channel)读取数据到缓冲区时(Buffer),线程还是可以进行其他事情。当数据被写入到缓冲区时,线程可以继续处理它。从缓冲区写入通道也类似。
Java NIO: Selectors(选择器)
Java NIO引入了选择器(Selector)的概念,选择器用于监听多个通道(Channel)的事件(比如:连接打开,数据到达)。因此,单个的线程可以监听多个数据通道(Channel)。
Linux epoll 的水平触发模式
- 对于读操作 只要缓冲内容不为空,LT模式返回读就绪。
- 对于写操作 只要缓冲区还不满,LT模式会返回写就绪。
所以,Linux epoll的水平触发是以缓冲区空满状态来判断的。
测试
import java.net.InetSocketAddress;
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.concurrent.TimeUnit;
/**
* NIO 水平触发测试
* 验证java nio水平触发的办法是客户端写多个字节(比如1000个),服务端每次都不读取字节,缓冲区一直没读完,处于非空状态。由于水平触发,读事件应当会一直触发。
*
* @author N3verL4nd
* @date 2021/7/20
*/
public class NioLevelTriggerServer {
public static void main(String[] argv) throws Exception {
Selector selector = Selector.open();
ServerSocketChannel server = ServerSocketChannel.open();
server.bind(new InetSocketAddress("0.0.0.0", 8888));
server.configureBlocking(false);
server.register(selector, SelectionKey.OP_ACCEPT);
while (true) {
int n = selector.select();
if (n == 0) {
continue;
}
System.out.println("Select " + n);
Iterator<SelectionKey> keys = selector.selectedKeys().iterator();
while (keys.hasNext()) {
SelectionKey key = keys.next();
keys.remove();
if (key.isAcceptable()) {
ServerSocketChannel channel = (ServerSocketChannel) key.channel();
SocketChannel accept = channel.accept();
accept.configureBlocking(false);
accept.register(selector, SelectionKey.OP_READ);
} else if (key.isReadable()) {
SocketChannel channel = (SocketChannel) key.channel();
// 不读取任何数据,缓冲区始终不为空
}
}
System.out.println("休眠一秒, 减缓输出, 便于观察");
TimeUnit.SECONDS.sleep(1);
}
}
}
或者使用如下脚本
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
/**
* @author N3verL4nd
* @date 2021/7/20
*/
public class NioLevelTriggerClient {
public static void main(String[] args) throws IOException {
SocketChannel socketChannel = SocketChannel.open();
InetAddress ia = InetAddress.getLocalHost();
InetSocketAddress isa = new InetSocketAddress(ia, 8888);
socketChannel.connect(isa);
socketChannel.configureBlocking(false);
System.out.println("与服务器的连接建立成功!");
socketChannel.write(StandardCharsets.UTF_8.encode("hello world"));
socketChannel.close();
}
}
所以,Java nio 是水平触发。
linux epoll 默认也是水平触发。
#include <unistd.h>
#include <stdio.h>
#include <sys/epoll.h>
int main()
{
int epfd, nfds;
char buf[1];
struct epoll_event event, events[5];
epfd = epoll_create(1);
event.data.fd = STDIN_FILENO;
event.events = EPOLLIN; // LT是默认模式
epoll_ctl(epfd, EPOLL_CTL_ADD, STDIN_FILENO, &event);
while (1) {
nfds = epoll_wait(epfd, events, 5, -1);
int i;
for (i = 0; i < nfds; ++i) {
if (events[i].data.fd == STDIN_FILENO) {
printf("hello world\n");
}
}
}
}
会一直输出 hello world。
—
http://tutorials.jenkov.com/java-nio/index.html
http://ifeve.com/java-nio-all/
https://tech.meituan.com/2016/11/04/nio.html
https://www.zhihu.com/question/29005375
https://zhuanlan.zhihu.com/p/25701512
https://zhuanlan.zhihu.com/p/25914350
https://zhuanlan.zhihu.com/p/26243285