Netty源码—3.Reactor线程模子四

[复制链接]
发表于 2025-10-9 16:37:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
大纲
5.NioEventLoop的实行总体框架
6.Reactor线程实行一次变乱轮询
7.Reactor线程处置惩罚产生IO变乱的Channel
8.Reactor线程处置惩罚使命队列之添加使命
9.Reactor线程处置惩罚使命队列之实行使命
10.NioEventLoop总结

8.Reactor线程处置惩罚使命队列之添加使命
(1)Reactor线程实行一次变乱轮询的过程
(2)使命的分类和添加分析
(3)平凡使命的添加
(4)定时使命的添加
(5)Netty的定时使命机制增补

(1)Reactor线程实行一次变乱轮询的过程
Reactor线程通过NioEventLoop的run()方法每举行一次变乱轮询,起首会调用select()方法实行检测出IO变乱,然后会调用processSelectedKeys()方法处置惩罚检测出的IO变乱。此中IO变乱重要包罗新毗连接入变乱和毗连的数据读写变乱,末了会调用runAllTasks()方法处置惩罚使命队列中的异步使命。

(2)使命的分类和添加分析
runAllTasks()方法中的Task包罗平凡使命和定时使命,分别存放于NioEventLoop差别的队列里。一个是平凡的使命队列MpscQueue,另一个是定时的使命队列PriorityQueue。

平凡的使命队列MpscQueue在创建NioEventLoop时创建的,然后在外部线程调用NioEventLoop的execute()方法时,会调用addTask()方法将Task生存到平凡的使命队列里。

定时的使命队列PriorityQueue则是在添加定时使命时创建的,然后在外部线程调用NioEventLoop的schedule()方法时,会调用scheduleTaskQueue().add()方法将Task生存到定时的使命队列里。

(3)平凡使命的添加
场景一:用户自界说平凡使命
场景二:外部线程调用Channel的方法

当通过ctx.channel().eventLoop().execute(...)自界说平凡使命,大概通过非Reactor线程(外部线程)调用Channel的各类方法时,末了都会实行到SingleThreadEventExecutor的execute()方法。

场景一:用户自界说平凡使命
不管是外部线程照旧Reactor线程实行NioEventLoop的execute()方法,都会调用NioEventLoop的addTask()方法,然后调用offerTask()方法。而offerTask()方法会利用一个taskQueue将Task生存起来。这个taskQueue实在就是一个MPSC队列,每一个NioEventLoop都会有一个MPSC队列。

Netty利用MPSC队列可以方便地将外部线程的异步使命举行聚集,然后在Reactor线程内部用单线程来批量实行以提拔性能。可以鉴戒Netty的这种使命实行模式来处置惩罚类似多线程数据聚合,定时上报应用。
  1. //场景一:用户自定义普通任务
  2. ctx.channel().eventLoop().execute(new Runnable() {
  3.     @Override
  4.     public void run() {
  5.         ...   
  6.     }
  7. });
  8. public final class NioEventLoop extends SingleThreadEventLoop {
  9.     ...
  10. }
  11. public abstract class SingleThreadEventLoop extends SingleThreadEventExecutor implements EventLoop {
  12.     ...
  13. }
  14. public abstract class SingleThreadEventExecutor extends AbstractScheduledEventExecutor implements OrderedEventExecutor {
  15.     //每一个NioEventLoop会有一个MPSC队列
  16.     private final Queue<Runnable> taskQueue;
  17.     protected SingleThreadEventExecutor(EventExecutorGroup parent, Executor executor,
  18.           boolean addTaskWakesUp, int maxPendingTasks, RejectedExecutionHandler rejectedHandler) {
  19.         super(parent);
  20.         this.addTaskWakesUp = addTaskWakesUp;
  21.         this.maxPendingTasks = Math.max(16, maxPendingTasks);
  22.         this.executor = ObjectUtil.checkNotNull(executor, "executor");
  23.         //创建普通的任务队列MpscQueue
  24.         taskQueue = newTaskQueue(this.maxPendingTasks);
  25.         rejectedExecutionHandler = ObjectUtil.checkNotNull(rejectedHandler, "rejectedHandler");
  26.     }
  27.     ...
  28.    
  29.     @Override
  30.     public void execute(Runnable task) {
  31.         if (task == null) throw new NullPointerException("task");
  32.         boolean inEventLoop = inEventLoop();
  33.         //不管是外部线程还是Reactor线程执行NioEventLoop的execute()方法,都会调用NioEventLoop的addTask()方法
  34.         if (inEventLoop) {
  35.             addTask(task);
  36.         } else {
  37.             startThread();
  38.             addTask(task);
  39.             if (isShutdown() && removeTask(task)) {
  40.                 reject();
  41.             }
  42.         }
  43.         if (!addTaskWakesUp && wakesUpForTask(task)) wakeup(inEventLoop);
  44.     }
  45.    
  46.     //Add a task to the task queue, or throws a RejectedExecutionException if this instance was shutdown before.
  47.     protected void addTask(Runnable task) {
  48.         if (task == null) throw new NullPointerException("task");
  49.         if (!offerTask(task)) reject(task);
  50.     }
  51.     final boolean offerTask(Runnable task) {
  52.         if (isShutdown()) reject();
  53.         return taskQueue.offer(task);
  54.     }
  55.     ...
  56. }
