2.1.3 NIO非阻塞网络编程三大理念

Java NIO

始于Java 1.4,提供了新的Java IO操作非阻塞APi。

用意是替代Java IO 和 Java Networking相关的API。

NIO中有三个核心组件:

  • Buffer缓冲区
  • Channel通道
  • Selector选择器

Buffer缓冲区

缓冲区本质上是一个可以写入数据的内存块(类似数组),然后可以再次读取。此内存块包含在NIO Buffer对象中,该对象提供了一组方法,可以更轻松地使用内存块。

相比较直接对数组的操作,Buffer API更加容易操作和管理。

使用Buffer进行数据写入与读取,需要进行如下四个步骤:

  1. 将数据写入缓冲区
  2. 调用buffer.filp(),转换为读取模式
  3. 缓冲区读取数据
  4. 调用buffer.clear()或buffer.compact()清除缓冲区

Buffer工作原理

Buffer三个重要属性

  • capacity容量:作为一个内存块,Buffer具有一定的固定大小,也称为“容量”。
  • position位置:写入模式时代表写数据的位置。读取模式时代表读取数据的位置。
  • limit限制:写入模式,限制等于buffer的容量。读取模式下,limit等于写入的数据量。[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JJ4zbcYA-1581856216566)(/Users/xiekaifei/Library/Application Support/typora-user-images/截屏2020-02-16下午3.07.07.png)]

来演示一下一个Buffer的使用

public class BufferDemo {
    public static void main(String[] args) {
        // 构建一个byte字节缓冲区,容量是4
        ByteBuffer buffer = ByteBuffer.allocate(4);
        // 默认写入模式,查看三个重要的指标
        System.out.println(String.format("初始化:capacity容量: %s,position位置: %s,limit限制: %s",
                buffer.capacity(),buffer.position(),buffer.limit()));
        // 写入2字节的数据
        buffer.put((byte)1);
        buffer.put((byte)2);
        // 再看数据
        System.out.println(String.format("写入2字节后:capacity容量: %s,position位置: %s,limit限制: %s",
                buffer.capacity(),buffer.position(),buffer.limit()));
        // 转换为读取模式(不调用flip方法,也是可以读取数据的,但是position记录读取的位置不对)
        System.out.println("####开始读取");
        buffer.flip();
        byte a = buffer.get();
        System.out.println(a);
        byte b = buffer.get();
        System.out.println(b);
        System.out.println(String.format("读取2字节后:capacity容量: %s,position位置: %s,limit限制: %s",
                buffer.capacity(),buffer.position(),buffer.limit()));
        // 继续写入3字节,此时读模式下,limit=3,position=2.继续写入只能覆盖写入一条数据
        // clear()方法清除整个缓冲区。compact()方法仅清除已阅读的数据。转为写入模式
        buffer.compact();
        buffer.put((byte)3);
        buffer.put((byte)4);
        buffer.put((byte)5);
        System.out.println(String.format("最终情况:capacity容量: %s,position位置: %s,limit限制: %s",
                buffer.capacity(),buffer.position(),buffer.limit()));
        //rewind() 重置position的位置为0
        //mark() 标记position的位置
        //reset() 重置position为上次mark()标记的位置
    }
}

输出结果为:

初始化:capacity容量: 4,position位置: 0,limit限制: 4
写入2字节后:capacity容量: 4,position位置: 2,limit限制: 4
####开始读取
1
2
读取2字节后:capacity容量: 4,position位置: 2,limit限制: 2
最终情况:capacity容量: 4,position位置: 3,limit限制: 4

Process finished with exit code 0

ByteBuffer内存类型

ByteBuffer为性能关键型代码提供了直接内存(direct堆外)非直接内存(heap堆)两种实现。

堆外内存获取的方式:ByteBuffer directByteBuffer = ByteBuffer.allocateDirect(noBytes);

好处:

  1. 进行网络IO或者文件IO时比heapBuffer少一次拷贝。(file/socket ---- OS memory — JVM heap)GC会移动对象内存,可能在写file或socket的过程中,对象内存的位置就改变了,所以在JVM的实现中,会先把数据复制到堆外,再进行写入。
  2. GC范围之外,降低GC压力,但实现了自动管理。DirectByteBuffer中有一个Cleaner对象(PhantomReference),Cleaner被GC前会执行clean方法,触发DirectByteBuffer中定义的Deallocator。

