>  Q&A  >  본문

java - Netty的future.channel().closeFuture().sync();到底有什么用?

我看到很多Netty的例子都在末尾加上了这句话:future.channel().closeFuture().sync();

比如:

public class TimeServer {
    private int count = 0;

    public void bind(int port) {
        try {
            EventLoopGroup bossGroup = new NioEventLoopGroup();
            EventLoopGroup workGroup = new NioEventLoopGroup();
            ServerBootstrap b = new ServerBootstrap(); // (2)
            b.group(bossGroup, workGroup).channel(NioServerSocketChannel.class) // (3)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel arg0) throws Exception {
                            arg0.pipeline().addLast(new LineBasedFrameDecoder(1024));
                            arg0.pipeline().addLast(new StringDecoder());
                            arg0.pipeline().addLast(new ChannelInboundHandlerAdapter() {
                                @Override
                                public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                    // ByteBuf buf = (ByteBuf) msg;
                                    // byte[] req = new
                                    // byte[buf.readableBytes()];
                                    // buf.readBytes(req);
                                    // String body = new String(req, "UTF-8");
                                    System.out.println(
                                            "The Time Server  Received order:" + msg + "; the  counter is:" + ++count);

                                    // String currentTime = "QUERY TIME
                                    // ORDER".equalsIgnoreCase(body)
                                    // ? new
                                    // Date(System.currentTimeMillis()).toString()
                                    // : "BAD ORDER";
                                    //
                                    // currentTime = currentTime +
                                    // System.getProperty("line.separator");
                                    // ByteBuf resp =
                                    // Unpooled.copiedBuffer(currentTime.getBytes());
                                    // ctx.writeAndFlush(resp);
                                }
                            });
                        }
                    });
            ChannelFuture future = b.bind(port).sync();

            System.out.println("Server start listen at " + port);
            future.channel().closeFuture().sync();
            
            System.out.println("执行到这里 " + port);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new TimeServer().bind(10000);

    }
}

但是我看这行代码一直没有执行。请问这是怎么回事呢?

阿神阿神2742일 전1757

모든 응답(1)나는 대답할 것이다

  • 大家讲道理

    大家讲道理2017-04-18 10:58:32

    실행되지 않는 것이 아니라 메인 스레드가 기다리고 하위 스레드가 여기서 종료된다는 것입니다. 실제로 요청을 모니터링하고 수락하는 스레드입니다.

    회신하다
    0
  • 취소회신하다