复制代码
场景二:外部线程调用Channel的方法
这个场景是在业务线程里,根据用户标识找到对应的Channel,然后调用Channel的write()方法向该用户推送消息。

外部线程在调用Channel的write()方法时,executor.inEventLoop()会返回false。于是会将write操纵封装成一个WriteTask,然后调用safeExecute()方法来实行。默认情况下会获取Channel对应的NIO线程,然后作为参数传入safeExecute()方法中举行实行,从而确保使命会由Channel对应的NIO线程实行,通过单线程实行来实现线程安全。
  1. //场景二:当前线程为业务线程
  2. channel.write(...)
  3. abstract class AbstractChannelHandlerContext extends DefaultAttributeMap implements ChannelHandlerContext, ResourceLeakHint {
  4.     final EventExecutor executor;//一般初始化时默认为null
  5.     AbstractChannelHandlerContext(DefaultChannelPipeline pipeline, EventExecutor executor, String name, Class<? extends ChannelHandler> handlerClass) {
  6.         this.pipeline = pipeline;
  7.         this.executor = executor;
  8.         ...
  9.     }
  10.         
  11.     ...
  12.     private void write(Object msg, boolean flush, ChannelPromise promise) {
  13.         AbstractChannelHandlerContext next = findContextOutbound();
  14.         final Object m = pipeline.touch(msg, next);
  15.         EventExecutor executor = next.executor();
  16.         //外部线程在调用Channel的write()方法时,executor.inEventLoop()会返回false
  17.         if (executor.inEventLoop()) {
  18.             if (flush) {
  19.                 next.invokeWriteAndFlush(m, promise);
  20.             } else {
  21.                 next.invokeWrite(m, promise);
  22.             }
  23.         } else {
  24.             //将write操作封装成一个WriteTask
  25.             AbstractWriteTask task;
  26.             if (flush) {
  27.                 task = WriteAndFlushTask.newInstance(next, m, promise);
  28.             }  else {
  29.                 task = WriteTask.newInstance(next, m, promise);
  30.             }
  31.             //调用safeExecute()来执行
  32.             safeExecute(executor, task, promise, m);
  33.         }
  34.     }
  35.    
  36.     @Override
  37.     public EventExecutor executor() {
  38.         if (executor == null) {
  39.             return channel().eventLoop();
  40.         } else {
  41.             return executor;
  42.         }
  43.     }
  44.    
  45.     private AbstractChannelHandlerContext findContextOutbound(int mask) {
  46.         AbstractChannelHandlerContext ctx = this;
  47.         EventExecutor currentExecutor = executor();
  48.         do {
  49.             ctx = ctx.prev;
  50.         } while (skipContext(ctx, currentExecutor, mask, MASK_ONLY_OUTBOUND));
  51.         return ctx;
  52.     }
  53.    
  54.     private static void safeExecute(EventExecutor executor, Runnable runnable, ChannelPromise promise, Object msg) {
  55.         try {
  56.             //调用SingleThreadEventExecutor.execute()方法
  57.             executor.execute(runnable);
  58.         } catch (Throwable cause) {
  59.             try {
  60.                 promise.setFailure(cause);
  61.             } finally {
  62.                 if (msg != null) {
  63.                     ReferenceCountUtil.release(msg);
  64.                 }
  65.             }
  66.         }
  67.     }
  68.     ...
  69. }
  70. public class DefaultChannelPipeline implements ChannelPipeline {
  71.     ...
  72.     final class HeadContext extends AbstractChannelHandlerContext implements ChannelOutboundHandler, ChannelInboundHandler {
  73.         private final Unsafe unsafe;
  74.         HeadContext(DefaultChannelPipeline pipeline) {
  75.             //传入的executor为null
  76.             super(pipeline, null, HEAD_NAME, HeadContext.class);
  77.             unsafe = pipeline.channel().unsafe();
  78.             setAddComplete();
  79.         }
  80.         ...
  81.     }
  82.    
  83.     final class TailContext extends AbstractChannelHandlerContext implements ChannelInboundHandler {
  84.         TailContext(DefaultChannelPipeline pipeline) {
  85.             //传入的executor为null
  86.             super(pipeline, null, TAIL_NAME, TailContext.class);
  87.             setAddComplete();
  88.         }
  89.         ...
  90.     }
  91.     ...
  92. }
  93. public abstract class SingleThreadEventExecutor extends AbstractScheduledEventExecutor implements OrderedEventExecutor {
  94.     //每一个NioEventLoop会有一个MPSC队列
  95.     private final Queue<Runnable> taskQueue;
  96.     protected SingleThreadEventExecutor(EventExecutorGroup parent, Executor executor,
  97.           boolean addTaskWakesUp, int maxPendingTasks, RejectedExecutionHandler rejectedHandler) {
  98.         super(parent);
  99.         this.addTaskWakesUp = addTaskWakesUp;
  100.         this.maxPendingTasks = Math.max(16, maxPendingTasks);
  101.         this.executor = ObjectUtil.checkNotNull(executor, "executor");
  102.         //创建普通的任务队列MpscQueue
  103.         taskQueue = newTaskQueue(this.maxPendingTasks);
  104.         rejectedExecutionHandler = ObjectUtil.checkNotNull(rejectedHandler, "rejectedHandler");
  105.     }
  106.   
  107.     ...
  108.     @Override
  109.     public void execute(Runnable task) {
  110.         if (task == null) throw new NullPointerException("task");
  111.         boolean inEventLoop = inEventLoop();
  112.         //不管是外部线程还是Reactor线程执行NioEventLoop的execute()方法,都会调用NioEventLoop的addTask()方法
  113.         if (inEventLoop) {
  114.             addTask(task);
  115.         } else {
  116.             startThread();
  117.             addTask(task);
  118.             if (isShutdown() && removeTask(task)) {
  119.                 reject();
  120.             }
  121.         }
  122.         if (!addTaskWakesUp && wakesUpForTask(task)) wakeup(inEventLoop);
  123.     }
  124.    
  125.     //Add a task to the task queue, or throws a RejectedExecutionException if this instance was shutdown before.
  126.     protected void addTask(Runnable task) {
  127.         if (task == null) throw new NullPointerException("task");
  128.         if (!offerTask(task)) reject(task);
  129.     }
  130.     final boolean offerTask(Runnable task) {
  131.         if (isShutdown()) reject();
  132.         return taskQueue.offer(task);
  133.     }
  134.     ...
  135. }
