Queue<T>队列-创新互联

=====================================Document.cs

10年积累的成都网站设计、网站建设、外贸网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有阳朔免费网站建设让你可以放心的选择与我们合作。
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();
        }
    }
}

Queue<T>队列

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

网站名称:Queue&lt;T&gt;队列-创新互联
网站路径:https://www.cdcxhl.com/article44/ediee.html

成都网站建设公司_创新互联,为您提供网站策划网站营销网站制作定制网站App开发域名注册

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

小程序开发