当前位置:网站首页>NiO service multithreaded version

NiO service multithreaded version

2022-06-22 16:28:00 The force is with you

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  Create fixed 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());
                    //  relation 
                    log.info("before register...{}",sc.getRemoteAddress());
                    //  Blocking 
                    worker.register(sc);
                    //  Registration is required before ,select Method post execution 
                    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(); //  Wake up the 
            sc.register(worker, SelectionKey.OP_READ,null);
        }

        @Override
        public void run() {
    

            while (true){
    
                try {
    
                    //  Blocking 
                    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){
    

                }
            }
        }
    }
原网站

版权声明
本文为[The force is with you]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221508060299.html