溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Queue<T>隊列

發(fā)布時間:2020-07-31 12:58:56 來源:網(wǎng)絡(luò) 閱讀:530 作者:1473348968 欄目:編程語言

=====================================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; }
        //內(nèi)容
        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};內(nèi)容:{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();
        }
    }
}

 

Queue<T>隊列

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI