Netty (14)-Overview of Threading Model

1. Basic introduction

1) Different threading models have a great impact on the performance of the program.
2) The threading models that currently exist are:

  • Traditional blocking I/O service model
  • Reactor model

3) According to the number of Reactor and the number of processing resource pool threads, there are three typical implementations:

  • Single Reactor single thread
  • Single Reactor multi-threaded
  • Master-slave Reactor multithreading

4) Netty threading model (Netty is mainly based on the master-slave Reactor multi-threading model and has made certain improvements, of which the master-slave Reactor multi-threading model has multiple Reactors)

2. Traditional blocking I/O service model

2.1 Working principle diagram

Traditional blocking I/O service model
The yellow box represents the object, the blue box represents the thread, and the white box represents the method (API)

2.2 Model characteristics

1) Adopt the blocking IO model to obtain the input data
2), each connection requires an independent thread to complete data input, business processing and data return

2.3 Problem analysis

1) When the number of concurrency is large, a large number of threads will be created, occupying a lot of system resources.
2) After the connection is created, if the current thread has no data to read temporarily, the thread will be blocked in the read operation, resulting in a waste of thread resources

Three, Reactor model

Reactor corresponds to Chinese translation:

  • Reactor mode
  • Dispatcher mode (dispatcher)
  • Notifier mode (notifier)

In view of the shortcomings of the traditional blocking I/O service model, the solutions are:
1) Based on the I/O reuse model: multiple connections share one blocking object, and the application only needs to wait on one blocking object, without blocking waiting for all connections. When a connection has new data that can be processed, the operating system informs the application that the thread returns from the blocked state and starts business processing.
2) Reuse thread resources based on the thread pool: no need to create threads for each connection, and connect The completed business processing tasks are allocated to threads for processing, and one thread can handle multiple connected businesses
Reactor model sketch

3.1 Working principle diagram

Reactor model working principle diagram

I/O reuse combined with thread pool is the basic idea of ​​the Reactor model.

Description:
1) Reactor model, a mode in which one or more inputs are passed to the service processor at the same time (based on event-driven)
2) The server-side program processes multiple incoming requests and assigns them to the corresponding processing threads. Therefore, the Reactor model is also called dispatcher model
3). The Reactor model uses IO to multiplex and monitor events. After receiving the event, it is distributed to a certain thread (process). This is the key to high concurrency processing of network services.

3.2 Core composition

1) Reactor: Reactor runs in a separate thread, is responsible for monitoring and distributing events, and distributing them to appropriate handlers to respond to IO events. It is like a company's telephone operator, it takes calls from customers and transfers the line to the appropriate contact.
2) Handlers: The actual events to be completed by the handler to execute the IO event, similar to the actual officials in the company that the customer wants to talk to. Reactor responds to IO events by dispatching appropriate handlers, and the handlers perform non-blocking operations.

3.2 Classification

  • Single Reactor single thread
  • Single Reactor multi-threaded
  • Master-slave Reactor multithreading

Guess you like

Origin blog.csdn.net/yangxshn/article/details/113842513