DirectByteBuffer这个对象是受GC管理的,但它申请的内存是不受管理。

建议:

  1. 性能确实可观的时候才去使用;分配给大型、长寿命;(网络传输、文件读写场景)
  2. 通过虚拟机参数MaxDirectMemorySize限制大小,防止耗尽整个机器的内存。

使用方法方面与allocate()一样。

Channel通道

在这里插入图片描述

SocketChannel

SocketChannel用于建立TCP网络连接,类似java.net.Socket。有两种创建SocketChannel形式:

  1. 服务器主动发起和服务器的连接。
  2. 服务器获取的新连接。
//客户端主动发起连接的方式
SocketChannel socketChannel = SocketChannel.open();
socketChannel.configureBlocking(flase);//设置为非阻塞模式
channel.write(byteBuffer);//发送请求数据 - 向通道写入数据
int bytesRead = socketChannel.read(byteBuffer);//读取服务端返回 - 读取缓冲区的数据
socketChannel.close();//关闭连接

write写:write()在尚未写入任何内容时就可能返回了。需要在循环中调用write()。

read读:read()方法可能直接返回而根本不读取任何数据,根据返回的int值判断读取了多少字节。

ServerSocketChannel

ServerSocketChannel可以监听新建的TCP连接通道,类似ServerSocket。

//创建网络服务端
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(flase);//设置为非阻塞模式
serverSocketChannel.socket().bind(new InetSocketAddress(8080));//绑定端口
while(true){
  SockertChannel socketChannel = serverSocketChannel.accept();//获取新tcp连接通道
  if(socketChannel != null){
    //tcp请求 读取/响应
  }
}

serverSocketChannel.accept():如果该通道处于非阻塞模式,那么如果没有挂起的连接,该方法将立即返回null。必须检查返回的SocketChannel是否为null。

public class NIOClient {

