I. About this article
The pchelper class implemented in this article is a simple producer-consumer operation tool class. This class can achieve the following goals: multiple threads write data to one of the queue, and multiple threads are responsible for receiving data from the queue for processing.
Ii. Tool code
/// <Summary> /// producer-consumer operation class // </Summary> public class pchelper {readonly object listlock = new object (); // thread lock system. collections. queue queue = new system. collections. queue (); // queue // <summary> // queue name: used to identify a queue // </Summary> private string _ queuename; /// <summary> // queue name: used to identify a queue // </Summary> Public String queuename {get {return _ queuename ;} private set {_ queuename = value ;}/// <summary> // queue threshold value: when the queue length exceeds the threshold, an exception is thrown. // </Summary> Public uint _ threshold; // <summary> // The queue threshold: an exception is thrown when the queue length exceeds the threshold. // </Summary> Public uint threshold {get {return _ threshold;} private set {_ Threshold = value ;}} /// <summary> // producer-consumer operation class // </Summary> /// <Param name = "queuename"> queue name </param> /// <Param name = "threshold"> queue data volume threshold: an exception occurs when the threshold value is exceeded. </param> Public pchelper (string queuename = "", uint Threshold = 300) {queuename = queuename; Threshold = threshold ;} /// <summary> /// producer function: add a data entry to the queue /// </Summary> /// <Param name = "O"> </param> Public void produce (Object O) {lock (listlock) {If (queue. count <threshold) {queue. enqueue (o); system. threading. monitor. pulse (listlock);} else {Throw new exception ("queue too long, failed to join"); }}/// <summary> // consumer function: retrieve data from the queue /// </Summary> /// <returns> retrieve the first data from the queue </returns> Public object consume () {lock (listlock) {While (queue. count = 0) {system. threading. monitor. wait (listlock) ;}} return queue. dequeue () ;}//< summary> /// clear data /// </Summary> Public void cleardata () {lock (listlock) {queue. clear ();}} /// <summary> /// number of data in the queue /// </Summary> /// <returns> </returns> Public int datacount () {int C; lock (listlock) {c = queue. count;} return C ;} /// <summary> /// data type in the queue /// </Summary> /// <returns> </returns> Public type datatype () {type T; lock (listlock) {T = queue. getType () ;}return t ;}}
Iii. Test code
The program contains the main function and the related function to be called by the main function.
The produceloop function is used to constantly write data to the queue.
The consumeloop function is used to continuously retrieve data from the queue.
In this Code, the time interval of each cosumeloop loop is set to be longer than 5 times that of produceloop.
Therefore, when the queue reaches the set threshold (the default value 300 is used in this Code), the tool class reports an exception.
Class program {static pchelper queue; static int I; static void main (string [] ARGs) {queue = new pchelper ("queuetest"); I = 0; (new system. threading. thread (produceloop )). start (10); (new system. threading. thread (consumeloop )). start (50); console. readline () ;}//< summary> /// constantly insert data into the queue /// </Summary> Public static void produceloop (Object sleeptime) {While (true) {queue. produce (I); console. writeline (string. format ("produce: {0} dataleft: {1}", I, queue. datacount (); I ++; system. threading. thread. sleep (Int. parse (sleeptime. tostring (); }}/// <summary> // constantly retrieve data from the queue /// </Summary> Public static void consumeloop (Object sleeptime) {While (true) {string temp = queue. consume (). tostring (); console. writeline (string. format ("consume: {0} dataleft: {1}", temp, queue. datacount (); system. threading. thread. sleep (Int. parse (sleeptime. tostring ()));}}}
Iv. Test Results
End