In daily development, we may use queues, which are a storage structure that can achieve "first-in, first-out".
Application scenarios: write logs, send emails, etc
If we need to record the interface log of each user requesting our data, we don't need to write a log every time the user requests it, we can put the log content in the queue and let other threads execute it, so as not to affect the efficiency of the application.
Another advantage is that adding data to the queue in the case of concurrency, using a single thread to execute the data of the queue can avoid many exceptions, if concurrency (multi-threaded), access to the same physical file will cause exceptions. (subject to availability)
Queue universal approach:
This is an abstract class, and you need to inherit this abstraction class and implement the Execute abstraction method. Because I don't know what type of data you want to process, generics are used here, that is, Entity is of string type, method parameters are of string type, and the data you are processing is string type data.
ConcurrentQueue thread-safe queue ensures that there will be no exceptions in a multi-threaded environment.
ManualResetEvent to control the thread's signal,In the past, we checked the queue for data every few seconds or minutesNow, we add the data to the queue and only need to pass a signal!
Use:
(End)
|