    public static void main(String[] args) throws Exception {
        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 {
    /**
     * 已经建立连接的集合
     */
    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) {
            SocketChannel socketChannel = serverSocketChannel.accept(); // 获取新tcp连接通道
                // 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

Selector选择器

Selector是一个Java NIO组件,可以检查一个或多个NIO通道,并确定哪些通道已准备好进行读取或写入。实现单个线程可以管理多个通道,从而管理多个网络连接

一个线程使用Selector监听多个Channel的不同事件:

四个事件分别对应SelectionKey四个常量。

  1. Connect连接(SelectionKey.OP_CONNECT)
  2. Accept准备就绪(OP_ACCEPT)
  3. Read读取(OP_READ)
  4. Write写入(OP_WRITE)

实现一个线程处理多个通道的核心概念理解:事件驱动机制

非阻塞的网络通道下,开发者通过Selector注册对于通道感兴趣的事件类型,线程通过监听事件来触发相应的代码执行。(拓展:更底层是操作系统的多路复用机制)

public class NIOServerV2 {

    public static void main(String[] args) throws Exception {
        // 1. 创建网络服务端ServerSocketChannel
        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();
            // 遍历查询结果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监听所有事件,一个线程处理所有请求事件. 会成为瓶颈! 要有多线程的运用

NIO与多线程结合的改进方案

在这里插入图片描述

/**
 * 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();
                    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 {
        NIOServerV3 nioServerV3 = new NIOServerV3();
        nioServerV3.newGroup(); // 1、 创建main和sub两组线程
        nioServerV3.initAndRegister(); // 2、 创建serverSocketChannel,注册到mainReactor线程上的selector上
        nioServerV3.bind(); // 3、 为serverSocketChannel绑定端口
    }
}

并在最后附加一个注解完整的加深理解

public class TonyNioHttpServer {

    public static Selector selector;

    // 定义线程池
    public static final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(25, 25, 25,
            TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());

    private static ServerSocketChannel socketChannel;

    private static final int port = 8080;

    public static void main(String[] args) throws Exception {

        // serversocket
        socketChannel = ServerSocketChannel.open();
        socketChannel.configureBlocking(false);
        socketChannel.bind(new InetSocketAddress(port));

        System.out.println("NIO启动:" + port);
        // 获取一个选择器
        // 底层的事件通知机制
        // 老板娘 selector
        TonyNioHttpServer.selector = Selector.open();

        // 登记: 表示对这个通道上OP_ACCEPT事件感兴趣,并且返回一个标记
        // 此处表示,希望收到socket通道8080端口上建立连接这个通知
        SelectionKey selectionKey = socketChannel.register(TonyNioHttpServer.selector, 0);
        selectionKey.interestOps(selectionKey.OP_ACCEPT);
        
        while (true) { // 带几个美女,坐在大厅

            // 如果没有新的socket与服务器有连接或者是数据交互,这里就会等待1秒
            TonyNioHttpServer.selector.select(1000);

            // 开始处理
            Set<SelectionKey> selected = TonyNioHttpServer.selector.selectedKeys();
            Iterator<SelectionKey> iter = selected.iterator();
            while (iter.hasNext()) {
                // 获取注册在上面标记
                SelectionKey key = iter.next();

                if (key.isAcceptable()) { // 判断是否OP_ACCEPT的通知
                    // 处理连接
                    System.out.println("有新的连接啦,当前线程数量:"
                            + TonyNioHttpServer.threadPoolExecutor.getActiveCount());
                    // 有新的连接,赶紧接客
                    SocketChannel chan = socketChannel.accept();
                    // 问一下价格多少,需要什么样服务...
                    chan.configureBlocking(false);
                    // 注册一个新监听。
                    // 表示希望收到该连接上OP_READ数据传输事件的通知
                    chan.register(TonyNioHttpServer.selector, SelectionKey.OP_READ);
                } else if (key.isReadable()) { // OP_READ
                    // 取出附着在上面的信息,也就是上面代码中附着的连接信息
                    SocketChannel socketChannel = (SocketChannel) key.channel();
                    // 处理中,不需要收到任何通知
                    key.cancel();
                    // tomcat 大保健旗舰店 有200技师,只有付钱的客户才会享受技师 泰式、保shen,
                    socketChannel.configureBlocking(false);
                    TonyNioHttpServer.threadPoolExecutor.execute(() -> {
                        try {
                            // 读取里面的内容,请注意,此处大小随意写的。
                            // tomcat中会根据Http协议中定义的长度来读取数据,或者一直读到通道内无数据为止
                            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
                            socketChannel.read(byteBuffer);
                            byteBuffer.flip(); // 转为读模式
                            String request = new String(byteBuffer.array());

                            System.out.println("收到新数据,当前线程数:"
                                    + TonyNioHttpServer.threadPoolExecutor.getActiveCount()
                                    + ",请求内容:" + request);
                            // 给一个当前时间作为返回值
                            // 随意返回,不是Http的协议
                            byteBuffer.clear();
                            ByteBuffer wrap = ByteBuffer
                                    .wrap(("tony" + System.currentTimeMillis()).getBytes());
                            socketChannel.write(wrap);
                            wrap.clear();
                            socketChannel.configureBlocking(false);
                            // 注册一个新监听。 表示希望收到该连接上OP_READ事件的通知
                            socketChannel.register(TonyNioHttpServer.selector,
                                    SelectionKey.OP_READ);
                        } catch (Exception e) {
                            // e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName() + " 服务器线程处理完毕,当前线程数:"
                                + threadPoolExecutor.getActiveCount());
                    });
                }
                // 取出后删除
                iter.remove();
            }
            selected.clear();
            // 过掉cancelled keys
            TonyNioHttpServer.selector.selectNow();
        }
    }
}

NIO对比BIO

在这里插入图片描述

小结

NIO为开发者提供了功能丰富及强大的IO处理API,但是在应用与网络应用开发的过程中,直接使用JDK提供的API,比较繁琐。而且要想将性能进行提升,光有NIO还不够,还需要将多线程技术与之结合起来。

因为网络编程本身的复杂性,以及JDK API开发的使用难度较高,所以在开源社区中,涌出来很多对JDK NIO进行封装、增强后的网络编程框架,例如:Netty(将在后面讲述)、Mina等。

发布了18 篇原创文章 · 获赞 14 · 访问量 2281

猜你喜欢

转载自blog.csdn.net/weixin_43871142/article/details/104348270