为什么 sync() 在 ChannelFuture 的 netty 中不生效
why sync() not take effect in netty for ChannelFuture
我正在学习Netty,我对ChannelFuture的sync方法不是很了解,这里是我的例子:
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 绑定成功
aaa
因为主线程将等待通道连接,
如果我去掉sync(),结果应该是:
啊啊
8811绑定成功
因为 connect() 是异步的
为什么我错了?
一旦操作完成,sync()
将解除阻塞,然后 EventLoop
将通知所有附加的侦听器到 ChannelFuture
。由于您的主线程和 EventLoop
线程不同,因此您很有可能会在 ChannelFutureListener
[=16= 之前看到主线程中的 System.out.println(...)
]
我正在学习Netty,我对ChannelFuture的sync方法不是很了解,这里是我的例子:
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 绑定成功 aaa
因为主线程将等待通道连接,
如果我去掉sync(),结果应该是: 啊啊 8811绑定成功
因为 connect() 是异步的
为什么我错了?
一旦操作完成,sync()
将解除阻塞,然后 EventLoop
将通知所有附加的侦听器到 ChannelFuture
。由于您的主线程和 EventLoop
线程不同,因此您很有可能会在 ChannelFutureListener
[=16= 之前看到主线程中的 System.out.println(...)
]