Netty - Thread Model Reactor

tags: netty  

table of Contents

Thread model

1, traditional IO service model

2, Reactor mode

reactor

Three modes:

to sum up

Netty model

Excommissum


Thread model

1, traditional IO service model

Blocked IO mode Get input data, each link requires a separate thread to complete data transmission and processing

When it is large, create a large number of threads, occupying system resources; if the thread is created, if there is no data reading, it will be blocked.

 

2, Reactor mode

reactor

Two shortcomings for traditional IO models, solutions:

a, Based on IO Multiplexing Model: Multiple connections share a blocking object. The application only needs to wait on one blocking object, no need to block all objects

b, based on thread pool reuse thread resources: no longer create threads for each connection, assign the connection completed service processing task to thread pool processing

IO multiplexed combined with thread pool is the basic idea of ​​Reactor model, its core composition:

A, Reactor: Running in a separate thread, responsible for monitoring and distributing events, such as ServiceHandler (EventDispatch) as above

B, Handlers: Specific IO Event

Three modes:

1, single Reactor single thread

 

Reactor / handler runs in a thread.

Advantages: no thread competition

Disadvantages: I can't stop high-concurrent, reliability, termination outside the thread or enter the dead cycle

Application: The number of customers is limited; business processing is very fast

2, single Reactor multi-thread

 

For single-threaded concurrency, derived a single Reactor multi-thread model.

1. Single Reactor is responsible for listening, establishing a connection, reading and writing IO

2, business logic thread pool is responsible for handling specific IO reading and writing and business logic

Advantages: Take advantage of the performance of computer multi-core, resistant to relatively high concurrency

Disadvantages: Single Reactor, in an ultra-high concurrent scene, it will become a bottleneck. Because Reactors are responsible for listening, connecting, reading and writing IO, this block is a single-threaded series.

3, the master from Reactor multi-thread

 

In order to deal with ultra-high concurrent, we continue to optimize and split Reactor on the basis of Reactor single-threaded operation:

1, the main Reactor is responsible for listening and connection, these two steps are very fast

2, from the Reactor, read and write IO data

3, business logic thread pool is responsible for handling business logic

to sum up

Single Reactor single thread: small restaurant, front desk, chef, waiter is a person, from ordering, cooking, dinner table is serial

Single Reactor multi-thread: big restaurant, front desk, chef is a person, the waiter is a group of people, the speed is fast, some

Lord from Reactor multi-thread: five-star hotel: a person in the front desk, a group of chefs, the waiter is a group of people, the speed of the meal

advantage:

1, fast response, do not have to block all the threads, but shares a blocked object

2, the extension performance can be extended from the number of reactors to make full use of CPU resources

3, good reachenability, Reactor model itself is independent of specific event processing logic, with high reuse.

Netty model

Netty's thread model is based on the master's multi-threaded mode and has made some extent on the basis:

1, BossGroup thread pool maintenance master Selector, only pay attention to ACCECPT

2, after receiving the Accept event, get the corresponding SocketChannel, encapsulate into the NiosocketChannel registration to the Worker thread cycle.

3. After the Worker thread listens to the time you are interested, it will be handled by Handler.

 

Netty's thread model is from Reactor in the thread pool, which is an improvement relative to Reactor.

 

Excommissum

 

1, Netty abstraction Two thread pools: BossGroup is responsible for listening and establishing a connection; workergroup is responsible for the reading and writing of network IO

2, bossgroup and workergroup types are NiOEventLoopGroup, equivalent to an event cycle group, which contains multiple event loops, each event loop is NiOEventLoop

3, nioeventloop Represents a Selector, user listening to the socket network communication bound to ride

4, each Boos NioEventLoop loop execution 3 step:

a, polling an Accept event

b, establish a connection, generate NiosocketChannel, and register to workergroup

c, handle the task in the task queue, ie RunAllTasks

5, each worker nioeventloop loop execution 3 step:

a, polling reading and writing time

b, process the IO time, processed on the corresponding NiosocketChannel

c, handling the task of the task queue, ie RunAllTasks

Intelligent Recommendation

Netty-(4) Netty's thread model and Reactor mode

1.Reactor mode 1) Brief description Design mode-Reactor mode (reactor design mode) is aEvent-driven design pattern, In an event-driven application, the service of one or more customersRequest separati...

Netty study notes (3) Reactor thread model

Single thread model All operations are processed in the same NIO thread. In this single thread, it is responsible for receiving requests, processing IO, encoding and decoding all operations, which is ...

Detailed explanation of netty reactor thread model and EventLoop

In this article, the Netty Reactor thread model and EventLoop Articles directory EventLoop event cycle Task scheduling Thread management Thread allocation Non -blocking transmission Obstructive transm...

What is Netty, Reactor thread model and NIO

Netty provides asynchronous, event -driven network application frameworks and tools to quickly develop high -performance, high -reliability network server and client programs. That is,NettyIt is a NIO...

Thread model: single Reactor single thread, single Reactor multithread, master-slave Reactor multithread, netty model

1) Reactor mode: Principle: One or more clients send requests to the server-side processor. After the server-side processor processes the request, it uses IO multiplexing to monitor events and sends t...

More Recommendation

Three types of Reactor thread model and Netty thread model

The basics in this article are based on non-blocking IO and asynchronous IO. For blocking IO, the following programming models are hardly applicable Three thread models of Reactor Single-threaded mode...

Handwritten reactor (netty reactor model)

                                      Donate to developers Driven by interest, write afreeThere is joy an...

Netty learning journey ------ thread model pre-reactor Reactor reactor design pattern implementation (based on java.nio)

<Transfer from http://blog.csdn.net/prestigeding/article/details/55100075?locationNum=11&fps=1> Copyright statement: This article is an original article of the blogger and may not be reprodu...

Netty--Reactor model

The previous blog talked about Netty creating event loop group objects. This blog post gives an introduction to the specific role played by event loop group objects in Netty's Reactor model. Reactor m...

Netty Reactor model

Whether it is a network framework written in C++ or Java, most of them are designed and developed based on Reactor mode. Reactor mode is based on event-driven, especially suitable for Handle massive I...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top