张坤的个人博客

  • 首页
  • 分类
  • 标签
  • 日志

  • 搜索
Jenkins RabbitMQ Zookeeper IDEA Logstash Kibana ELK NIO Netty Spring Cloud Golang DataX Elasticsearch React Native Mysql H2 Socket Spring Boot Kafka Mybatis Sqlmap Vue Postgresql Docker Vert.x Flutter Flink Redis

Netty创建NioEventLoopGroup默认线程数量

发表于 2020-06-17 | 分类于 Netty | 0 | 阅读次数 57

如果在代码中写了这句,并没有显示指定线程数量,那么 NioEventLoopGroup 默认线程数量是多少呢

EventLoopGroup bossGroup = new NioEventLoopGroup();

在 NioEventLoopGroup 的默认构造方法中,直接将线程数指向0

public NioEventLoopGroup() {
    this(0);
}

一路跟踪,发现最后调用了父类 MultithreadEventLoopGroup 的构造方法

protected MultithreadEventLoopGroup(int nThreads, Executor executor, Object... args) {
    super(nThreads == 0 ? DEFAULT_EVENT_LOOP_THREADS : nThreads, executor, args);
}

可以看到,默认线程数量是 DEFAULT_EVENT_LOOP_THREADS 这个常量,否则就会是 nThreads

可以发现 DEFAULT_EVENT_LOOP_THREADS 在一个静态代码块里被赋值

static {
    DEFAULT_EVENT_LOOP_THREADS = Math.max(1, SystemPropertyUtil.getInt(
        "io.netty.eventLoopThreads", NettyRuntime.availableProcessors() * 2));

    if (logger.isDebugEnabled()) {
        logger.debug("-Dio.netty.eventLoopThreads: {}", DEFAULT_EVENT_LOOP_THREADS);
    }
}

如果设置了 jvm启动参数 io.netty.eventLoopThreads 那么默认线程数就是 io.netty.eventLoopThreads 的值,否则就是 NettyRuntime.availableProcessors() 的两倍。注意这里Netty保证了默认线程数必须大于等于1。如果 io.netty.eventLoopThreads 设置的值小于等于0,Math.max 会返回1,那么默认线程数量就为1。

再来跟踪 NettyRuntime.availableProcessors() 做了什么,调用了 NettyRuntime 的内部类 AvailableProcessorsHolder 的 availableProcessors 方法

public final class NettyRuntime {

    static class AvailableProcessorsHolder {

        private int availableProcessors;

        @SuppressForbidden(reason = "to obtain default number of available processors")
        synchronized int availableProcessors() {
            if (this.availableProcessors == 0) {
                final int availableProcessors =
                    SystemPropertyUtil.getInt(
                    "io.netty.availableProcessors",
                    Runtime.getRuntime().availableProcessors());
                setAvailableProcessors(availableProcessors);
            }
            return this.availableProcessors;
        }
    }

    public static int availableProcessors() {
        return holder.availableProcessors();
    }

}

availableProcessors 的初始值为0,就会再次获取jvm启动参数 io.netty.availableProcessors 的值。否则调用 Runtime.getRuntime().availableProcessors() 获取jvm处理器的个数,最后线程数等于处理器个数的两倍

可以发现,jvm启动参数的 io.netty.eventLoopThreads 的优先级比 io.netty.availableProcessors 高

区别是 io.netty.eventLoopThreads 的值就是线程数, io.netty.availableProcessors * 2 才是线程数

# Jenkins # RabbitMQ # Zookeeper # IDEA # Logstash # Kibana # ELK # NIO # Netty # Spring Cloud # Golang # DataX # Elasticsearch # React Native # Mysql # H2 # Socket # Spring Boot # Kafka # Mybatis # Sqlmap # Vue # Postgresql # Docker # Vert.x # Flutter # Flink # Redis
Can't execute code from a freed script
IO基础回顾
  • 文章目录
  • 站点概览
会Coding的猴子

会Coding的猴子

57 日志
19 分类
28 标签
RSS
Github
Creative Commons
© 2021 会Coding的猴子
由 Halo 强力驱动
|
主题 - NexT.Gemini v5.1.4

湘ICP备18011740号