public class BufferDemo {
public static void main(String[] args) {
// 构建一个 byte 字节缓冲区,容量是 4
// ByteBuffer byteBuffer = ByteBuffer.allocate(4);
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4);
// 默认写入模式,查看三个重要指标
System.out.println(String.format("初始化,capacity 容量:%s,position 位置:%s,limit 限制:%s",
byteBuffer.capacity(), byteBuffer.position(), byteBuffer.limit()));
// 写入三字节的数据
byteBuffer.put((byte) 1);
byteBuffer.put((byte) 2);
byteBuffer.put((byte) 3);
// 再看数据
System.out.println(String.format("写入 3 字节后,capacity 容量:%s,position 位置:%s,limit 限制:%s",
byteBuffer.capacity(), byteBuffer.position(), byteBuffer.limit()));
// 转换为读取模式(不调用 flip 方法,也是可以读取数据的,但是 posiition 记录读取的位置不变)
System.out.println("#### 开始读取 ####");
byteBuffer.flip();
byte a = byteBuffer.get();
System.out.println(a);
byte b = byteBuffer.get();
System.out.println(b);
System.out.println(String.format("读取 2 字节数据后,capacity 容量:%s,position 位置:%s,limit 限制:%s",
byteBuffer.capacity(), byteBuffer.position(), byteBuffer.limit()));
// 继续写入三字节,此时读模式下,limit=3,position=2,继续写入只能覆盖写入一条数据
// clean() 方法清除整个缓冲区。compact() 方法仅清除已阅读的数据。转为写入模式
byteBuffer.compact();
byteBuffer.put((byte) 3);
byteBuffer.put((byte) 4);
byteBuffer.put((byte) 5);
System.out.println(String.format("最终的情况,capacity 容量:%s,position 位置:%s,limit 限制:%s",
byteBuffer.capacity(), byteBuffer.position(), byteBuffer.limit()));
// rewind() 重置 position 位置为 0
// mark() 标记 position 的位置
// reset() 重置 position 为上次 mark() 标记的位置
}
}
public class NIOClient {
public static void main(String[] args) throws IOException {
SocketChannel socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
socketChannel.connect(new InetSocketAddress("127.0.0.1", 8080));
while (! socketChannel.finishConnect()) {
// 没连上,则一直等待
Thread.yield();
}
Scanner scanner = new Scanner(System.in);
System.out.println("请输入:");
// 发送内容
String msg = scanner.nextLine();
ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes());
while (buffer.hasRemaining()) {
socketChannel.write(buffer);
}
// 读取响应
System.out.println("收到服务器端响应...");
ByteBuffer requestBuffer = ByteBuffer.allocate(1024);
while (socketChannel.isOpen() && socketChannel.read(requestBuffer) != -1) {
// 长连接情况下,需要手动判断数据有没有读取结束(此处做一个简单的判断,超过 0 字节就认为请求结束了)
if (requestBuffer.position() > 0) break;
}
requestBuffer.flip();
byte[] content = new byte[requestBuffer.limit()];
requestBuffer.get(content);
System.out.println(new String(content));
scanner.close();
socketChannel.close();
}
}
/**
* 直接基于非阻塞的写法
*/
public class NIOServer1 {
public static void main(String[] args) throws IOException {
// 创建网络服务器
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false); // 设置为非阻塞模式
serverSocketChannel.socket().bind(new InetSocketAddress(8080)); // 绑定端口
System.out.println("启动成功");
while (true) {
SocketChannel socketChannel = serverSocketChannel.accept(); // 获取新 tcp 连接通道
// tcp 请求 读取/响应
if (socketChannel != null) {
System.out.println("收到新连接:" + socketChannel.getRemoteAddress());
socketChannel.configureBlocking(false); // 默认阻塞,设置为非阻塞
ByteBuffer requestBuffer = ByteBuffer.allocate(1024);
while (socketChannel.isOpen() && socketChannel.read(requestBuffer) != -1) {
// 长连接情况下,需要手动判断有没有读取结束(此处做一个简单判断,超过 0 字节就认为请求结束)
if (requestBuffer.position() > 0) break;
}
if (requestBuffer.position() == 0) continue; // 如果没数据了,则不继续之后的处理
requestBuffer.flip();
byte[] content = new byte[requestBuffer.limit()];
requestBuffer.get(content);
System.out.println(new String(content));
System.out.println("收到数据,来自:" + socketChannel.getRemoteAddress());
// 响应结果 200
String response = "HTTP/1.1 200 OK\r\n" +
"Content-Length: 11\r\n\r\n" +
"Hello World";
ByteBuffer buffer = ByteBuffer.wrap(response.getBytes());
while (buffer.hasRemaining()) {
socketChannel.write(buffer); // 非阻塞
}
}
}
}
}
/**
* 直接基于非阻塞的写法,一个线程轮询处理所有请求
*/
public class NIOServer2 {
// 已经建立连接的集合
private static ArrayList<SocketChannel> channels = new ArrayList<>();
public static void main(String[] args) throws IOException {
// 创建网络客户端
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false);
serverSocketChannel.socket().bind(new InetSocketAddress(8080));
System.out.println("启动成功");
while (true) {
SocketChannel socketChannel = serverSocketChannel.accept();
if (socketChannel != null) {
System.out.println("收到新连接:" + socketChannel.getRemoteAddress());
socketChannel.configureBlocking(false);
channels.add(socketChannel);
} else {
// 没有新连接的情况下,就去处理现有的连接的数据,处理完就删除掉
Iterator<SocketChannel> iterator = channels.iterator();
while (iterator.hasNext()) {
SocketChannel ch = iterator.next();
ByteBuffer requestBuffer = ByteBuffer.allocate(1024);
if (ch.read(requestBuffer) == 0) {
// 等于 0,代表这个通道没有数据需要处理,那就等会再处理
continue;
}
while (ch.isOpen() && ch.read(requestBuffer) != -1) {
if (requestBuffer.position() > 0) break;
}
if (requestBuffer.position() == 0) continue;
requestBuffer.flip();
byte[] content = new byte[requestBuffer.limit()];
requestBuffer.get(content);
System.out.println(new String(content));
System.out.println("收到数据,来自:" + ch.getRemoteAddress());
String response = response = "HTTP/1.1 200 OK\r\n" +
"Content-Length: 11\r\n\r\n" +
"Hello World";
ByteBuffer buffer = ByteBuffer.wrap(response.getBytes());
while (buffer.hasRemaining()) {
ch.write(buffer);
}
iterator.remove();
}
}
}
}
}
/**
* 结合 Selector 实现的非阻塞服务器(放弃对 Channel 的轮询,借助消息通知机制)
*/
public class NIOServer3 {
public static void main(String[] args) throws IOException {
// 1. 创建网络服务器
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false); // 设置非阻塞模式
// 2. 构建一个 Selector 选择器,并且将 Channel 注册上去
Selector selector = Selector.open();
SelectionKey selectionKey = serverSocketChannel.register(selector, 0, serverSocketChannel); // 将 ServerSocketChannel 注册到 Selector
selectionKey.interestOps(SelectionKey.OP_ACCEPT); // 对 ServerSocketChannel 上面的 accept 事件感兴趣(ServerSocketChannel 只支持 accept 操作)
// 3. 绑定端口
serverSocketChannel.socket().bind(new InetSocketAddress(8080));
System.out.println("启动成功");
while (true) {
// 不再轮询通过,改用下面轮询事件的方式,select() 方法有阻塞作用,直到有事件通知才会有返回。
selector.select();
// 获取事件
Set<SelectionKey> selectionKeys = selector.selectedKeys();
// 遍历查询结果
Iterator<SelectionKey> iter = selectionKeys.iterator();
while (iter.hasNext()) {
// 被封装的查询结果
SelectionKey key = iter.next();
iter.remove();
// 关注 Read 和 Accept 两个事件
if (key.isAcceptable()) {
ServerSocketChannel server = (ServerSocketChannel) key.attachment();
// 将拿到的客户端连接到通道,注册到 Selector 上面
SocketChannel clientSocketChannel = server.accept();
clientSocketChannel.configureBlocking(false);
clientSocketChannel.register(selector, SelectionKey.OP_READ, clientSocketChannel);
System.out.println("收到新连接:" + clientSocketChannel.getRemoteAddress());
}
if (key.isReadable()) {
SocketChannel socketChannel = (SocketChannel) key.attachment();
ByteBuffer requestBuffer = ByteBuffer.allocate(1024);
while (socketChannel.isOpen() && socketChannel.read(requestBuffer) != -1) {
if (requestBuffer.position() > 0) break;
}
if (requestBuffer.position() == 0) continue;
requestBuffer.flip();
byte[] content = new byte[requestBuffer.limit()];
requestBuffer.get(content);
System.out.println(new String(content));
System.out.println("收到数据,来自:" + socketChannel.getRemoteAddress());
// 响应结果 200
String response = "HTTP/1.1 200 OK\r\n" +
"Content-Length: 11\r\n\r\n" +
"Hello World";
ByteBuffer buffer = ByteBuffer.wrap(response.getBytes());
while (buffer.hasRemaining()) {
socketChannel.write(buffer);
}
}
}
selector.selectNow();
}
}
}
/**
* NIO Selector 多路复用 Reactor 线程模型
*/
public class NIOServer4 {
// 处理业务操作的线程
private static ExecutorService workPool = Executors.newCachedThreadPool();
// 封装了 Selector.select 等事件轮询的代码
abstract class ReactorThread extends Thread {
Selector selector;
LinkedBlockingQueue<Runnable> taskQueue = new LinkedBlockingQueue<>();
/**
* Selector 监听到事件后,调用这个方法
*/
public abstract void handler(SelectableChannel channel) throws Exception;
private ReactorThread() throws IOException {
this.selector = Selector.open();
}
volatile boolean running = false;
@Override
public void run() {
// 轮询 Selector 事件
while (running) {
try {
// 执行队列中的任务
Runnable task;
while ((task = taskQueue.poll()) != null) {
task.run();
}
selector.select(1000);
// 获取查询结果
Set<SelectionKey> selected = selector.selectedKeys();
// 遍历查询结果
Iterator<SelectionKey> iter = selected.iterator();
while (iter.hasNext()) {
SelectionKey key = iter.next();
iter.remove();
int readyOps = key.readyOps();
// 关注 Read 和 Accept 两个事件
if ((readyOps & (SelectionKey.OP_READ | SelectionKey.OP_ACCEPT)) != 0 || readyOps == 0) {
SelectableChannel channel = (SelectableChannel) key.attachment();
channel.configureBlocking(false);
handler(channel);
if (! channel.isOpen()) {
key.cancel(); // 如果关闭了,就取消这个 key 的订阅
}
}
}
selector.selectNow();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private SelectionKey register(SelectableChannel channel) throws Exception {
// 为什么 register 要以任务提交的形式,让 reactor 线程去处理?
// 因为线程在执行 channel 注册到 selector 的过程中,会和调用 selector.select() 方法的线程争用同一把锁
// 而 select() 方法实在 eventLoop 中通过 while 循环调用的,争抢的可能性很高,为了让 register 能更快的执行,就放到同一个线程来处理
FutureTask<SelectionKey> futureTask = new FutureTask<>(() -> channel.register(selector, 0, channel));
taskQueue.add(futureTask);
return futureTask.get();
}
private void doStart() {
if (!running) {
running = true;
start();
}
}
}
private ServerSocketChannel serverSocketChannel;
// 1、创建多个线程 - accept处理reactor线程 (accept线程)
private ReactorThread[] mainReactorThreads = new ReactorThread[1];
// 2、创建多个线程 - io处理reactor线程 (I/O线程)
private ReactorThread[] subReactorThreads = new ReactorThread[8];
/**
* 初始化线程组
*/
private void newGroup() throws IOException {
// 创建IO线程,负责处理客户端连接以后socketChannel的IO读写
for (int i = 0; i < subReactorThreads.length; i++) {
subReactorThreads[i] = new ReactorThread() {
@Override
public void handler(SelectableChannel channel) throws IOException {
// work线程只负责处理IO处理,不处理accept事件
SocketChannel ch = (SocketChannel) channel;
ByteBuffer requestBuffer = ByteBuffer.allocate(1024);
while (ch.isOpen() && ch.read(requestBuffer) != -1) {
// 长连接情况下,需要手动判断数据有没有读取结束 (此处做一个简单的判断: 超过0字节就认为请求结束了)
if (requestBuffer.position() > 0) break;
}
if (requestBuffer.position() == 0) return; // 如果没数据了, 则不继续后面的处理
requestBuffer.flip();
byte[] content = new byte[requestBuffer.limit()];
requestBuffer.get(content);
System.out.println(new String(content));
System.out.println(Thread.currentThread().getName() + "收到数据,来自:" + ch.getRemoteAddress());
// TODO 业务操作 数据库、接口...
workPool.submit(() -> {
});
// 响应结果 200
String response = "HTTP/1.1 200 OK\r\n" +
"Content-Length: 11\r\n\r\n" +
"Hello World";
ByteBuffer buffer = ByteBuffer.wrap(response.getBytes());
while (buffer.hasRemaining()) {
ch.write(buffer);
}
}
};
}
// 创建mainReactor线程, 只负责处理serverSocketChannel
for (int i = 0; i < mainReactorThreads.length; i++) {
mainReactorThreads[i] = new ReactorThread() {
AtomicInteger incr = new AtomicInteger(0);
@Override
public void handler(SelectableChannel channel) throws Exception {
// 只做请求分发,不做具体的数据读取
ServerSocketChannel ch = (ServerSocketChannel) channel;
SocketChannel socketChannel = ch.accept();
socketChannel.configureBlocking(false);
// 收到连接建立的通知之后,分发给I/O线程继续去读取数据
int index = incr.getAndIncrement() % subReactorThreads.length;
ReactorThread workEventLoop = subReactorThreads[index];
workEventLoop.doStart();
SelectionKey selectionKey = workEventLoop.register(socketChannel);
selectionKey.interestOps(SelectionKey.OP_READ);
System.out.println(Thread.currentThread().getName() + "收到新连接 : " + socketChannel.getRemoteAddress());
}
};
}
}
/**
* 初始化channel,并且绑定一个eventLoop线程
*
* @throws IOException IO异常
*/
private void initAndRegister() throws Exception {
// 1、 创建ServerSocketChannel
serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false);
// 2、 将serverSocketChannel注册到selector
int index = new Random().nextInt(mainReactorThreads.length);
mainReactorThreads[index].doStart();
SelectionKey selectionKey = mainReactorThreads[index].register(serverSocketChannel);
selectionKey.interestOps(SelectionKey.OP_ACCEPT);
}
/**
* 绑定端口
*
* @throws IOException IO异常
*/
private void bind() throws IOException {
// 1、 正式绑定端口,对外服务
serverSocketChannel.bind(new InetSocketAddress(8080));
System.out.println("启动完成,端口8080");
}
public static void main(String[] args) throws Exception {
NIOServer4 nioServer = new NIOServer4();
nioServer.newGroup(); // 1、 创建main和sub两组线程
nioServer.initAndRegister(); // 2、 创建serverSocketChannel,注册到mainReactor线程上的selector上
nioServer.bind(); // 3、 为serverSocketChannel绑定端口
}
}
public class MyNIOHttpServer {
public static Selector selector;
// 定义线程池
public static final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(25, 25, 25,
TimeUnit.SECONDS, new LinkedBlockingQueue<>());
private static ServerSocketChannel serverSocketChannel;
private static final int port = 8080;
public static void main(String[] args) throws IOException {
serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false);
serverSocketChannel.socket().bind(new InetSocketAddress(port));
System.out.println("NIO 启动:" + port);
// 获取一个选择器
// 底层的事件通知机制
MyNIOHttpServer.selector = Selector.open();
// 登记:表示对这个通道上的 OP_ACCEPT 事件感兴趣,并且返回一个标记
// 此处表示,希望收到 socket 通道 8080 端口上建立连接这个通知
SelectionKey selectionKey = serverSocketChannel.register(MyNIOHttpServer.selector, 0);
selectionKey.interestOps(SelectionKey.OP_ACCEPT);
while (true) {
// 如果没有新的 socket 与服务器连接或者数据交互,这里会等待 1 秒
MyNIOHttpServer.selector.select(1000);
// 开始处理
Set<SelectionKey> selected = MyNIOHttpServer.selector.selectedKeys();
Iterator<SelectionKey> iter = selected.iterator();
while (iter.hasNext()) {
// 获取注册在上面标记
SelectionKey key = iter.next();
if (key.isAcceptable()) {
System.out.println("有新的连接,当前线程数量:" + MyNIOHttpServer.threadPoolExecutor.getActiveCount());
SocketChannel chan = serverSocketChannel.accept();
chan.configureBlocking(false);
// 注册一个新监听
// 表示希望收到该连接上 OP_READ 数据传输事件的通知
chan.register(MyNIOHttpServer.selector, SelectionKey.OP_READ);
} else if (key.isReadable()) {
// 取出附着在上面的信息,也就是上面代码中附着的连接信息
SocketChannel socketChannel = (SocketChannel) key.channel();
// 处理中,不需要收到任何通知
key.cancel();
socketChannel.configureBlocking(false);
MyNIOHttpServer.threadPoolExecutor.execute(() -> {
try {
// 读取里面的内容,请注意,此处大小写随意
// tomcat 中会根据 http 协议中定义的长度来读取数据,或者一直读到通道内无数据为止
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
socketChannel.read(byteBuffer);
byteBuffer.flip(); // 转为读模式
String request = new String(byteBuffer.array());
System.out.println("收到新数据,当前线程数:" + MyNIOHttpServer.threadPoolExecutor.getActiveCount()
+ ",请求内容:" + request);
// 给一个当前时间作为返回值
// 随意返回,不是 http 的协议
byteBuffer.clear();
ByteBuffer wrap = ByteBuffer.wrap(("hello" + System.currentTimeMillis()).getBytes());
socketChannel.write(wrap);
wrap.clear();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " 服务器线程处理完毕,当前线程数:"
+ threadPoolExecutor.getActiveCount());
});
}
// 取出后删除
iter.remove();
}
selected.clear();
// 过掉cancelled keys
MyNIOHttpServer.selector.selectNow();
}
}
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有