C # Set queue,
A queue is a set of elements processed in FIFO mode.
The Queue is implemented by using the wildcard class Queue in the System. Collections. Generic namespace <T>. Internally, the Queue <T> class uses arrays of the T type, which is similar to the List <T> (http://www.cnblogs.com/afei-24/p/6824791.html) type. The ICollection and IEnumerable APIs are implemented in the queue, but the ICollection <T> APIs are not implemented. Therefore, the Add () and Remove () methods defined in the ICollection <T> APIs cannot be used in the queue.
The Enqueue () method adds elements to one end of the queue, and the Dequeue () method reads and deletes elements from the other end of the queue. Calling Dequeue () again will delete the next element of the queue:
Methods and attributes of the Queue <T> class:
When creating a queue, you can use a constructor similar to List <T>, or you can use the constructor to specify the capacity.
The default constructor of the non-generic Queue class is different. It creates an empty array containing 32 items.
The following is an example of a queue. One thread is used to add documents to the queue, and the other thread is used to read and process documents from the queue:
// The elements stored in the queue are of the Document Type public class Document {public string Title {get; private set;} public string Content {get; private set;} public Document (string title, string content) {this. title = title; this. content = content ;}/// DocumentManager class is an external layer of Queue <Document>. This Document is used to add documents to and obtain documents from queues. public class DocumentManager {private readonly Queue <Document> documentQueue = new Queue <Document> (); // because multiple threads access the DocumentManager class, use the lock statement to lock the access to the queue public void AddDocument (Document doc) {lock (this) {documentQueue. enqueue (doc) ;}} public Document GetDocument () {Document doc = null; lock (this) {if (this. isDocumentAvailable) doc = documentQueue. dequeue ();} return doc;} p Ublic bool IsDocumentAvailable {get {lock (this) {return documentQueue. Count> 0 ;}}// use the ProcessDocuments class to read and delete documents in the queue in a separate task. Public class ProcessDocuments {// the only method that can be accessed from outside is the Start () method // instantiate a new task in Start. Create a ProcessDocuments object and call the Run () method of ProcessDocuments public static void Start (DocumentManager dm) {Task. factory. startNew (new ProcessDocuments (dm ). run);} protected ProcessDocuments (DocumentManager dm) {if (dm = null) throw new ArgumentNullException ("dm"); documentManager = dm;} private DocumentManager documentManager; // define an infinite loop. Use the IsDocumentAvailable attribute of the DocumentManager class to determine whether there are documents in the queue. Protected void Run () {while (true) {if (documentManager. IsDocumentAvailable) {Document doc = documentManager. GetDocument (); if (doc! = Null) Console. WriteLine ("Processing document {0}", doc. Title);} Thread. Sleep (new Random (). Next (20 ));}}}
Client code
static void Main() { var dm = new DocumentManager(); ProcessDocuments.Start(dm); ProcessDocuments.Start(dm); // Create documents and add them to the DocumentManager for (int i = 0; i < 1000; i++) { Document doc = new Document("Doc " + i.ToString(), "content"); dm.AddDocument(doc); Console.WriteLine("Added document {0}", doc.Title); Thread.Sleep(new Random().Next(20)); } Console.ReadKey(); }