u013179958
CSDNRGY
2017-04-15 03:45

对于netty中sync()方法的一些疑问~

  • websocket
  • socket
  • net
  • java-ee
 public final class WebSocketServer {

    static final boolean SSL = System.getProperty("ssl") != null;
    static final int PORT = Integer.parseInt(System.getProperty("port", SSL? "8443" : "8080"));

    public static void main(String[] args) throws Exception {
        // Configure SSL.
        final SslContext sslCtx;
        if (SSL) {
            SelfSignedCertificate ssc = new SelfSignedCertificate();
            sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
        } else {
            sslCtx = null;
        }

        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
             .channel(NioServerSocketChannel.class)
             .handler(new LoggingHandler(LogLevel.INFO))
             .childHandler(new WebSocketServerInitializer(sslCtx));
            //从这里就有点看不懂了,这里绑定了一个端口,这个sync是什么?
            //文档上说:等待这个未来直到它完成,如果这个未来失败,重新抛出失败的原因。
            //文档上说的感觉就是java中serversocket的acpte接收方法,而且还是同步接收
            //但实时指定不是,因为执行完sync这段代码,程序并没有阻塞,而是继续执行下面的代码了
            Channel ch = b.bind(PORT).sync().channel();

            System.out.println("Open your web browser and navigate to " +
                    (SSL? "https" : "http") + "://127.0.0.1:" + PORT + '/');
            //程序执行到这里开始阻塞,等待有计算机连接进来
            ch.closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