Netty 在关闭前滞后几秒钟

Netty lags for a few seconds before shutting down

我从事网络编程已有一段时间,最近将我的一个项目转换为 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.
    }
}

循环class:

    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);
}

group.shutdownGracefully(); 默认情况下会等待大约 3 秒,直到所有待处理的任务都完全执行后才会自行终止。您可以指定一个不同的超时值,但是太小的值有获得 RejectedExecutionException 的风险。如果您确定您的应用程序与事件循环组没有任何关系,您可以指定一个非常小的值来立即终止它。