This article is a mirror article of machine translation, please click here to jump to the original article.

View: 6386|Reply: 3

[Source] .NET Core high-performance queue Channels is used

[Copy link]
Posted on 2023-5-8 22:11:01 | | | |
System.Threading.Channels isNew collection types introduced after .NET Core 3.0It can be used to queue messages, produce and consume data, and the public Writer and Reader APIs correspond to the producers and consumers of messages, which also makes Channel more concise and easy to use.

.NET/C# is based on the BlockingCollection background task queue
https://www.itsvse.com/thread-9528-1-1.html


System.Threading.Channels library:The hyperlink login is visible.
Channel<T> class:The hyperlink login is visible.

Channel type

Microsoft offers two types of channels, one is a bound channel with limited capacity and one is an unbound channel with unlimited capacity.

Create a channel with limited capacity:Channel.CreateBounded<string>(100);
Create a channel with unlimited capacity:Channel.CreateUnbounded<string>();

Create unlimited channels

When creating a channel, you can set the SingleWriter and SingleReader of ChannelOptions to specify a single producer and consumer when the channel is set to false, when SingleWriter = true is set, it will limit only one producer to write data at the same time, SingleReader = true is the same.


Try opening three producer threads and one consumer thread with the following code:



The consumer uses WaitToReadAsync to wait for the data to be read. Consumers attempt to read the data only after it is available.


In the above code, consumers read data one by one to perform operations, how to make itBulk consumer consumption dataWhat about a piece of processing?

Create a new ChannelReaderExtensions extension with the following code:


Adjust the consumer code as follows:



(End)




Previous:ASP.NET Core (18) Customize a simple OutputCache output cache
Next:[Actual combat]. NET/C# offline IP address (home) locator library
Posted on 2023-5-8 22:50:27 |
Learn to learn
Posted on 2023-5-12 10:07:46 |
Learn to learn
Posted on 2023-5-16 17:21:37 |
Learn to learn
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com