What does Netty use to handle threads?

The common thread pooling mode is

  • Select a Thread from the pool’s free task list and assign it to pick and run a committed task
  • When the task is complete, the Thread is returned to the list, making it reusable

The programming constructs that run task processing are often called event loops, which Netty describes using EventLoop. An EventLoop will be driven by a Thread that never changes, can be assigned to multiple channels, and tasks can be executed immediately after being submitted to the EventLoop or scheduled for execution. Tasks are executed in first-in, first-out order.

What does Netty use for task scheduling?

The JDK’s ScheduledExecutorService in the Concurrent package performs scheduling. As part of thread management, additional threads are created, but this can be a bottleneck when a large number of tasks are scheduled tightly. EventLoop inherits it. And task scheduling does not have this problem.

To implement delayed execution, simply call the Schedule method, which calls scheduledAtFixedRate each time it is executed, and cancel it by calling the returned scheduledFuture

What is the execution logic of EventLoop?



After getting the current thread of execution, determine if it is the thread assigned to the eventLoop. If it is, execute it directly; otherwise, put it in a queue and execute it later. This mode is the excellence of Netty thread mode without worrying about thread safety and synchronization

What is the difference in thread allocation between asynchronous and synchronous transports for eventLoop?

Asynchronous transports use a small number of Eventloops and their corresponding threads to support multiple channels through a single Thread (thus allowing a small number of threads to support a large number of channels), while synchronous transports use one Thread per channel