代码之家  ›  专栏  ›  技术社区  ›  idiotprogrammer

Netty在关机前滞后了几秒钟

  •  0
  • idiotprogrammer  · 技术社区  · 11 年前

    我做了一段时间的网络编程,最近把我的一个项目转换成了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 回复  |  直到 11 年前
        1
  •  3
  •   trustin    11 年前

    group.shutdownGracefully(); 默认情况下,等待大约3秒,直到所有未决任务在其自身终止之前被完全执行。您可以指定不同的超时值,但值太小可能会导致 RejectedExecutionException 。如果您确定应用程序没有与事件循环组相关的业务,则可以指定一个非常小的值来立即终止它。