Java 为什么sync()不能在netty for ChannelFuture中生效
我正在学习Netty,我并不真正了解ChannelFuture的同步方法,以下是我的示例:Java 为什么sync()不能在netty for ChannelFuture中生效,java,netty,Java,Netty,我正在学习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.len
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();
}
}
公共类EchoServer{
私人最终国际港口;
公共EchoServer(内部端口){
this.port=端口;
}
公共静态void main(字符串[]args)引发异常{
如果(args.length!=1){
System.err.println(“用法:“+EchoServer.class.getSimpleName()+”);
返回;
}
int port=Integer.parseInt(args[0]);
新的EchoServer(端口).start();
}
public void start()引发异常{
//final EchoServerHandler serverHandler=新EchoServerHandler();
EventLoopGroup=new NioEventLoopGroup();
试一试{
ServerBootstrap b=新的ServerBootstrap();
b、 group(group).channel(nioserverssocketchannel.class).localAddress(新的InetSocketAddress(端口)).childHandler(新的ChannelInitializer()){
@凌驾
受保护的void initChannel(SocketChannel ch)引发异常{
//ch.pipeline().addLast(serverHandler);
}
});
ChannelFuture f=b.bind().sync();
f、 通道().closeFuture().sync();
}最后{
group.shutdownGracefully().sync();
}
}
}
公共类EchoClient{
私有最终字符串主机;
私人最终国际港口;
公共EchoClient(字符串主机,int端口){
this.host=host;
this.port=端口;
}
public void start()引发异常{
EventLoopGroup=new NioEventLoopGroup();
试一试{
引导b=新引导();
b、 组(组).channel(NioSocketChannel.class).remoteAddress(新的InetSocketAddress(主机,端口)).handler(新的ChannelInitializer()){
@凌驾
受保护的void initChannel(SocketChannel ch)引发异常{
//ch.pipeline().addLast(新的EchoClientHandler());
}
});
ChannelFuture f=b.connect().sync().addListener(未来->{
if(future.issucess()){
System.out.println(端口+“绑定成功”);
}否则{
System.err.println(端口+“绑定失败”);
}
});
系统输出打印项次(“aaa”);
f、 通道().closeFuture().sync();
}最后{
group.shutdownGracefully().sync();
}
}
公共静态void main(字符串[]args)引发异常{
如果(参数长度!=2){
System.err.println(“用法:“+EchoClient.class.getSimpleName()+”);
返回;
}
字符串host=args[0];
int port=Integer.parseInt(args[1]);
新的EchoClient(主机、端口).start();
}
}
我的问题是,无论我是否为ChannelFuture f=b.connect().sync()删除EchoClient.class中的sync(),结果总是:
aaa
8811绑定成功
我认为,如果添加sync(),结果应该是:
8811绑定成功
aaa
由于主线程将等待通道连接
如果删除sync(),结果应该是:
aaa
8811绑定成功
因为connect()是异步的
为什么我错了?操作完成后,
sync()
将立即解除阻止,EventLoop
将通知所有连接到频道的侦听器。由于您的主线程和EventLoop
线程不同,您很可能会在channelfutureelistener
中的主线程前面看到System.out.println(…)
,抱歉,我不理解您的问题。你能重写一下吗?@NormanMaurer好的,因为英语不是我的母语,也许我没有很好地描述这个问题,我的意思是对于代码通道f=b.connect().sync().addListener,sync()方法,在我看来,将确保connect()先完成,然后执行下面的代码,所以我认为System.out.println(port+“bind success”);将首先执行,然后执行System.out.println(“aaa”);但结果是:aaa 8811 bind success它不像我想的那样,System.out.println(“aaa”);已首先执行下面的回答。。。