网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Queue<T>队列

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

创新互联主营宣恩网站建设的网络公司,主营网站建设方案,重庆App定制开发,宣恩h5小程序开发搭建,宣恩网站营销推广欢迎宣恩等地区企业咨询

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 doc = new Queue();
        //向队列中添加元素
        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;
        }
        /// 
        /// 开启线程读取文档中的元素
        /// 
        /// 
        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>队列


文章题目:Queue<T>队列
文章网址:http://bjjierui.cn/article/jghpci.html

其他资讯