=====================================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();
}
}
}
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
網(wǎng)站名稱:Queue<T>隊列-創(chuàng)新互聯(lián)
網(wǎng)站路徑:http://chinadenli.net/article44/ediee.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、網(wǎng)站營銷、網(wǎng)站制作、定制網(wǎng)站、App開發(fā)、域名注冊
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)