您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
=====================================Document.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { 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; } public override string ToString() { return string.Format("标题:{0};内容:{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//文本操作类 { Queue<Document> doc = new Queue<Document>(); //向队列中添加元素 public void AddDocument(Document d) { lock (this)//同步操作 doc.Enqueue(d); } //读取头元素,并删除 public Document GetDocument() { lock (this) return doc.Dequeue(); } //判断队列中有没有元素 public bool IsAvailableDocument { get { return doc.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//开启线程读取文档中的元素 { private DocumentManage dm; public ProcessDocument(DocumentManage d) { dm = d; } /// <summary> /// 开启线程读取文档中的元素 /// </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)//判断队列中有没有元素 { Console.WriteLine(dm.GetDocument().ToString()); } } } } }
=====================================主程序
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);//开启线程读取文档 for (int i = 0; i < 10000; i++)//向队列中添加10000个元素 { Document doc = new Document(i.ToString(), i.ToString()); dm.AddDocument(doc); Console.WriteLine(doc.ToString() + ".....New"); } Console.ReadKey(); } } }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。