In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this "C# multi-stage parallel thread engineer case analysis" article, so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article, let's take a look at this "C# multi-stage parallel thread engineer case analysis" article.
Preface
The main purpose of the application scenario is to control N threads (threads that can be increased or decreased at any time), so that multi-threads can be synchronized in M phases.
The thread works as follows:
Next we will learn Barrier in C #, which is used to implement parallel collaborative work.
Barrier class
Enables multiple tasks to work together in multiple stages in a parallel manner according to an algorithm, and enables multiple threads (called "participants") to process the algorithm in stages at the same time.
You can make multiple threads (called "participants") process the algorithm in phases at the same time. (pay attention to the word algorithm)
Each participant will be prevented from continuing execution after completing the phase task until all participants have reached the same stage.
The constructor of Barrier is as follows:
The constructor explains that Barrier (Int32) initializes a new instance of the Barrier class. Barrier (Int32, Action) initializes a new instance of the Barrier class.
One of the constructors is defined as follows:
Public Barrier (int participantCount, Action postPhaseAction)
ParticipantCount: the number of threads in, greater than 0 and less than 32767.
PostPhaseAction: Action (delegate) is performed after each phase.
Properties and methods
Before we know what this class does, let's take a look at the common properties and methods of this class.
After an overview of the common properties and methods of Barrier, we began to write sample code.
Attributes:
Property indicates that the CurrentPhaseNumber gets the number of the current phase of the barrier. ParticipantCount gets the total number of participants in the barrier. ParticipantsRemaining gets the number of participants in the barrier who have not yet signaled at the current stage.
Methods:
The method description AddParticipant () notifies Barrier that there will be another participant. AddParticipants (Int32) notifies Barrier that there will be multiple other participants. RemoveParticipant () notifies Barrier that it will lose one participant. RemoveParticipants (Int32) notifies Barrier that it will reduce some participants. SignalAndWait () sends out that the participant has reached the barrier and waits for all other participants to also reach the barrier. SignalAndWait (CancellationToken) signals that the participant has reached the barrier and waits for all other participants to reach the barrier while watching the unmark. SignalAndWait (Int32) signals that the participant has reached the barrier and waits for all other participants to reach the barrier, while measuring the timeout using a 32-bit signed integer. SignalAndWait (Int32, CancellationToken) signals that the participant has reached the barrier and waits for all other participants to reach the barrier, using a 32-bit signed integer to measure the timeout while observing the unmark. SignalAndWait (TimeSpan) signals that the participant has reached the barrier and waits for all other participants to reach the barrier, while measuring the time interval using the TimeSpan object. SignalAndWait (TimeSpan, CancellationToken) signals that the participant has reached the barrier and waits for all other participants to reach the barrier, using the TimeSpan object to measure the time interval while observing the unmark.
The Barrier translation barrier, the "phase" mentioned earlier, is called a barrier in the documentation, and there are some official examples and practical scenarios:
Https://docs.microsoft.com/zh-cn/dotnet/standard/threading/barrier?view=netcore-3.1
Https://docs.microsoft.com/zh-cn/dotnet/standard/threading/how-to-synchronize-concurrent-operations-with-a-barrier?view=netcore-3.1
The tutorial in this article is relatively simple. You can take a look at this tutorial first and then take a look at the official examples.
Example
Suppose there is a competition, one has three stages, and there are three groups participating in the competition.
There are three stages of the game. After the group finishes one step, you can go to the waiting area to have a rest and wait for other groups to finish the game, and then start the next stage of the game.
Examples are as follows:
New Barrier (int,Action) sets how many threads participate, and the Action delegate sets what actions are performed after each phase is completed.
.SignalAndWait () prevents the current thread from continuing to execute; until other completions are executed as well.
Class Program {/ / Barrier (Int32, Action) private static Barrier barrier = new Barrier (3, b = > Console.WriteLine ($"\ n{ b.CurrentPhaseNumber + 1} session is over, please score!")) ; static void Main (string [] args) {/ / Random simulates the time required for each group to complete a segment of the game Thread thread1 = new Thread (() = > DoWork ("first group", new Random (). Next (2, 10)); Thread thread2 = new Thread (() = > DoWork ("second group", new Random (). Next (2, 10) Thread thread3 = new Thread (() = > DoWork ("Group 3", new Random (). Next (2, 10)); / / three groups start the competition thread1.Start (); thread2.Start (); thread3.Start (); Console.ReadKey () } static void DoWork (string name, int seconds) {/ / first stage Console.WriteLine ($"\ n {name}: start to enter the first stage of the game"); Thread.Sleep (TimeSpan.FromSeconds (seconds)) / / Console.WriteLine ($"\ n {name}: finish the first part of the game, wait for other groups"); / / the group completes the task of the stage, go to rest and wait for other groups to finish the game barrier.SignalAndWait () / / the second stage Console.WriteLine ($"\ n {name}: start to enter the second stage of the competition"); Thread.Sleep (TimeSpan.FromSeconds (seconds)); Console.WriteLine ($"\ n {name}: finish the second stage of the game and wait for other groups\ n"); barrier.SignalAndWait () / / third stage Console.WriteLine ($"\ n {name}: start to enter the third stage of the competition"); Thread.Sleep (TimeSpan.FromSeconds (seconds)); Console.WriteLine ($"\ n {name}: finish the third stage of the game and wait for other groups\ n"); barrier.SignalAndWait ();}}
In the above example, each thread uses the DoWork () method to do the same thing. Of course, you can set up multiple threads to perform different tasks, but you must ensure that each thread has the same number of .SignalAndWait (); methods.
Of course, SignalAndWait () can set the wait time, and if other threads don't get there for a long time, continue to run. Deadlocks and other problems can be avoided.
So far, only SignalAndWait () has been used, so let's move on to other methods of the Barrier class.
New example
Barrier.AddParticipant (): add a participant
Barrier.RemoveParticipant (): remove participants
Continue to use the example in section 2.
Because this is a game, always waiting for other groups will make the game go on slowly.
New rule: there is no need to wait for the last place, when only the last place is left for the completion, other groups can immediately proceed to the next stage of the competition.
Of course, the last group has the right to continue to finish the race.
Modify the code in the second section by adding barrier.RemoveParticipant (); to the first line of the Main.
Static void Main (string [] args) {barrier.RemoveParticipant ();...
Try to run it again.
Description
SignalAndWait () is overloaded, such as SignalAndWait (CancellationToken), so I won't explain how to use this method here. Wait until writing to the later Task, readers learn the relevant knowledge points, we review again, so that from easy to difficult, natural and natural.
Barrier is suitable for performing the work of the same process at the same time, because the work content is the same and is easy to coordinate. Workflow might come in handy.
The above is about the content of the article "C# Multi-stage parallel Thread engineer case Analysis". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 228
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.