小编A-Z*_*A-Z的帖子

如何使用 netty 在单独的线程池中执行业务逻辑处理程序

我有一个需要执行一些业务逻辑的处理程序,我希望它在单独的线程池中执行,以免阻塞 io 事件循环。我已将 DefaultEventExecutorGroup 添加到管道中,如http://netty.io/4.0/api/io/netty/channel/ChannelPipeline.html javadoc 和http://netty.io/wiki/new-and-noteworthy-in -4.0.html#no-more-executionhandler---它的核心维基:

ch.pipeline().addLast(new DefaultEventExecutorGroup(10), new ServerHandler());
Run Code Online (Sandbox Code Playgroud)

仅出于测试目的,我的 ServerHandler 只是让当前线程休眠 5 秒:

protected void channelRead0(ChannelHandlerContext ctx, Command cmd) throws Exception {
    System.out.println("Starting.");

    try {
        Thread.currentThread().sleep(5000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    System.out.println("Finished.");        
}
Run Code Online (Sandbox Code Playgroud)

但显然业务逻辑仍然是同步执行的:

Starting.
Finished.
Starting.
Finished.
Starting.
Finished.
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

java multithreading handler netty

4
推荐指数
1
解决办法
2183
查看次数

标签 统计

handler ×1

java ×1

multithreading ×1

netty ×1