当前位置:网站首页>nio服务多线程版本

nio服务多线程版本

2022-06-22 15:10:00 原力与你同在

public static void main(String[] args) throws IOException {
    
        Thread.currentThread().setName("boss");
        ServerSocketChannel ssc = ServerSocketChannel.open();
        ssc.configureBlocking(false);
        Selector boss = Selector.open();
        SelectionKey bossKey = ssc.register(boss, 0, null);
        bossKey.interestOps(SelectionKey.OP_ACCEPT);
        ssc.bind(new InetSocketAddress(8888));
        // worker 创建固定worker
        Worker worker = new Worker("worker-0");

        while (true){
    
            boss.select();
            Iterator<SelectionKey> iterator = boss.selectedKeys().iterator();
            while (iterator.hasNext()){
    
                SelectionKey key = iterator.next();
                iterator.remove();
                if(key.isAcceptable()){
    
                    SocketChannel sc = ssc.accept();
                    sc.configureBlocking(false);
                    log.info("connected...{}",sc.getRemoteAddress());
                    // 关联
                    log.info("before register...{}",sc.getRemoteAddress());
                    // 阻塞
                    worker.register(sc);
                    // 注册要在前,select方法后执行
                    log.info("after register...{}",sc.getRemoteAddress());
                }
            }
        }
    }

    static class Worker implements Runnable{
    
        private Thread thread;
        private Selector worker;
        private String name;
        private volatile boolean start = false;

        public Worker(String name){
    this.name = name;}
        public void register(SocketChannel sc) throws IOException {
    
            if(!start){
    
                thread = new Thread(this,name);
                worker = Selector.open();
                thread.start();

                start = true;
            }
            worker.wakeup(); // 唤醒
            sc.register(worker, SelectionKey.OP_READ,null);
        }

        @Override
        public void run() {
    

            while (true){
    
                try {
    
                    // 阻塞
                    worker.select();
                    Iterator<SelectionKey> iterator = worker.selectedKeys().iterator();
                    while (iterator.hasNext()){
    
                        SelectionKey key = iterator.next();
                        iterator.remove();
                        if(key.isReadable()){
    
                            ByteBuffer buffer = ByteBuffer.allocate(16);
                            SocketChannel channel = (SocketChannel) key.channel();
                            log.info("read:{}",channel.getRemoteAddress());
                            channel.read(buffer);
                            buffer.flip();
                            log.info("buffer is:{}",Charset.defaultCharset().decode(buffer).toString());
                        }
                    }
                }catch (IOException e){
    

                }
            }
        }
    }
原网站

版权声明
本文为[原力与你同在]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43978420/article/details/122877261