reactor thread netty source code analysis of (a)

netty is the reactor core thread, the corresponding item in the use of a wide range of NioEventLoop, then NioEventLoop which in the end in doing things? How netty is to ensure timely implementation and effective polling task event loop? And how to fix gracefully out of nio bug jdk?

reactor thread start

NioEventLoop run method is the main thread of the reactor was started execute method NioEventLoop parent SingleThreadEventExecutor in the first time to add the task

@Override
public void execute(Runnable task) {
    ...
    boolean inEventLoop = inEventLoop();
    if (inEventLoop) {
        addTask(task);
    } else {
        startThread();
        addTask(task);
        ...
    }
    ...
}

External thread adding a task to task execution queue inside when startThread (), netty will judge reactor thread has not been started, if not start, then start the thread task queue and then further add tasks inside

private void startThread() {
    if (STATE_UPDATER.get(this) == ST_NOT_STARTED) {
        if (STATE_UPDATER.compareAndSet(this, ST_NOT_STARTED, ST_STARTED)) {
            doStartThread();
        }
    }
}

SingleThreadEventExecutor doStartThread in the implementation of calls inside the actuator executor execute method, calls a process run method NioEventLoop packaged as a thread to a runnable execution stuffed

private void doStartThread() {
    ...
    executor.execute(new Runnable() {
        @Override
        public void run() {
            thread = Thread.currentThread();
            ...
                SingleThreadEventExecutor.this.run();
            ...
        }
    }
}

The executor thread is created, the corresponding netty the reactor thread entity. The default executor is ThreadPerTaskExecutor

By default, ThreadPerTaskExecutor creates a FastThreadLocalThread thread through DefaultThreadFactory each time the execute method of execution, and this thread is netty entity in the reactor thread

ThreadPerTaskExecutor

public void execute(Runnable command) {
    threadFactory.newThread(command).start();
}

And about why a combination ThreadPerTaskExecutor DefaultThreadFactory to a new FastThreadLocalThread, not described in detail herein, by the code will be briefly described in the following paragraphs

  • Netty standard procedure calls to the parent class MultithreadEventExecutorGroup NioEventLoopGroup the following code
protected MultithreadEventExecutorGroup(int nThreads, Executor executor,
                                        EventExecutorChooserFactory chooserFactory, Object... args) {
    if (executor == null) {
        executor = new ThreadPerTaskExecutor(newDefaultThreadFactory());
    }
}
  • NioEventLoop then passed to the manner by newChild
@Override
protected EventLoop newChild(Executor executor, Object... args) throws Exception {
    return new NioEventLoop(this, executor, (SelectorProvider) args[0],
        ((SelectStrategyFactory) args[1]).newSelectStrategy(), (RejectedExecutionHandler) args[2]);
}

About reactor thread creation and started to talk about so much, we summarize: netty thread is created in the reactor add a task, the thread entity FastThreadLocalThread, the last threads of execution run as the main method of NioEventLoop

Guess you like

Origin blog.csdn.net/weixin_34059951/article/details/90929365