复制代码
(4)定时使命的添加
通常利用ctx.channel().eventLoop().schedule(..)自界说定时使命,此中schedule()方法会通过scheduledTaskQueue().add(task)来添加定时使命。起首scheduledTaskQueue()方法会返回一个优先级队列,然后通过该优先级队列的add()方法将定时使命对象参加到队列中。

留意,这里可以直接利用优先级队列而不消思量多线程并发题目的缘故起因如下。假如是外部线程调用schedule()方法添加定时使命,那么Netty会将添加定时使命这个逻辑封装成一个平凡的Task。这个Task的使命是一个"添加某定时使命"的使命,而不是添加某定时使命。如许,对优先级队列的访问就酿成单线程了,也就是只有Reactor线程会访问,从而不存在多线程并发题目。
  1. //场景三:用户自定义定时任务,这也是用得最多的方法
  2. ctx.channel().eventLoop().schedule(new Runnable() {
  3.     @Override
  4.     public void run() {
  5.         ...   
  6.     }
  7. }, 60, TimeUnit.SECONDS);
  8. //Abstract base class for EventExecutors that want to support scheduling.
  9. public abstract class AbstractScheduledEventExecutor extends AbstractEventExecutor {
  10.     Queue<ScheduledFutureTask<?>> scheduledTaskQueue;
  11.     ...
  12.     //添加定时任务
  13.     <V> ScheduledFuture<V> schedule(final ScheduledFutureTask<V> task) {
  14.         if (inEventLoop()) {
  15.             //如果当前线程是Reactor线程,则直接往PriorityQueue中添加任务
  16.             scheduledTaskQueue().add(task);
  17.         } else {
  18.             //如果是外部线程,则调用SingleThreadEventExecutor.execute()方法
  19.             //将添加定时任务这一动作也封装成一个普通任务
  20.             execute(new Runnable() {
  21.                 @Override
  22.                 public void run() {
  23.                     scheduledTaskQueue().add(task);
  24.                 }
  25.             });
  26.         }
  27.         return task;
  28.     }
  29.    
  30.     Queue<ScheduledFutureTask<?>> scheduledTaskQueue() {
  31.         if (scheduledTaskQueue == null) {
  32.             //创建定时的任务队列PriorityQueue
  33.             scheduledTaskQueue = new PriorityQueue<ScheduledFutureTask<?>>();
  34.         }
  35.         return scheduledTaskQueue;
  36.     }
  37.     ...
  38. }
