======================================================== Document. CS
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; namespace consoleapplication2 {public class document // text class {// Title Public String title {Get; private set ;}// content Public String content {Get; private set ;} public document (String title, string content) {This. title = title; this. content = content;} public override string tostring () {return string. format ("title: {0}; content: {1};", this. title, this. content );}}}
======================================================== Documentmanage. CS
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; using system. collections; namespace consoleapplication2 {public class documentmanage // text operation class {queue <document> Doc = new queue <document> (); // Add the public void adddocument (document D) {lock (this) element to the queue // synchronization operation Doc. enqueue (d);} // read the Header element and delete the public document getdocument () {lock (this) return Doc. dequeue ();} // determines whether the public bool isavailabledocument {get {return Doc is an element in the queue. count> 0 ;}}}}
====================================================== Processdocument. CS
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; using system. threading; namespace consoleapplication2 {public class processdocument // enable the thread to read the elements in the document {private documentmanage DM; Public processdocument (documentmanage d) {dm = D ;} /// <summary> /// enable the thread to read the elements in the document /// </Summary> /// <Param name = "D"> </param> Public static void start (documentmanage D) {New thread (New processdocument (d ). run ). start ();} private void run () {While (true) {If (DM. isavailabledocument) // checks whether the queue has any elements {console. writeline (DM. getdocument (). tostring ());}}}}}
============================================ Main program
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; namespace consoleapplication2 {class program {static void main (string [] ARGs) {documentmanage dm = new documentmanage (); processdocument. start (DM); // enable the thread to read the document for (INT I = 0; I <10000; I ++) // Add 10000 elements to the queue {document DOC = new document (I. tostring (), I. tostring (); DM. adddocument (DOC); console. writeline (Doc. tostring () + "..... new ");} console. readkey ();}}}
650) This. width = 650; "style =" width: 736px; Height: 285px; "Title =" spximage51.jpg "src =" http://s3.51cto.com/wyfs02/M02/47/81/wKioL1P7bGbBX1wwAAInY2WYhJU205.jpg "width =" 780 "Height =" 279 "alt =" wkiol1p7bgbbx1wwaainy2wyhju205.jpg "/>
This article from the "programmer's home" blog, please be sure to keep this source http://962410314.blog.51cto.com/7563109/1544977
Queue <t> queue