Java之NIO基础介绍(一)

概念

NIO中非阻塞IO采用了基于Reactor的模式。会在Selector中注册某个事件,然后监听通道Channel,当有事件到达才对该事件进行处理。也就是延迟IO

核心Selectors、channel、buffers

1. channel

类比InputStream、OutputStream。是一个层面,面对流的,只不过后者channel是双向的,即可写又可读,所以才是通道。
并且双向的前提是,基于随机访问文件RandomAccessFile可读可写访问指针。

在Java中Channel的实现

package java.nio.channels;

import java.io.IOException;
import java.io.Closeable;

public interface Channel extends Closeable {

    public boolean isOpen();

   
    public void close() throws IOException;

}

channel不能直接传输数据,需要依赖buffer缓存区实现。
重要的实现类:

  1. FileChannel 从文件中读写数据
  2. DatagramChannel,使用UDP从网络中读写数据
  3. SocketChannel,TCP
  4. ServerSocketChannel,监听TCP连接,为每一个连接创建一个3
//示例:读取文件
public static void main(String[] args) throws Exception {
        RandomAccessFile file = new RandomAccessFile("D:\\makedowm笔记\\服务器部署有关.txt", "r");//只读模式打开
        FileChannel channel = file.getChannel();
        ByteBuffer allocate = ByteBuffer.allocate(1024);
        int read = channel.read(allocate);
        long l = System.currentTimeMillis();
        System.out.println("开始时间:" + l);
        while (read != -1) {
            System.out.println("读取了:" + read);
            allocate.flip();
            System.out.println(new String(allocate.array(), StandardCharsets.UTF_8));
            allocate.clear();
            read = channel.read(allocate);
        }
        channel.close();
        System.out.println("over");
        long l1 = System.currentTimeMillis();
        System.out.println("结束时间:" + l1);
        System.out.println("用时:" + (l1 - l) + "ms");
    }
    //示例:写文件
     public static void main(String[] args) throws Exception {
        RandomAccessFile file = new RandomAccessFile("D:\\makedowm笔记\\a.txt", "rw");
        FileChannel channel = file.getChannel();
        ByteBuffer allocate = ByteBuffer.allocate(1024);
        String data = "123abc";
        allocate.clear();
        allocate.put(data.getBytes(StandardCharsets.UTF_8));
        allocate.flip();
        while(allocate.hasRemaining()) {
            channel.write(allocate);
        }
        channel.close();

    }
    
    package nio;

    //示例:接收TCP连接
    public static void main(String[] args) throws Exception {

        ServerSocketChannel open = ServerSocketChannel.open();
        open.bind(new InetSocketAddress(8888));
        open.configureBlocking(false);

        while(true) {
            System.out.println("等待");
            SocketChannel accept = open.accept();
            if(accept == null) {
                System.out.println("无连接");
                Thread.sleep(2000);
            } else {
                System.out.println(accept.socket().getRemoteSocketAddress());
                accept.close();
            }
        }

    }

接下来是buffer缓存区的学习。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值