复制代码
(5)Netty的定时使命机制增补
一.怎样包管克制时间近来的使命优先实行
为什么定时使命要生存在优先级队列中?优先级队列的特性是会按照肯定的次序来分列内部元素,内部元素是可以比力的。由于优先级队列中的每个元素都是定时使命,以是定时使命也是可以比力的。比力的逻辑就是:先比力定时使命的克制时间,在克制时间类似的情况下再比力定时使命的添加次序也就是ID。

二.Netty的定时使命有三种实行方式
方式一:定时使命不会被重复实行
ctx.channel().eventLoop().schedule(),通报的periodNanos为0。
方式二:每隔一段时间实行一次
ctx.channel().eventLoop().scheduleAtFixedRate(),通报的periodNanos为正数。
方式三:隔类似时间再实行一次
ctx.channel().eventLoop().scheduleWithFixedDelay(),通报的periodNanos为负数。

Netty的3种定时使命的实行逻辑是通过调解下一次使命的克制时间来运行的。起首修改完下一次实行的克制时间,然后把当前使命再次参加队列,如许就能确保使命在得当的时间实行。
  1. final class ScheduledFutureTask<V> extends PromiseTask<V> implements ScheduledFuture<V> {
  2.     //每个定时任务都有一个唯一的ID
  3.     private static final AtomicLong nextTaskId = new AtomicLong();
  4.     private final long id = nextTaskId.getAndIncrement();
  5.     private long deadlineNanos;
  6.     //标识一个任务是否重复执行,以及以何种方式执行
  7.     private final long periodNanos;
  8.     ...
  9.    
  10.     @Override
  11.     public int compareTo(Delayed o) {
  12.         if (this == o) {
  13.             return 0;
  14.         }
  15.         ScheduledFutureTask<?> that = (ScheduledFutureTask<?>) o;
  16.         long d = deadlineNanos() - that.deadlineNanos();
  17.         if (d < 0) {
  18.             return -1;
  19.         } else if (d > 0) {
  20.             return 1;
  21.         } else if (id < that.id) {
  22.             return -1;
  23.         } else if (id == that.id) {
  24.             throw new Error();
  25.         } else {
  26.             return 1;
  27.         }
  28.     }
  29.     public long deadlineNanos() {
  30.         return deadlineNanos;
  31.     }
  32.    
  33.     @Override
  34.     public void run() {
  35.         ...
  36.         if (periodNanos == 0) {
  37.             //1.对应schedule()方法,表示一次性任务
  38.             V result = task.call();
  39.             setSuccessInternal(result);
  40.         } else {
  41.             task.call();
  42.             long p = periodNanos;
  43.             if (p > 0) {
  44.                 //2.对应scheduleAtFixedRate()方法,表示以固定速率执行任务
  45.                 deadlineNanos += p;
  46.             } else {
  47.                 //3.对应scheduleWithFixedDelay()方法,表示以固定的延时执行任务
  48.                 deadlineNanos = nanoTime() - p;
  49.             }
  50.             scheduledTaskQueue.add(this);
  51.         }
  52.         ...
  53.     }
  54.     ...
  55. }
