zoukankan      html  css  js  c++  java
  • 不会吧!做了这么久开发还有不会NIO的,看看BAT大佬是怎么用的吧

    前言

    • 在将NIO之前,我们必须要了解一下Java的IO部分知识。
    • BIO(Blocking IO)
    • 阻塞IO,在Java中主要就是通过ServerSocket.accept()实现的。
    • NIO(Non-Blocking IO)
    • 非阻塞IO,在Java主要是通过NIOSocketChannel + Seletor实现的。
    • AIO(Asyc IO)
    • 异步IO,目前不做学习。

    BIO

    简单实现服务器和客户端

    package net.io;
    
    import net.ByteUtil;
    
    import java.io.*;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    //NIO(NonBlocking IO)非阻塞IO
    //通过一个事件监听器,吧这些客户端的连接保存起来,如果有时间发生再去处理,没时间发生不处理
    public class Server {
        public Server(int port) {
            try {
                //创建服务器端,监听端口port
                ServerSocket serverSocket = new ServerSocket(port);
                //对客户端进行一个监听操作,如果有连接过来,就将连接返回(socket)-----阻塞方法
                while (true) {
                    //监听,阻塞方法
                    Socket socket = serverSocket.accept();
                    //每个服务器和客户端的通信都是针对与socket进行操作
                    System.out.println("客户端" + socket.getInetAddress());
                    InputStream inputStream = socket.getInputStream();
                    ObjectInputStream ois = new ObjectInputStream(inputStream);
                    //获取客户端发送的message
                    Object get = ois.readObject();
                    System.out.println("接收到的消息为:"  + get);
    
                    //服务器需要给客户端进行一个回应
                    OutputStream outputStream = socket.getOutputStream();
                    ObjectOutputStream oos = new ObjectOutputStream(outputStream);
                    String message = "客户端你好,我是服务器端";
                    //我这里写了,不代表发送了,知识写到了输出流的缓冲区
                    oos.writeObject(message);
                    //发送并清空
                    oos.flush();
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            new Server(7000);
        }
    }
    
    
    package net.io;
    
    import net.ByteUtil;
    
    import java.io.*;
    import java.net.Socket;
    
    public class Client {
        public Client(int port){
            try {
                Socket socket = new Socket("localhost",port);
    
                //inputStream是输入流,从外面接收信息
                //outpurStream是输出流, 往外面输出信息
                OutputStream outputStream = socket.getOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(outputStream);
                //发送的信息
                String message = "服务器你好,我是客户端";
                //我这里写了,不代表发送了,知识写到了输出流的缓冲区
                oos.writeObject(message);
                //发送并清空
                oos.flush();
    
                //接收服务器的回应
                InputStream inputStream = socket.getInputStream();
                ObjectInputStream ois = new ObjectInputStream(inputStream);
                Object get = ois.readObject();
                System.out.println("接收的信息为:" + get);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            new Client(7000);
        }
    }
    
    

    针对于BIO,为什么是阻塞IO,是因为BIO是基于Socket完成数据的读写操作,Server调用accept()方法持续监听socket连接,所以就阻塞在accept()这里(后面的操作如果没有socket连接则无法执行),那这样就代表服务器只能同时处理一个socket的操作。
    所以后序我们通过为socket连接创建一个线程执行,这样就可以增大服务器处理连接的数量了。
    但是新的问题也就出来了,如果客户端的连接很多,那么就会导致服务器创建很多的线程
    对socket进行处理,如果服务器端不断开连接的话,那么对应的线程也不会被销毁,这样大数量的线程的维护十分消耗资源。针对于这种情况设计出了Java的NIO。

    NIO

    首先我们需要介绍一下NIO。如果说BIO是面向socket进行读写操作的话,那么NIO则是面向channel进行读写操作(或者说面向buffer)。
    这里我们在讲解NIO之前,需要先讲解一下这个buffer。众所周知这就是一个缓冲,并且我们知道socket具有输入流inputStream和输出流outputStream(读写分开的),但是我们的channel是同时具有read和write两个方法,而且两个方法都是基于buffer进行操作(这里就可以说明channel仅能比普通输入输出流好,相当于channel是一条双向,输入输出流是两条单向),所以我们可以知道buffer的重要性。

    Buffer

    诸如ByteBuffer,IntBuffer等都是Buffer的派生抽象类,需要调用抽象类的静态方法allocate(X capacity)方法进行一个初始化操作,该方法就是初始化buffer的大小,或者使用wrap(X x)方法,该方法相当于直接将信息存入缓冲中。至于存入buffer的put()方法和取出缓存的get()方法在下面代码中我就详细介绍(有底层知识,具有源码阅读能力的可以根据我的注释进行阅读),最关键的还有flip()方法,它是作为一个读写切换的作用,他使的缓存可又读又写,又使得读写相互隔离(需要注意的是使用buffer尽量是依次写完然后再一次读完,最后在调用clear()方法进行复位,不然会导致buffer容量越来越小,具体解释在下面代码)。

    package net.nio.buffer;
    
    import java.nio.IntBuffer;
    
    public class TestBuffer {
        public static void main(String[] args) {
    
            /*
             *IntBuffer有四个重要参数
             * 1.mark 标记
             * 2.position 相当于当前下标、索引
             * 3.limit 代表缓冲的终点,读取不能超过该下标,当然也不能超过最大容量。(在调用flip时候会将当前下标position值赋值给limit,然后position置0)
             * 4.Capacity 最大容量,在初始化IntBuffer对象时候就定义好了,不能改变(IntBuffer.allocate(int capacity) )
             *
             * ctrl+h 可以查看该类的子类
             */
    
            //intBuffer初始化
            IntBuffer intBuffer = IntBuffer.allocate(5);
    
            //放数据到缓冲区中
            intBuffer.put(10);
            intBuffer.put(11);
            intBuffer.put(12);
    //        intBuffer.put(13);
    //        intBuffer.put(14);
    
            /*
             *这里的读写反转的实现机制是:
             * 例如我们缓冲区容量为5,调用方法put()将数据写入缓冲区中,假如我们写入三个此时position为3,此时limit = capacity
             * 如果我们调用flip方法使得limit = 3 ,position = 0 ,mark我们现在先不管(下图源码已说明)
             *  public Buffer flip() {
             *      limit = position;
             *      position = 0;
             *      mark = -1;
             *      return this;
             * }
             *
             * 此时我们调用get()方法时候,取得下标是position的值,即从0下标读取。直到读取到position = limit = 3时候停止(不包括3)
             * 1.如果我们这个时候不调用flip()方法直接再次put()往缓冲区写入数据(即没从读状态切换到写状态),那么就会报错超过下标overflow
             * 2.如果我们调用一次flip()(即进入写状态)写入一个数据后,那么此时position = 0,limit = 3,此时我们最多存放3个数据(即下标0,1,2)
             * 如果我们不再次调用flip()切换状态那么就会导致,读取到错误数据,(即只存入了一个数据,但是却取出来了3个数据)
             *
             * 上述说明了一个问题,如果我们存取的数据越来越小,那么这个缓冲区逐渐缩小,导致并不能存取他的最大容量,可能会浪费内存,
             *(因为position是不能超过limit的,然而调用flip()方法后会使的limit = position(赋值操作),那么如果数据越来越少,
             * 就会导致缓冲区能使用的部分越来越小)
             *
             * 总结:缓冲区的大小设置应该根据实际使用进行设置(并且要及时调用clear() ),否则可能会导致缓冲区的内存浪费。
             */
            intBuffer.flip();
            //切换读写状态
            //判断缓存区是否还有剩余
            while (intBuffer.hasRemaining()) {
                System.out.println(intBuffer.get());
            }
        }
    }
    
    

    Channel

    Channel是NIO实现的基础,对于NIO,Channel的地位相当于BIO的socket。
    Channel具有非常多方法,其中使用最多的就是两个方法write(ByteBuffer buf)和read(ByteBuffer buf)方法。
    (这里需要注意的是这个read和write是buffer作为主体的,即read()方法是channel往buffer里写数据,而write()方法是指buffer向channel写数据)

    package net.nio.channel;
    
            import java.io.FileOutputStream;
            import java.nio.ByteBuffer;
            import java.nio.channels.FileChannel;
    
    public class TestChannel {
        public static void main(String[] args) throws Exception{
            String abc = "我写入文件了";
    
            //写入的文件地址与文件名
            FileOutputStream fileOutputStream = new FileOutputStream("C:\xxx\xxx\xxx\test.txt");
    
            //从输出流中获取channel实例
            FileChannel channel = fileOutputStream.getChannel();
    
            //创建字节缓冲区
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
    
            //将字符串转化成字节数组并放入缓冲区中,然后缓冲区转换读写状态,由写入状态变为读取状态
            byteBuffer.put(abc.getBytes());
            byteBuffer.flip();
    
            //将缓冲区数据写入到channel中(这里write代表从缓冲区写入,read代表从channel读取到缓冲区)
            channel.write(byteBuffer);
            //关闭通道和输出流
            channel.close();
            fileOutputStream.close();
        }
    }
    
    

    简单NIO实现

    上面在介绍NIO时候讲过,NIO是需要一个Selector线程去监听那些客户端有实现发生,从而在进行处理,而不是BIO的一个线程维护一个socket。

    下面针对于NIO我们先不引入Selector,就用BIO的方式实现一个客户端和服务器端。(相当于作为一个练手)

    Server

    package net.nio.socket;
    
    import java.net.InetSocketAddress;
    import java.nio.ByteBuffer;
    import java.nio.channels.Selector;
    import java.nio.channels.ServerSocketChannel;
    import java.nio.channels.SocketChannel;
    import java.util.Arrays;
    
    public class Server {
        public static void main(String[] args) throws Exception {
            //开启nio的服务器端,并且绑定8000端口进行监听
            ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
            InetSocketAddress inetSocketAddress = new InetSocketAddress(8000);
            serverSocketChannel.bind(inetSocketAddress);
    
            //创建缓冲区数组
            ByteBuffer byteBuffer = ByteBuffer.allocate(40);
    
            //服务器端接收来自客户端的请求,创建客户端的socket的实例
            SocketChannel socketChannel = serverSocketChannel.accept();
            //将客户端发送的数据读取到buffer数组中
            socketChannel.read(byteBuffer);
            byte[] array = byteBuffer.array();
            String msg = new String(array);
            System.out.println("服务器收到信息 : " + msg);
    
            //对buffer数组进行读写反转,由读状态到写状态
            byteBuffer.flip();
    
            //将数据回显到客户端去
            byteBuffer.put("ok".getBytes());
    
            //做完一套读写操作后,需要进行clear
            byteBuffer.clear();
            }
    }
    
    

    Client

    package net.nio.serverclient;
    
    import java.net.InetSocketAddress;
    import java.nio.ByteBuffer;
    import java.nio.channels.SocketChannel;
    
    public class NIOClient {
        public static void main(String[] args) throws Exception {
            SocketChannel socketChannel = SocketChannel.open();
            InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost",8000);
            socketChannel.configureBlocking(false);
    
            //如果客户端未连接上服务器
            if (!socketChannel.connect(inetSocketAddress)) {
                System.out.println("客户端连接不上服务器。。。。");
    
                //如果客户端没有完成连接
                while (!socketChannel.finishConnect()) {
                    System.out.println("连接中。。。。");
                }
            }
    
            //进入到这里说明连接成功
            String message = "hello , Server!";
            ByteBuffer byteBuffer = ByteBuffer.wrap(message.getBytes());
    
            //将buffer中的数据写入socketChannel
            socketChannel.write(byteBuffer);
            System.out.println("发送完毕");
        }
    }
    
    

    NIO实现(基于Selector)

    首先我们需要知道Selector是什么?
    Selector是一个选择器,既然是一个选择器,那么肯定是先有选项再有选择,理解这个后就知道channel肯定有的就是rigister()方法(因为需要将自己注册到Selector中)。

    既然选项有了,那么如何选择呢?
    Selector是针对已注册的channel中对有事件(例如:服务器:接受,读写,客户端:读写,服务器是在服务器开始就将自己注册,客户端是连接成功后由服务器将其注册)发生的channel进行处理。

    Selector注册的不是简单的channel,而是将channel和其监听事件封装成一个SelectionKey保存在Selector底层的Set集合中。

    Selector的keys()和selectedKeys()两个方法需要注意:
    keys()方法是返回已注册的所有selectionKey。
    selectedKeys()方法是返回有事件发生的selectionKey。

    上面就是Selector的简单工作流程,下面我将附上代码,因为有较详细的注释,所以除了重要知识点我不再多介绍。
    Server

    package net.nio.serverclient;
    
    import java.net.InetSocketAddress;
    import java.nio.ByteBuffer;
    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.Set;
    
    public class NIOServer {
        public static void main(String[] args) throws Exception {
            //开启ServerSocketChannel的监听
            ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
    
            //绑定端口8000
            serverSocketChannel.bind(new InetSocketAddress(8000));
    
            //创建Selector对象
            Selector selector = Selector.open();
    
            //设置监听为非阻塞
            serverSocketChannel.configureBlocking(false);
    
            //将ServerSocketChannel注册到Selector中(注册事件为ACCEPT)
            serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
    
            //Selector监听ACCEPT时间
            while (true) {
                //未有事件发生(下面是等待),返回值为int,代表事件发生个数
                if (selector.select(1000) == 0) {
                    System.out.println("服务器等待了1S,无事件发生。。。。");
                    continue;
                }
    
                //有客户端请求过来,就获取到相关的selectionKeys集合
                Set<SelectionKey> selectionKeys = selector.selectedKeys();
                Iterator<SelectionKey> iterator = selectionKeys.iterator();
                while (iterator.hasNext()) {
                    //获取到事件
                    SelectionKey selectionKey = iterator.next();
                    //移出读取过的事件
                    iterator.remove();
    
                    //根据对应事件对应处理
                    if (selectionKey.isAcceptable()) {
                        //有新的客户端连接服务器
                        SocketChannel socketChannel = serverSocketChannel.accept();
                        //给客户端设置非阻塞
                        socketChannel.configureBlocking(false);
                        //设置该SocketChannel为读事件,并为它绑定一个Buffer
                        socketChannel.register(selector,SelectionKey.OP_READ,ByteBuffer.allocate(1024));
                    }
                    if (selectionKey.isReadable()) {
                        //通过Key反向获取到事件的channel
                        SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
                        //获取到事件绑定的buffer
                        ByteBuffer byteBuffer = (ByteBuffer) selectionKey.attachment();
                        socketChannel.read(byteBuffer);
                        //重置缓冲
                        byteBuffer.clear();
                        String message = new String(byteBuffer.array());
                        System.out.println("接收到客户端信息为: "+ message);
                    }
                }
            }
        }
    }
    
    

    Client

    package net.nio.serverclient;
    
    import java.net.InetSocketAddress;
    import java.nio.ByteBuffer;
    import java.nio.channels.SocketChannel;
    import java.util.Scanner;
    
    public class NIOClient {
        public static void main(String[] args) throws Exception {
            SocketChannel socketChannel = SocketChannel.open();
            InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost",8000);
            socketChannel.configureBlocking(false);
    
            //如果客户端未连接上服务器
            if (!socketChannel.connect(inetSocketAddress)) {
                System.out.println("客户端连接不上服务器。。。。");
    
                //如果客户端没有完成连接
                while (!socketChannel.finishConnect()) {
                    System.out.println("连接中。。。。");
                }
            }
    
            //进入到这里说明连接成功
            while(true) {
                Scanner scanner = new Scanner(System.in);
                String message = scanner.nextLine();
                ByteBuffer byteBuffer = ByteBuffer.wrap(message.getBytes());
    
                //将buffer中的数据写入socketChannel
                socketChannel.write(byteBuffer);
                System.out.println("发送完毕");
            }
    
        }
    }
    
    

    我们这里可以发现,只需要主函数中进行一个死循环,死循环中对selector注册的channel进行监听(select()方法),有事件发生则根据channel注册的监听事件对应进行处理。

    这里需要注意的是需要将ServerSocketChannel和SocketChannel编程非阻塞(调用configureBlocking(false)),不然是无法注册到Selector中。

    还有一件事需要注意:我们每次是通过iterator(迭代器)遍历发生时间的Set ,为了避免重复处理时间,我们在获取发生时间的selctionKey以后,就将其remove()。

    最后

    感谢你看到这里,看完有什么的不懂的可以在评论区问我,觉得文章对你有帮助的话记得给我点个赞,每天都会分享java相关技术文章或行业资讯,欢迎大家关注和转发文章!

  • 相关阅读:
    【JMeter】if语句中不能Failure=false解决办法
    【java】method.invoke(方法底层所属对象/null,new Object[]{实际参数})
    【java设计模式】【行为模式Behavioral Pattern】模板方法模式Template Method Pattern
    【java设计模式】代理模式
    33-算法训练 安慰奶牛
    32-java 里面list的问题
    31-java中知识总结:list, set, map, stack, queue
    30-算法训练 最短路 spfa
    29-算法训练 结点选择-超时了!!!
    28-算法训练 最大最小公倍数 -贪心
  • 原文地址:https://www.cnblogs.com/lwh1019/p/13564858.html
Copyright © 2011-2022 走看看