欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

NIO与BIO 的介绍

程序员文章站 2022-04-24 10:34:16
...

1 NIO与BIO
IO为同步阻塞形式,NIO为同步非阻塞形式,NIO并没有实现异步,在JDK1.7后升级NIO库包,支持异步非阻塞
BIO:同步阻塞式IO,服务器实现模式为一个连接一个线程,即客户端有连接请求时服务器端就需要启动一个线程进行处理,如果这个连接不做任何事情那么就会造成不必要的线程开销,当然可以通过线程池机制改善。
NIO:同步非阻塞式IO,服务器实现模式为一个请求一个线程,即客户端发送的连接请求都会注册到多路复用器上,多路复用器轮询到连接有I/O请求时才启动一个线程进行处理。
AIO(NIO.2):异步非阻塞式IO,服务器实现模式为一个有效请求一个线程,客户端的I/O请求都是由OS先完成了再通知服务器应用去启动线程进行处理。
2 BIO NIO AIO区别
BIO: 同步 阻塞 的IO
NIO: 同步 非阻塞的IO(生产环境使用最多的一种IO)
AIO(nio2.0): 异步 非阻塞IO
3 附带NIO编程
1 NIO的服务端


import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Iterator;
import java.util.Set;
import java.lang.String;

/**
 * NIO服务器端
 */
public class NioServer
{
    public static void main(String[] args)
    {
        //创建缓冲区迎来存储数据
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        try
        {
            //打开服务器通道
            ServerSocketChannel scc = ServerSocketChannel.open();
            //配置非阻塞服务器端通道
            scc.configureBlocking(false);
            //打开选择器 多路复用器
            Selector selector = Selector.open();
            //绑定一个端口
            scc.bind(new InetSocketAddress(8899));
            //在选择器中注册通道
            scc.register(selector , SelectionKey.OP_ACCEPT);
            //循环挑选
            while (true)
            {
                //阻塞
                selector.select();
                Set<SelectionKey> selectionKeys = selector.selectedKeys();
                Iterator<SelectionKey> iterator = selectionKeys.iterator();
                while (iterator.hasNext())
                {
                    SelectionKey sk = iterator.next();
                    //服务器端通道的accept事件
                    if(sk.isAcceptable())
                    {
                        SelectableChannel channel = sk.channel();
                        //把channel强转成socketChannel事件 调用阻塞方法
                        SocketChannel accept = ((ServerSocketChannel) channel).accept();
                        //客户端建立连接后立马把客户端的通道注册到选择器
                        accept.configureBlocking(false);
                        accept.register(selector , SelectionKey.OP_READ);
                        if(sk.isReadable())
                        {
                            SocketChannel channel1 = (SocketChannel) sk.channel();
                            int len;
                            //read方法在此处不阻塞
                            while ((len = channel1.read(buffer)) > 0)
                            {
                                buffer.flip();
                                byte [] b = new byte[buffer.limit()];
                                buffer.get(b);
                                System.out.println(new String(b));
                                buffer.clear();
                            }
                            if (len == -1)
                            {
                                System.out.println("读到最后一个了吆");
                                channel1.close();
                            }
                        }
                    }
                }
                selectionKeys.clear();
            }

        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}
**2NIO客户端**
package NIOtest;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * NIO客户端
 */
public class NioClient
{
    public static void main(String[] args) throws IOException
    {

        SocketChannel s = SocketChannel.open(new InetSocketAddress("localhost",8899));
        //jdk1.7以后支持false(同步非阻塞)通道
        s.configureBlocking(false);
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        buffer.put("HelloWorde".getBytes());
        buffer.flip();
        s.write(buffer);
        buffer.clear();
        s.close();
    }
}

以上附加的是 nio基础编程 如有错误请及时指出
仅供参考…

相关标签: NIO BIO