复制代码
9.Reactor线程处置惩罚使命队列之实行使命
(1)runAllTasks()方法须要传入超时时间
(2)Reactor线程实行使命的步调
(3)Netty性能优化之批量战略
(4)NioEventLoop.run()方法实行使命总结

(1)runAllTasks()方法须要传入超时时间
SingleThreadEventExecutor的runAllTasks()方法须要传入参数timeoutNanos,体现只管在timeoutNanos时间内将全部的使命都取出来实行一遍。由于假如Reactor线程在实行使命时停顿的时间过长,那么将会累积许多IO变乱无法及时处置惩罚,从而导致大量客户端哀求壅闭。因此Netty会精致控制内部使命队列的实行时间。

(2)Reactor线程实行使命的步调
一.使命聚合
转移定时使命到MPSC队列,这里只是将快到期的定时使命转移到MPSC队列里。

二.时间盘算
盘算本轮使命实行的克制时间,此时全部克制时间已到达的定时使命均被添补到平凡的使命队列(MPSC队列)里了。

三.使命实行
起首不抛非常地同步实行使命,然后累加当前已实行的使命数,接着每隔64次盘算一下当前时间是否已超克制时间,末了判断本轮使命是否已经实行完毕。
  1. //Abstract base class for OrderedEventExecutor's that execute all its submitted tasks in a single thread.
  2. public abstract class SingleThreadEventExecutor extends AbstractScheduledEventExecutor implements OrderedEventExecutor {
  3.     //每一个NioEventLoop会有一个MPSC队列
  4.     private final Queue<Runnable> taskQueue;
  5.     ...
  6.    
  7.     //Poll all tasks from the task queue and run them via Runnable#run() method.
  8.     //This method stops running the tasks in the task queue and returns if it ran longer than timeoutNanos.
  9.     protected boolean runAllTasks(long timeoutNanos) {
  10.         //1.转移定时任务到MPSC队列,也就是任务聚合
  11.         fetchFromScheduledTaskQueue();
  12.         //从普通的任务队列(MPSC队列)里获取任务
  13.         Runnable task = pollTask();
  14.         if (task == null) {
  15.             afterRunningAllTasks();
  16.             return false;
  17.         }
  18.         //2.计算本轮任务执行的截止时间
  19.         final long deadline = ScheduledFutureTask.nanoTime() + timeoutNanos;
  20.         long runTasks = 0;
  21.         long lastExecutionTime;
  22.         //3.执行任务,通过for循环逐个执行pollTask()取出的任务
  23.         for (;;) {
  24.             //3.1 不抛异常地执行任务(同步阻塞),确保任务可以安全执行
  25.             safeExecute(task);
  26.             //3.2 累加当前已执行的任务数
  27.             runTasks ++;
  28.             //3.3 每隔64次计算一下当前时间是否已经超过截止时间,因为ScheduledFutureTask.nanoTime()也挺耗时的
  29.             if ((runTasks & 0x3F) == 0) {
  30.                 lastExecutionTime = ScheduledFutureTask.nanoTime();
  31.                 if (lastExecutionTime >= deadline) {
  32.                     break;
  33.                 }
  34.             }
  35.             //3.4 判断本轮任务是否已经执行完毕
  36.             task = pollTask();
  37.             if (task == null) {
  38.                 lastExecutionTime = ScheduledFutureTask.nanoTime();
  39.                 break;
  40.             }
  41.         }
  42.         afterRunningAllTasks();
  43.         this.lastExecutionTime = lastExecutionTime;
  44.         return true;
  45.     }
  46.    
  47.     private boolean fetchFromScheduledTaskQueue() {
  48.         long nanoTime = AbstractScheduledEventExecutor.nanoTime();
  49.         Runnable scheduledTask  = pollScheduledTask(nanoTime);
  50.         while (scheduledTask != null) {
  51.             if (!taskQueue.offer(scheduledTask)) {
  52.                 scheduledTaskQueue().add((ScheduledFutureTask<?>) scheduledTask);
  53.                 return false;
  54.             }
  55.             scheduledTask  = pollScheduledTask(nanoTime);
  56.         }
  57.         return true;
  58.     }
  59.    
  60.     protected Runnable pollTask() {
  61.         assert inEventLoop();
  62.         return pollTaskFrom(taskQueue);
  63.     }
  64.     protected final Runnable pollTaskFrom(Queue<Runnable> taskQueue) {
  65.         for (;;) {
  66.             Runnable task = taskQueue.poll();
  67.             if (task == WAKEUP_TASK) {
  68.                 continue;
  69.             }
  70.             return task;
  71.         }
  72.     }
  73.     ...
  74. }
  75. //Abstract base class for EventExecutors that want to support scheduling.
  76. public abstract class AbstractScheduledEventExecutor extends AbstractEventExecutor {
  77.     Queue<ScheduledFutureTask<?>> scheduledTaskQueue;
  78.     ...
  79.     //Return the Runnable which is ready to be executed with the given nanoTime.
  80.     //You should use #nanoTime() to retrieve the the correct nanoTime.
  81.     protected final Runnable pollScheduledTask(long nanoTime) {
  82.         assert inEventLoop();
  83.         Queue<ScheduledFutureTask<?>> scheduledTaskQueue = this.scheduledTaskQueue;
  84.         ScheduledFutureTask<?> scheduledTask = scheduledTaskQueue == null ? null : scheduledTaskQueue.peek();
  85.         if (scheduledTask == null) {
  86.             return null;
  87.         }
  88.         if (scheduledTask.deadlineNanos() <= nanoTime) {
  89.             scheduledTaskQueue.remove();
  90.             return scheduledTask;
  91.         }
  92.         return null;
  93.     }
  94.     ...
  95. }
  96. //Abstract base class for EventExecutor implementations.
  97. public abstract class AbstractEventExecutor extends AbstractExecutorService implements EventExecutor {
  98.     ...
  99.     //Try to execute the given Runnable and just log if it throws a Throwable.
  100.     protected static void safeExecute(Runnable task) {
  101.         try {
  102.             task.run();//同步执行任务
  103.         } catch (Throwable t) {
  104.             logger.warn("A task raised an exception. Task: {}", task, t);
  105.         }
  106.     }
  107.     ...
  108. }
