【问题标题】:why sync() not take effect in netty for ChannelFuture为什么sync()在netty中对ChannelFuture不起作用
【发布时间】:2020-03-16 22:14:28
【问题描述】:

我在学习Netty,对ChannelFuture的同步方法不是很了解,下面是我的例子:

public class EchoServer {
    private final int port;

    public EchoServer(int port) {
        this.port = port;
    }

    public static void main(String[] args) throws Exception {
        if (args.length != 1) {
            System.err.println("Usage: " + EchoServer.class.getSimpleName() + " <port>");
            return;
        }

        int port = Integer.parseInt(args[0]);

        new EchoServer(port).start();
    }

    public void start() throws Exception {
        // final EchoServerHandler serverHandler = new EchoServerHandler();
        EventLoopGroup group = new NioEventLoopGroup();

        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(group).channel(NioServerSocketChannel.class).localAddress(new InetSocketAddress(port)).childHandler(new ChannelInitializer<SocketChannel>() {

                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    // ch.pipeline().addLast(serverHandler);
                }
            });

            ChannelFuture f = b.bind().sync();
            f.channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully().sync();
        }
    }
}



public class EchoClient {
    private final String host;
    private final int port;

    public EchoClient(String host, int port) {
        this.host = host;
        this.port = port;
    }

    public void start() throws Exception {
        EventLoopGroup group = new NioEventLoopGroup();

        try {
            Bootstrap b = new Bootstrap();
            b.group(group).channel(NioSocketChannel.class).remoteAddress(new InetSocketAddress(host, port)).handler(new ChannelInitializer<SocketChannel>() {

                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    // ch.pipeline().addLast(new EchoClientHandler());
                }
            });


            ChannelFuture f = b.connect().sync().addListener(future -> {

                   if(future.isSuccess()) {

                       System.out.println(port + " bind success");

                   } else{

                       System.err.println(port + " bind fail");

                   }

               });

            System.out.println("aaa");

            f.channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully().sync();
        }
    }

    public static void main(String[] args) throws Exception {
        if (args.length != 2) {
            System.err.println("Usage: " + EchoClient.class.getSimpleName() + " <host> <port>");
            return;
        }

        String host = args[0];
        int port = Integer.parseInt(args[1]);

        new EchoClient(host, port).start();
    }
}

我的问题是,无论我是否删除了 ChannelFuture f = b.connect().sync() 的 EchoClient.class 中的 sync(),结果总是: 啊啊啊 8811绑定成功

在我看来,如果我添加sync(),结果应该是: 8811 绑定成功 啊

因为主线程会等待通道连接,

如果我删除了sync(),结果应该是: 啊啊啊 8811绑定成功

因为 connect() 是异步的

为什么我错了?

【问题讨论】:

  • 对不起,我不明白你的问题。你能改一下吗?
  • @NormanMaurer 好吧,因为英语不是我的母语,可能我没有很好地描述这个问题,我的意思是代码 ChannelFuture f = b.connect().sync().addListener, sync() 方法,在我看来,将确保 connect() 先完成,然后执行下面的代码,所以我认为 System.out.println(port + "bind success");会先执行,再执行 System.out.println("aaa");但结果是:aaa 8811 绑定成功它不像我想的那样, System.out.println("aaa");已先执行
  • 在下面回答...

标签: java netty


【解决方案1】:

sync() 将在操作完成后立即解除阻塞,然后EventLoop 将通知所有附加的侦听器到ChannelFuture。由于您的主线程和EventLoop 线程不同,您很有可能会在主线程中看到System.out.println(...),然后在ChannelFutureListener 中看到。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-09
    • 2022-01-11
    • 2023-03-14
    • 1970-01-01
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多