A simple and lightweight concurrent thread-assisted class for concurrent execution of methods
In practical applications, threads are essential for concurrent execution of multiple methods to save running time. multi-thread management is often a headache, for example, a simple and lightweight method-based Concurrent execution thread helper class is shared here.
Two goals of the thread management helper class:
1. Multiple thread methods are executed in parallel, and the main thread is waiting. You need to know that all sub-threads have completed the execution;
2. Set the timeout time for Asynchronous Method execution. You can skip this method for timeout and the main thread returns the result directly;
3. Lightweight. Although Microsoft provides available components such as thread wait and timeout, such as ManualResetEvent, It is a kernel object that occupies a large amount of system resources.
Design implementation:
1. This class maintains two internal variables. The total number of threads in the asynchronous execution method is totalThreadCount, and the data in the currently executed thread is currThreadCount;
2. Two open methods: WaitAll: Wait for execution, and SetOne: Set the execution of the method to complete. SetOne is called after each method is completed. The number of currThreadCount is increased by 1. WaitAll checks whether currThreadCount and totalThreadCount are equal, equals indicates that all methods are executed and return;
3. For thread security, Interlocked is used for Variable Processing of currThreadCount.
Code implementation:
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading; 6 7 namespace Loncin. codeGroup10.Utility 8 {9 public class ThreadHelper10 {11 /// <summary> 12 /// total number of threads 13 /// </summary> 14 private int totalThreadCount; 15 16 /// <summary> 17 /// the number of threads completed for the current execution is 18 /// </summary> 19 private int currThreadCount; 20 21 /// <summary> 22 /// constructor 23 /// </summary> 24 // <param name = "totalThreadCount"> total number of threads </param> 25 public ThreadHelper (int totalThreadCount) 26 {27 Interlocked. exchange (ref this. totalThreadCount, totalThreadCount); 28 Interlocked. exchange (ref this. currThreadCount, 0 ); 29} 30 31 // <summary> 32 // wait for all threads to finish executing 33 // </summary> 34 // <param name = "overMiniseconds"> timeout time (MS) </param> 35 public void WaitAll (int overMiniseconds = 0) 36 {37 int checkCount = 0; 38 39 // spin 40 while (Interlocked. CompareExchange (ref this. currThreadCount, 0, this. totalThreadCount )! = This. totalThreadCount) 41 {42 // if (overMiniseconds> 0) 44 {45 if (checkCount> = overMiniseconds) 46 {47 break is returned when the timeout value is exceeded; 48} 49} 50 51 checkCount ++; 52 53 Thread. sleep (1); 54} 55} 56 57 // <summary> 58 // set the semaphore, indicates that the execution of a single thread is completed 59 // </summary> 60 public void SetOne () 61 {62 Interlocked. increment (ref this. currThreadCount); 63} 64} 65}View Code
Example:
1 public class ThreadHelperTest 2 {3 /// <summary> 4 // thread help class 5 /// </summary> 6 private ThreadHelper threadHelper; 7 8 public void Test () 9 {10 // enable the method to execute 11 int funcCount = 5; 12 13 this. threadHelper = new ThreadHelper (funcCount); 14 15 for (int I = 0; I <funcCount; I ++) 16 {17 Action <int> action = new Action <int> (TestFunc); 18 action. beginInvoke (I, null, null); 19} 20 21 // wait for method execution. The timeout time is 12 ms, after 12 ms Force end 22 threadHelper. WaitAll (12); 23 24 Console. WriteLine ("all methods have been executed! "); 25} 26 27 private void TestFunc (int I) 28 {29 try30 {31 Console. WriteLine (" method {0} execution! "); 32 Thread. Sleep (10); 33} 34 finally35 {36 // method execution end 37 this. threadHelper. SetOne (); 38} 39} 40}View Code
Summary:
1. This thread help class is only a simple thread management class and lacks many functions, such as exception handling. However, it is generally used.