复制代码
(3)Netty性能优化之隔断战略
假设使命队列里有海量的小使命,假如每次实行完使命都须要判断是否到克制时间,那么服从是比力低的。以是Netty选择通过每隔64个使命才判断一下是否到克制时间,那么服从就会高许多。

(4)NioEventLoop.run()方法实行使命总结
Netty里的使命分两种:一种是平凡的使命,另一种是定时的使命。Netty在实行这些使命时起首会把定时使命聚合到平凡使命队列里,然后再从平凡使命队列里获取使命逐个实行,而且是每实行64个使命之后才判断一下当前时间是否高出最大答应实行时间。假如高出就直接停止,停止之后就会举行下一次NioEventLoop.run()方法的for循环。

10.NioEventLoop总结
(1)NioEventLoop的实行流程总结
(2)Reactor线程模子总结
(3)NioEventLoop创建启动实行的总结

(1)NioEventLoop的实行流程总结
一.NioEventLoop在实行过程中起首会不停检测是否有IO变乱发生,然后假如检测出有IO变乱就处置惩罚IO变乱,接着处置惩罚完IO变乱之后再处置惩罚外部线程提交过来的异步使命。

二.在检测是否有IO变乱发生时,为了包管异步使命的及时处置惩罚,只要有使命要处置惩罚,那么就立刻克制检测行止置惩罚使命。

