首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NetTY4,ExecutorService的使用

NetTY4,ExecutorService的使用
EN

Stack Overflow用户
提问于 2014-02-11 04:55:22
回答 1查看 4.6K关注 0票数 6

我想在我的应用程序中重用ExecutorService。

Reusing a NioWorkerPool across multiple server and client bootstraps

我试图用netty 4重现上面发布的代码,但我没有找到这样做的方法,我也谷歌了很多次quit,但似乎我们不能为bootstrap或NioEventLoopGroup对象提供ExecutorService。

以netty 3为例,下面是如何共享executorservice的方法:

代码语言:javascript
复制
ExecutorService executor = Executors.newCachedThreadPool();
NioClientBossPool clientBossPool = new NioClientBossPool(executor, clientBossCount);
NioServerBossPool serverBossPool = new NioServerBossPool(executor, serverBossCount);
NioWorkerPool workerPool = new NioWorkerPool(executor, workerCount);

ChannelFactory cscf = new NioClientSocketChannelFactory(clientBossPool, workerPool);
ChannelFactory sscf = new NioServerSocketChannelFactory(serverBossPool, workerPool);
...

ClientBootstrap cb = new ClientBootstrap(cscf);
ServerBootstrap sb = new ServerBootstrap(sscf);

但是在netty 4中,据我所知你不能使用一个执行器服务...您必须提供一个像NioEventLoopGroup这样的EventLoop实现,但我真的想使用一个通用的executorService,我将在我的应用程序中使用它。因为我想在一个线程池中让线程做不同种类的工作:计算,网络和网络...

代码语言:javascript
复制
EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)
EventLoopGroup workerGroup = new NioEventLoopGroup()
ServerBootstrap b = new ServerBootstrap(); // (2)
        b.group(bossGroup, workerGroup)
         .channel(NioServerSocketChannel.class) // (3)
         .childHandler(new ChannelInitializer<SocketChannel>() { // (4)
             @Override
             public void initChannel(SocketChannel ch) throws Exception {
                 ch.pipeline().addLast(new DiscardServerHandler());
             }
         })
         .option(ChannelOption.SO_BACKLOG, 128)          // (5)
         .childOption(ChannelOption.SO_KEEPALIVE, true); // (6)
EN

回答 1

Stack Overflow用户

发布于 2014-02-12 10:55:35

在netty 5中,NioEventLoopGroup附带了一个构造函数,该构造函数接受一个executor作为参数:

代码语言:javascript
复制
EventLoopGroup bossGroup = new NioEventLoopGroup(nThreads, yourExecutor);

但是我不确定在netty 4中是不是这样。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21687708

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档