C#集合之隊(duì)列的用法
隊(duì)列是其元素按照先進(jìn)先出(FIFO)的方式來(lái)處理的集合。
隊(duì)列使用System.Collections.Generic名稱空間中的泛型類Queue<T>實(shí)現(xiàn)。在內(nèi)部,Queue<T>類使用T類型的數(shù)組,這類似List<T>(http://www.dbjr.com.cn/article/244084.htm)類型。隊(duì)列實(shí)現(xiàn)ICollection和IEnumerable<T>接口,但沒有實(shí)現(xiàn)ICollection<T>接口,所以ICollection<T>接口定義的Add()合Remove()方法不能用于隊(duì)列。
Enqueue()方法在隊(duì)列的一端添加元素,Dequeue()方法在隊(duì)列的另一端讀取和刪除元素。再次調(diào)用Dequeue(),會(huì)刪除隊(duì)列的下一個(gè)元素:

Queue<T>類的方法和屬性:

在創(chuàng)建隊(duì)列時(shí),可以使用與List<T>類似的構(gòu)造函數(shù),也可以使用構(gòu)造函數(shù)指定容量。
非泛型Queue類的默認(rèn)構(gòu)造函數(shù)不同,它會(huì)創(chuàng)建一個(gè)包含32項(xiàng)的空數(shù)組
下面用一個(gè)例子演示隊(duì)列,使用一個(gè)線程將文檔添加到隊(duì)列中,用另一個(gè)線程從隊(duì)列中讀取文檔,并處理:
//存儲(chǔ)在隊(duì)列中的元素是Document類型
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類是Queue<Document>外面的一層。用來(lái)如何將文檔添加到隊(duì)列和從隊(duì)列中獲取文檔
public class DocumentManager
{
private readonly Queue<Document> documentQueue = new Queue<Document>();
//因?yàn)槎鄠€(gè)線程訪問(wèn)DocumentManager類,所以用lock語(yǔ)句鎖定對(duì)隊(duì)列的訪問(wèn)
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;
}
public bool IsDocumentAvailable
{
get
{
lock (this)
{
return documentQueue.Count > 0;
}
}
}
}
//使用ProcessDocuments類在一個(gè)單獨(dú)的任務(wù)中讀取和刪除隊(duì)列中的文檔。
public class ProcessDocuments
{
//能從外部訪問(wèn)的唯一方法是Start()方法
//在Start()中,實(shí)例化一個(gè)新任務(wù)。創(chuàng)建一個(gè)ProcessDocuments對(duì)象,調(diào)用ProcessDocuments的Run()方法
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;
//定義一個(gè)無(wú)限循環(huán),使用DocumentManager類的IsDocumentAvailable屬性確定隊(duì)列中是否還有文檔。
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));
}
}
}客戶端代碼
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();
}到此這篇關(guān)于C#集合之隊(duì)列的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)文件壓縮與解壓的方法示例【ZIP格式】
這篇文章主要介紹了C#實(shí)現(xiàn)文件壓縮與解壓的方法,結(jié)合具體實(shí)例形式分析了C#針對(duì)文件進(jìn)行zip格式壓縮與解壓縮的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
C#實(shí)現(xiàn)軟件開機(jī)自啟動(dòng)的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)軟件開機(jī)自啟動(dòng),且不需要管理員權(quán)限,文中的示例代碼講解詳細(xì),需要的小伙伴可以參考一下2023-07-07
C# 數(shù)組刪除元素的實(shí)現(xiàn)示例
本文主要介紹了C# 數(shù)組刪除元素的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
C#實(shí)現(xiàn)查殺本地與遠(yuǎn)程進(jìn)程的方法
這篇文章主要介紹了C#實(shí)現(xiàn)查殺本地與遠(yuǎn)程進(jìn)程的方法,可實(shí)現(xiàn)針對(duì)特定進(jìn)程的關(guān)閉操作,是C#進(jìn)程操作的一個(gè)典型應(yīng)用,需要的朋友可以參考下2014-12-12
C#實(shí)現(xiàn)關(guān)閉子窗口而不釋放子窗口對(duì)象的方法
下面小編就為大家?guī)?lái)一篇C#實(shí)現(xiàn)關(guān)閉子窗口而不釋放子窗口對(duì)象的方法 。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01
C# lambda表達(dá)式應(yīng)用如何找出元素在list中的索引
這篇文章主要介紹了C# lambda表達(dá)式應(yīng)用如何找出元素在list中的索引的相關(guān)資料,需要的朋友可以參考下2018-01-01

