netty分析-基于helloworld

本文主要是针对 netty使用-helloworld一文中的代码,简单分析下其中使用到的netty的api。

一、NioEventLoopGroup

当执行 new NioEventLoopGroup() 时,netty主要执行了以下代码


	public NioEventLoopGroup() {
 //默认传入线程数为0,当最终生成的线程数其实不为0,后面会说明
        this(0);
    }
    
    public NioEventLoopGroup(int nThreads) {
        this(nThreads, (Executor) null);
    }
    
    public NioEventLoopGroup(int nThreads, Executor executor) {
		//调用nio api生成 选择器
        this(nThreads, executor, SelectorProvider.provider());
    }
===========================================
public static SelectorProvider provider() {
		//加锁
        synchronized (lock) {
        //判断provider是否已经初始化过
            if (provider != null)
                return provider;
            return AccessController.doPrivileged(
                new PrivilegedAction<SelectorProvider>() {
                    public SelectorProvider run() {
                    //判断如果系统属性java.nio.channels.spi.SelectorProvider 已经被定义了,则该属性名看作具体提供者类的完全限定名。加载并实例化该类;如果此进程失败,则抛出未指定的错误。
                            if (loadProviderFromProperty())
                                return provider;
                                //如果在对系统类加载器可见的 jar 文件中安装了提供者类,并且该 jar 文件包含资源目录 META-INF/services 中名为 java.nio.channels.spi.SelectorProvider 的提供者配置文件,则采用在该文件中指定的第一个类名称。加载并实例化该类;如果此进程失败,则抛出未指定的错误。
                           if (loadProviderAsService())
                                return provider;
                                //生成默认的选择器(一般都会是这种情况)
                            provider = sun.nio.ch.DefaultSelectorProvider.create();
                            return provider;
                        }
                    });
        }
    }

public class DefaultSelectorProvider {
    private DefaultSelectorProvider() {
    }
   //不同系统的默认的选择器并不一样,由于我是mac系统,所以返回的是KQueueSelectorProvider
    public static SelectorProvider create() {
        return new KQueueSelectorProvider();
    }
}
==============================================
	
	public NioEventLoopGroup(
            int nThreads, Executor executor, final SelectorProvider selectorProvider) {
            //使用默认选择策略的工厂
        this(nThreads, executor, selectorProvider, DefaultSelectStrategyFactory.INSTANCE);
    }
    
    public NioEventLoopGroup(int nThreads, Executor executor, final SelectorProvider selectorProvider,
                             final SelectStrategyFactory selectStrategyFactory) {
         //拒绝策略 默认实现中是直接抛出异常。
        super(nThreads, executor, selectorProvider, selectStrategyFactory, RejectedExecutionHandlers.reject());
    }

super()进入MultithreadEventLoopGroup调用

	//调用super类MultithreadEventLoopGroup构造方法
   protected MultithreadEventLoopGroup(int nThreads, Executor executor, Object... args) {
   		//当线程数传入0,则使用默认线程数
        super(nThreads == 0 ? DEFAULT_EVENT_LOOP_THREADS : nThreads, executor, args);
    }
==============================================
//默认线程数为cpu核心数*2
  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);
        }
==============================================

super()进入MultithreadEventExecutorGroup调用

//进入super父类MultithreadEventExecutorGroup
	protected MultithreadEventExecutorGroup(int nThreads, Executor executor, Object... args) {
//默认的执行器选择工厂
        this(nThreads, executor, DefaultEventExecutorChooserFactory.INSTANCE, args);
    }
    
    /**创建实例,执行了大量的初始化
    * @param nThreads 此实例将使用的线程数
    * @param executor 要使用的 Executor,如果应该使用默认null则为null 。
    * @param chooserFactory 要使用的EventExecutorChooserFactory 
    * @param args  是 selectorProvider, selectStrategyFactory, RejectedExecutionHandlers.reject()的简写
    */
	protected MultithreadEventExecutorGroup(int nThreads, Executor executor,
                                            EventExecutorChooserFactory chooserFactory, Object... args) {
        if (nThreads <= 0) {
            throw new IllegalArgumentException(String.format("nThreads: %d (expected: > 0)", nThreads));
        }

        if (executor == null) {
        //为空则初始化
            executor = new ThreadPerTaskExecutor(newDefaultThreadFactory());
        }
		
		//以传入的线程数量生成执行器数组
        children = new EventExecutor[nThreads];
		//循环
        for (int i = 0; i < nThreads; i ++) {
            boolean success = false;
            try {
            	//创建一个新的 EventExecutor
                children[i] = newChild(executor, args);
                success = true;
            } catch (Exception e) {
                // TODO: Think about if this is a good exception type
                throw new IllegalStateException("failed to create a child event loop", e);
            } finally {
            	//未成功
                if (!success) {
                	//依次优雅退出
                    for (int j = 0; j < i; j ++) {
                        children[j].shutdownGracefully();
                    }

                    for (int j = 0; j < i; j ++) {
                        EventExecutor e = children[j];
                        try {
                            while (!e.isTerminated()) {
                                e.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);
                            }
                        } catch (InterruptedException interrupted) {
                            // Let the caller handle the interruption.
                            Thread.currentThread().interrupt();
                            break;
                        }
                    }
                }
            }
        }
		//初始化EventExecutorChooserFactory.EventExecutorChooser
        chooser = chooserFactory.newChooser(children);

        final FutureListener<Object> terminationListener = new FutureListener<Object>() {
            @Override
            public void operationComplete(Future<Object> future) throws Exception {
                if (terminatedChildren.incrementAndGet() == children.length) {
                    terminationFuture.setSuccess(null);
                }
            }
        };

        for (EventExecutor e: children) {
            e.terminationFuture().addListener(terminationListener);
        }

        Set<EventExecutor> childrenSet = new LinkedHashSet<EventExecutor>(children.length);
        Collections.addAll(childrenSet, children);
        readonlyChildren = Collections.unmodifiableSet(childrenSet);
    }

上文的初始化过程中有个需要注意的点

executor = new 
//使用的是netty自定义的工厂类
ThreadPerTaskExecutor(newDefaultThreadFactory());

 private final ThreadFactory threadFactory;
	
	//通过工厂创建ThreadPerTaskExecutor
    public ThreadPerTaskExecutor(ThreadFactory threadFactory) {
        if (threadFactory == null) {
            throw new NullPointerException("threadFactory");
        }
        this.threadFactory = threadFactory;
    }

    @Override
    public void execute(Runnable command) {
    //一旦执行它就会开启新的线程
        threadFactory.newThread(command).start();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值