首页 > 编程语言 >Netty源码-06-MpscQueue

Netty源码-06-MpscQueue

时间:2022-11-16 22:36:27浏览次数:41  
标签:NioEventLoop Netty return newMpscQueue static 源码 线程 IO 06

在IO线程NioEventLoop中维护了一个队列实现,用于存放非IO任务,一个IO线程负责N个Channel,为了保证一个线程贯穿始终负责一个Channel的所有任务(任务执行次序有先后区分需要),因此可能IO线程自己将待执行的内容封装成异步任务,也有可能其他线程提交任务(立即执行或者定时任务)

面对这种场景

  • 多个线程充当生产者
  • 唯一线程充当消费者

Netty选择了JCTools的实现MpscQueue,即多生产者单消费者模型

一 源码出处

// NioEventLoop.java
 newTaskQueue(taskQueueFactory), // 非IO任务队列

NioEventLoop的构造方法

// NioEventLoop.java
private static Queue<Runnable> newTaskQueue(
            EventLoopTaskQueueFactory queueFactory) {
        if (queueFactory == null) {
            return newTaskQueue0(DEFAULT_MAX_PENDING_TASKS);
        }
        return queueFactory.newTaskQueue(DEFAULT_MAX_PENDING_TASKS);
    }
// NioEventLoop.java
private static Queue<Runnable> newTaskQueue0(int maxPendingTasks) {
        // This event loop never calls takeTask()
        return maxPendingTasks == Integer.MAX_VALUE ? PlatformDependent.<Runnable>newMpscQueue()
                : PlatformDependent.<Runnable>newMpscQueue(maxPendingTasks);
    }
// PlatformDependent.java
public static <T> Queue<T> newMpscQueue() {
        return Mpsc.newMpscQueue();
    }

static <T> Queue<T> newMpscQueue() {
            return USE_MPSC_CHUNKED_ARRAY_QUEUE ? new MpscUnboundedArrayQueue<T>(MPSC_CHUNK_SIZE)
                                                : new MpscUnboundedAtomicArrayQueue<T>(MPSC_CHUNK_SIZE);
        }

二 类图关系

三 实现

标签:NioEventLoop,Netty,return,newMpscQueue,static,源码,线程,IO,06
From: https://www.cnblogs.com/miss-u/p/16897735.html

相关文章

  • Netty源码-07-Channel
    一类图关系在Java的NIO体系中定义了ServerSocketChannel和SocketChannelNetty为了支持Reactor线程模型和异步编程,自己也实现了与Java中对应的两个实现NioServerSocke......
  • Netty源码-09-ServerBootstrapAcceptor
    在ServerBootstrapAcceptor启用之前,此刻Reactor状态应该是NioServerSocketChannel在IO多路复用器上关注着Accept(16)事件pipeline中有4个handlerheadbossHandlerSer......
  • Netty源码-08-ChannelInitializer
    一回顾几个时机点pipeline的初始化用户向pipeline添加ChannelInitializer辅助实例Channel注册到复用器之后回调1pipeline的初始化初始化Channel的时候触发了pipel......
  • Netty源码-10-ChannelFuture
    Netty为了提高系统的吞吐,大量使用异步线程模型一DemopublicclassFutureTest00{publicstaticvoidmain(String[]args)throwsInterruptedException,Execut......
  • Netty源码-01-NioEventLoopGroup
    一定义摘自源码JavaDoc/***The{@linkEventExecutorGroup}isresponsibleforprovidingthe{@linkEventExecutor}'stouse*viaits{@link#next()}method......
  • Netty源码-02-FastThreadLocalThread
    一DemopublicclassFastThreadLocalTest00{privatefinalstaticFastThreadLocal<Long>v=newFastThreadLocal<Long>(){@Overrideprotec......
  • 第五章第6节: 2020.06.10 智能互联网之弹性容器云与Service Mesh【六】
                                                         ......
  • 06.适配器模式
    适配器模式作为两个不兼容的接口之间的桥梁结合了两个独立接口的功能例如实际中有个两孔的插座,现在需要有个三孔的插口,如果不直接在两孔的class修改,就需要加一个适配cla......
  • apache启动遇到phpinfo只显示源码问题
    在安装php和apache的时候,会遇到只显示源码的问题网上找了好多帖子都是在改php.ini的东西,但是改了半天也不对,发现我安装的wordpress目录也打不开,所以我认为这就是apache服......
  • T292306 01最短路 题解
    又是一个找不到题目所以自己写的题。。。40迪杰斯特拉,但是搞不懂为什么是wa而不是re的#include<bits/stdc++.h>#definefor1(i,a,b)for(inti=a;i<=b;i++)#definell......