java - Netty的future.channel().closeFuture().sync();到底有什么用?
問(wèn)題描述
我看到很多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>() {@Overrideprotected void initChannel(SocketChannel arg0) throws Exception { arg0.pipeline().addLast(new LineBasedFrameDecoder(1024)); arg0.pipeline().addLast(new StringDecoder()); arg0.pipeline().addLast(new ChannelInboundHandlerAdapter() {@Overridepublic 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('執(zhí)行到這里 ' + port);} catch (InterruptedException e) { e.printStackTrace();} } public static void main(String[] args) {new TimeServer().bind(10000); }}
但是我看這行代碼一直沒(méi)有執(zhí)行。請(qǐng)問(wèn)這是怎么回事呢?
問(wèn)題解答
回答1:不是沒(méi)執(zhí)行,是主線程到這里就 wait 子線程退出了,子線程才是真正監(jiān)聽(tīng)和接受請(qǐng)求的。
相關(guān)文章:
1. 關(guān)docker hub上有些鏡像的tag被標(biāo)記““This image has vulnerabilities””2. docker - 如何修改運(yùn)行中容器的配置3. Docker for Mac 創(chuàng)建的dnsmasq容器連不上/不工作的問(wèn)題4. docker鏡像push報(bào)錯(cuò)5. 前端 - @media query 使用出現(xiàn)的問(wèn)題?6. 利用IPMI遠(yuǎn)程安裝centos報(bào)錯(cuò)!7. 運(yùn)行python程序時(shí)出現(xiàn)“應(yīng)用程序發(fā)生異?!钡膬?nèi)存錯(cuò)誤?8. docker 下面創(chuàng)建的IMAGE 他們的 ID 一樣?這個(gè)是怎么回事????9. phpstudy8.1沒(méi)集成mysql-front10. html - css氣泡,實(shí)現(xiàn)“倒三角(不知道算不算三角了)”可透明的。

網(wǎng)公網(wǎng)安備