三.外部线程异步实行的使命分为两种:平凡使命和定时使命。这两种使命分别生存到MPSC队列和优先级队列,而优先级队列中的使命终极都会转移到MPSC队列里举行处置惩罚。

四.Netty每处置惩罚完64个使命才会查抄一次是否超时而退出实行使命的循环。

(2)Reactor线程模子总结
一.NioEventLoopGroup在用户代码中被创建,默认情况下会创建两倍CPU核数个NioEventLoop。

二.NioEventLoop是懒启动的,bossNioEventLoop在服务端启动时启动,workerNioEventLoop在新毗连接入时启动。

三.当CPU核数为2的幂时,为每一个新毗连绑定NioEventLoop之后,都会做一个取模运算转位与运算的优化。

四.每个毗连都对应一个Channel,每个Channel都绑定唯逐一个NioEventLoop,一个NioEventLoop大概会被绑定多个Channel,每个NioEventLoop都对应一个FastThreadLocalThread线程实体和一个Selector。因此单个毗连的全部操纵都是在一个线程上实行的,以是是线程安全的。

五.每个NioEventLoop都对应一个Selector,这个Selector可以批量处置惩罚注册到它上面的Channel。

六.每个NioEventLoop的实行过程都包罗变乱检测、变乱处置惩罚以及异步使命的实行。

七.用户线程池在对Channel举行一些操纵时均为线程安全的。这是由于Netty会把外部线程的操纵都封装成一个Task放入这个Channel绑定的NioEventLoop中的MPSC队列,然后在该NioEventLoop的实行过程(变乱循环)的第三个过程中举行串行实行。

八.以是NioEventLoop的职责不光仅是处置惩罚网络IO变乱,用户自界说的平凡使命和定时使命也会同一由NioEventLoop处置惩罚,从而实现线程模子的同一。

九.从调治层看,也不存在从NioEventLoop线程中再启动其他范例的线程用于异步实行别的的使命,从而克制了多线程并发操纵和锁竞争,提拔了IO线程的处置惩罚性能和调治性能。

(3)NioEventLoop创建启动实行的总结
一.用户在创建bossGroup和workerGroup时,NioEventLoopGroup被创建,默认不传参时会创建两倍CPU核数个NioEventLoop。

二.每个NioEventLoopGroup都有一个线程实行器executor和一个线程选择器chooser。线程选择器chooser用于举行线程分配,它会针对NioEventLoop的个数举行优化。

三.NioEventLoop在创建时会创建一个Selector和一个MPSC使命队列,创建Selector时Netty会通过反射的方式用数组去更换Selector里的两个HashSet数据结构。

四.Netty的NioEventLoop在初次调用execute()方法时会启动线程,这个线程是一个FastThreadLocalThread对象。启动线程后,Netty会将创建完成的线程生存到成员变量,以便能判断实行NioEventLoop里的逻辑的线程是否是这个创建好的线程。

五.NioEventLoop的实行逻辑在run()方法里,重要包罗3部分:第一是检测IO变乱,第二是处置惩罚IO变乱,第三是实行异步使命。

回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表