关闭之前,Netty滞后了几秒钟

时间:2015-03-17 00:59:14

标签: netty

我已经做了一段时间的网络编程,并且最近将我的一个项目转换为netty。由于与我的原始程序不同,客户端会在关闭前冻结约3-5秒,足以让我每次都终止它,因为我不想等待它,这让我很烦恼。 。这对于netty来说是正常的吗?难道我做错了什么?

主要方法:

public static void main(String[] args) throws Exception {
    final SslContext sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);

    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group)
         .channel(NioSocketChannel.class)
         .handler(new NetworkInitializer(sslCtx));
        // Start the connection attempt.
         ch = b.connect(HOST, PORT).sync().channel();

        //sends the clients name to the server
        askname();


    //loop just does some simple gl stuff and gameplay updating, posted below
        while(running){loop();
        if (Display.isCloseRequested()){


            running = false;
            if (lastWriteFuture != null) {
                lastWriteFuture.sync();
            }    //tells the server to shut down
            write("9");
            ch.closeFuture().sync();
            group.shutdownGracefully();
            System.out.println("herro der");
            break;
        }
        }
    } finally {
        group.shutdownGracefully();

        // The connection is closed automatically on shutdown.
    }
}

循环类:

    private static void loop() {
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
    //main update method for my game.  does calculations and stuff.  I can post it if neccisary but its kinda big.
    SpaceState.update();
    Display.update();
    Display.sync(60);
}

1 个答案:

答案 0 :(得分:1)

默认情况下,

group.shutdownGracefully();会等待大约3秒钟,直到所有挂起的任务在终止之前完全执行。您可以指定不同的超时值,但是值太小会有获得RejectedExecutionException的风险。如果您确定您的应用程序没有事件循环组的业务,您可以指定一个非常小的值来立即终止它。

相关问题