Socket这个概念没有对应到一个具体的实体,它描述计算机之间完成相互通信的一种抽象功能。打个比方,可以把Socket比作两个城市之间的交通工具,有了它,就可以在城市之间来回穿梭。交通工具有很多种,每种交通工具也有相应的交通规则。Socket也一样,也有多种。大部分情况下我们使用的都是基于TCP/IP协议,它是一种稳定的通信协议。
NIO三大组件
- Channel:任然用交通工具比喻NIO的工作方式,这里的Channel要比Socket更加具体,可以把它比作某种具体的交通工具,如汽车或高铁。
- Selector:Selector好比一个车站的车辆运行调度系统,它将负责监控每辆车的当前状态,是已经出站,还是在路上等。也就是它可以轮询每个Channel的状态。
- Buffer:Buffer好比汽车上面的座位,为channel来使用的,它始终是一个具体的概念,相比Stream,Stream只能代表一个座位,至于是什么座位由你自己想象,也就是在你上车之前并不知道在这个车上是否还有座位,也不知道自己上的什么车,因为你并不能选择。而这些信息都已经被封装在了运输工具(Socket)里面,对你是透明的。
NIO中为何引入这三大组件
NIO引入了Channel、Buffer、Selector,就是想把这些信息具体化,让程序员有机会控制他们。例如,当我们调用write()往SendQ中写数据时,当一次写的的数据超过SendQ长度时需要按照SendQ的长度进行分割,在这个过程中需要将用户空间数据和内核地址空间进行切换,而这个切换不是你可以控制的,但在Buffer中我们可以控制Buffer的容量,是否扩容以及如何扩容。
Buffer工作原理
Buffer三个重要属性:
- capacity容量:作为一个内存块,Buffer具有一定的固定大小,也称为“容量”。
- position位置:写入模式时代表写数据的位置。读取模式时代表读取数据的位置。
- limit限制:写入模式,限制等于buffer的容量。读取模式下limit等于写入的数据量。
// 构建一个byte字节缓冲区,容量是4
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4);
// 默认写入模式,查看三个重要的指标
System.out.println(String.format("初始化:capacity容量:%s, position位置:%s, limit限制:%s", byteBuffer.capacity(),
byteBuffer.position(), byteBuffer.limit()));
// 写入2字节的数据
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方法,也是可以读取数据的,但是position记录读取的位置不对)
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()));
// 继续写入3字节,此时读模式下,limit=3,position=2.继续写入只能覆盖写入一条数据
// clear()方法清除整个缓冲区。compact()方法仅清除已阅读的数据。转为写入模式
byteBuffer.compact(); // buffer : 1 , 3
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()标记的位置
ByteBuffer内存模型
- ByteBuffer为性能关键型代码提供了直接直接内存(direct堆外)和非直接内存(heap堆)两种实现。
//直接内存(direct堆外)
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4);
//非直接内存(heap堆)
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
堆外内存好处:
使用堆外内存进行网络IO或者文件IO时比heapBuffer少一次拷贝。(file/socket----OS memory — jvm heap),如果使用堆内内存的话,在进行写入时,他可能在没写入之前,对象被GC回收掉了导致内存地址变化,写入失败。为了解决写入和GC冲突所以使用堆外内存,JVM的实现中,会先把数据复制到堆外,这样对象的内存地址就不会发生改变,不过堆外内存就没把被jvm的GC管理,GC范围之外,为了降低GC压力,实现了自动管理,DirectByteBuffer中有一个Cleaner对象(PhantomReference软引用),Cleaner被GC前会执行clean方法,触发DirectByteBuffer中定义的Deallocator,这是JDK已经封装好了API,专门用于堆外内存的GC回收。(举例说明:OS memory相当于银行卡一样,jvm heap先把钱打到银行卡上,在把钱转给file/socket,确保了钱数没有发生变化,如果jvm heap用股票来交易的话,可能交易完股票价值会变化,所以这样是不行的,造成变化的原因是GC回收的时候可能会改变内存的地址)
建议:
- 性能确实可观的时候才去使用;分配给大型、长寿(网络传输、文件读写场景)。
- 通过虚拟机参数MaxDirectMemorySize限制大小,防止耗尽整个机器的内存。
Channel通道
- 在BIO中使用的是outputStream、inputStream和socket一起使用实现的数据传输(Io包+Net包)。
- 在NIO中使用了Channel这个API来实现(NIO包),Channel的API覆盖了UDP/TCP网络和文件IO。(常用的SocketChannel和ServerSocketChannel)
区别:BIO中需要用到Stream,在一个通道内进行读取和写入stream通常是单向的,而NIO可以非阻塞读取和写入通道。
下面看具体的代码示例来解析NIO
1.SocketChannel
用于建立TCP网络连接,类似java.net.Socket。有两种创建socketChannel形式:
- 客户端主动发起和服务器的连接。
- 服务器获取的新连接。
//1.客户端主动发起和服务器的连接
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();
write写:write()在尚未写入任何内容时就可能返回了,因为NIO是非阻塞的,会直接返回结果。所以需要在循环中调用write(),加入判断条件。
read读:read()方法可能直接返回而根本不读取任何数据,根据返回的int值判断读取了多少字节。
2.ServerSocketChannel
serverSocketChannel 可以监听新建的TCP连接通道,类似serverSocket。
//服务端版本1
// 创建网络服务端
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); // 默认是阻塞的,一定要设置为非阻塞
try {
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);// 非阻塞
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 用到了非阻塞的API, 在设计上,和BIO可以有很大的不同.继续改进
serverSocketChannel.accept():如果该通道处于非阻塞模式,那么如果没有挂起的连接,该方法将立即返回null。必须检查返回的SocketChannel是否为null。
服务端版本1代码解析:先通过ServerSocketChannel.open() 创建网络服务端,并设置为非阻塞已经绑定好监听的端口,在通过while循环里面的serverSocketChannel.accept()去尝试获取新接入的连接,如果返回的结果不是null的话就成功的获取到了新连接,在把新连接设置为非阻塞模式,在创建一个byteBuffer(相当于channel上的座位,指定一个大小,专门为channel服务的),在用一个while循环用来判断如果当前连接没有关闭,且socketChannel.read(requestBuffer) != -1(= -1的话说明连接已经结束)的情况下,在判断是否有数据写入,有的话就跳出当前while循环,调用flip()转换成读的模式,把结果通过构造协议返回给客户端。
缺点:这里人工实现了一个阻塞(while循环),一次只能获取一个连接,当前连接不写入数据的话,会一直停在第一个while循环,只有当前面的线程写完并且返回给客户端,下面的连接才会进入,单线程太影响效率,相比之前只改进了非阻塞。
3.ServerSocketChannel改进版
/**
*服务端版本2代码解析:
* 直接基于非阻塞的写法,一个线程处理轮询所有请求
*/
public class NIOServer1 {
/**
* 已经建立连接的集合
*/
private static ArrayList<SocketChannel> channels = new ArrayList<>();
public static void main(String[] args) throws Exception {
// 创建网络服务端
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false); // 设置为非阻塞模式
serverSocketChannel.socket().bind(new InetSocketAddress(8080)); // 绑定端口
System.out.println("启动成功");
while (true) {
// 获取新tcp连接通道
SocketChannel socketChannel = serverSocketChannel.accept();
// tcp请求 读取/响应
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();
try {
ByteBuffer requestBuffer = ByteBuffer.allocate(1024);
if (ch.read(requestBuffer) == 0) {
// 等于0,代表这个通道没有数据需要处理,那就待会再处理
continue;
}
while (ch.isOpen() && ch.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("收到数据,来自:" + ch.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()) {
ch.write(buffer);
}
iterator.remove();
} catch (IOException e) {
e.printStackTrace();
iterator.remove();
}
}
}
}
// 用到了非阻塞的API, 再设计上,和BIO可以有很大的不同
// 问题: 轮询通道的方式,低效,浪费CPU
}
服务端版本2代码解析:创建一个ArrayList来存放新连接,不停轮询判断是否有新的连接进来,如果有的话就把新连接加入到ArrayList里面,没有的话就开始去处理现在ArrayList集合里面已有的数据,处理完成就删除掉。用到了非阻塞的API, 再设计上,和BIO可以有很大的不同。
缺点:一个线程处理轮询所有请求,轮询通道的方式,低效,浪费CPU。
Selector选择器
Selector是一个Java NIO组件,可以检查一个或多个NIo通道,并确定哪些通道已准备好进行读取或写入。实现单个线程可以管理多个通道,从而管理多个网络连接。
一个线程使用Selector监听多个channel的不同事件。四个事件分别对应SelectionKey的四个常量:
- Connect连接(SelectionKey.OP_CONNECT)
- Accept准备就绪(OP_ACCEPT)
- Read读取(OP_READ)
- Write写入(OP_WRITE)
//服务端版本3代码
// 1. 创建网络服务端ServerSocketChannel
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false); // 设置为非阻塞模式
// 2. 构建一个Selector选择器,并且将channel注册上去
Selector selector = Selector.open();
// 将serverSocketChannel注册到selector
SelectionKey selectionKey = serverSocketChannel.register(selector, 0, serverSocketChannel);
// 对serverSocketChannel上面的accept事件感兴趣(serverSocketChannel只能支持accept操作)
selectionKey.interestOps(SelectionKey.OP_ACCEPT);
// 3. 绑定端口
serverSocketChannel.socket().bind(new InetSocketAddress(8080));
System.out.println("启动成功");
while (true) {
// 不再轮询通道,改用下面轮询事件的方式.select方法有阻塞效果,直到有事件通知才会有返回
selector.select();
// 获取事件
Set<SelectionKey> selectionKeys = selector.selectedKeys();
// 遍历查询结果e
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(); // mainReactor 轮询accept
clientSocketChannel.configureBlocking(false);
clientSocketChannel.register(selector, SelectionKey.OP_READ, clientSocketChannel);
System.out.println("收到新连接 : " + clientSocketChannel.getRemoteAddress());
}
if (key.isReadable()) {
SocketChannel socketChannel = (SocketChannel) key.attachment();
try {
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());
// TODO 业务操作 数据库 接口调用等等
// 响应结果 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);
}
} catch (IOException e) {
// e.printStackTrace();
key.cancel(); // 取消事件订阅
}
}
}
selector.selectNow();
}
// 问题: 此处一个selector监听所有事件,一个线程处理所有请求事件. 会成为瓶颈! 要有多线程的运用
服务端版本3代码解析:改用消息通知机制,先注册,在selector.select()方法对注册的事件进行通知。先通过ServerSocketChannel.open() 创建网络服务端,并设置为非阻塞已经绑定好监听的端口,在构建一个Selector选择器,并且将channel注册上去,选择对serverSocketChannel上面的accept事件感兴趣(serverSocketChannel只能支持accept操作),绑定端口,启动成功开始检测新连接。不再轮询通道,改用selector.select()轮询事件的方式.select方法有阻塞效果,直到有事件通知才会有返回,通过selector.selectedKeys()查询到所有注册上去的事件,开始遍历查询事件,如果进来的是Accept事件,就获取新连接并把Read事件注册到Channel上,用来监听这个连接的Read事件。如果这个连接有Read事件发生,下面的if语句将会处理Read事件,并把接收到的消息返回给客户端。
缺点:此处一个selector监听所有事件,一个线程处理所有请求事件. 会成为瓶颈! 要有多线程的运用。
以下是解决方案
第一种方案是单Reactor模式,Reactor只负责acceptor(接受)请求,负责网络层的一些协议交互,剩下对数据的处理交给了Thread Pool(线程池)来处理,应用层这部分耗时的操作分发给线程池进行工作。
第二种是多Reactor模式,mainReactor负责acceptor(接受)请求,然后分发给subReactor进行读写,最后具体的业务逻辑分发给线程池进行处理。
package com.study.hc.net.nio;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
/**
* NIO selector 多路复用reactor线程模型
*/
public class NIOServerV3 {
/** 处理业务操作的线程 */
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 {
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) {
try {
SelectableChannel channel = (SelectableChannel) key.attachment();
channel.configureBlocking(false);
handler(channel);
if (!channel.isOpen()) {
key.cancel(); // 如果关闭了,就取消这个KEY的订阅
}
} catch (Exception ex) {
key.cancel(); // 如果有异常,就取消这个KEY的订阅
}
}
}
selector.selectNow();
} catch (IOException 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();
//把新加入进来的连接随机绑定到subReactor上面,让subReactor进行I/O处理
SelectionKey selectionKey = workEventLoop.register(socketChannel);
//绑定subReactor感兴趣的事件
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 {
NIOServerV3 nioServerV3 = new NIOServerV3();
nioServerV3.newGroup(); // 1、 创建main和sub两组线程
nioServerV3.initAndRegister(); // 2、 创建serverSocketChannel,注册到mainReactor线程上的selector上
nioServerV3.bind(); // 3、 为serverSocketChannel绑定端口
}
}
==代码解析:==通过main方法分析,nioServerV3.newGroup()先创建两组线程,mainReactor线程用来处理新连接(accept事件),subReactor线程用来处理Read事件,每个subReactor线程在读取完数据之后可以进行相应的数据处理。nioServerV3.initAndRegister()是为了创建serverSocketChannel,注册到mainReactor线程上的selector上,并绑定mainReactor感兴趣的事件(accept事件),最后的nioServerV3.bind()是为serverSocketChannel绑定端口号,用来监听服务的端口。