打开APP
未登录
开通VIP,畅享免费电子书等14项超值服
开通VIP
首页
好书
留言交流
下载APP
联系客服
C#中队列集合的用法,配合线程(转)
franklinfj
>《vs》
2013.05.02
关注
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading;
namespace
集合
{
class
队列
{
public
static
void
Main()
{
//队列: 元素以先进先出的方式来处理的集合(FIFO-First In First Out)第一个来,第一个滚
//例:飞机登记排队,靠前的就先上飞要,不过队列有优先级,如商务仓的队与经济仓的队,是两个不同的队,而商务优先
//在.Net技术中, using System.Collections.Generic.Queue<T>是队列的泛型版本实现
// System.Collections.Queue是非泛型的实现,参数是Object
//public class Queue<T> : IEnumerable<T>, ICollection, IEnumerable
//从队列的定义可以看出,它实现了跌代,集合接口 ,它没有实现ICollection<T>这个接口,因为其中定义的Add Remove方法会破坏队列
//队列与列表主要区别就是在于,它没有实现IList接口
//所以,不可以用索引器访问队列,队列只允许添加元素,该元素只能放在队列的最后(Enqueue()),还有就是从头部取元素Dequeue()
//Enqueue从队列的后面插入元素,而 Dequeue在取一个元素的同时,会前取出的元素删除,如再再调用一次,就删除下一个元素
//方法简介 :
// Enqueue() 一端插入元素
// Dequeue() 从头部读取和删除一个元素,如果调用方法时,队列中没有元素,刨出InvalidOperationException异常
// Peek() 在队列头部读取一个元素,但是,不删除它
// Count 返回队列元素个数
// TrimExcess() 重新设置队列容量,Dequeue方法,是可能删除一个对象,但是,不会重设容量,这样会浪费空余空间 ,本方法,从头部清除空余空间
// Contains() 确定某个元素是不是在队列中,如果是,返回true
// CopyTo() 把元素从队列复制到一个已有的数组中
// ToArray() 返回一个包含队列元素的新数组
// 下面我们开始写一个关于队列的例子, 使用一个线程将文档添加到队列中,用另一个线程读取队列,并处理他们
// 存储队列的类型是Document,我们先定义一个文档类,接着定义一个文档处理类DocumentManager,其中包含了添加与读取方法,还有一个属性确定队列中是不是为空
// 然后我们定义一个 ProcessDocuments来处理线程,操作文档
DocumentManager mg =
new
DocumentManager();
ProcessDocuments prcess =
new
ProcessDocuments( mg );
//启动读取线程,不过现在没内容,要等一会加入后,就能读到了
ProcessDocuments.Start(mg);
Document doc =
null
;
for
(
int
i = 0; i < 500; i++)
{
doc =
new
Document(
"Aladdin:"
+ i,
"Hello,Nihao!"
);
mg.AddDocument( doc );
//睡会,让给其他线程玩会
//Thread.Sleep(20);
}
Console.ReadLine();
}
}
// 文档类,描述了文档的标题与内容
class
Document
{
public
string
title;
public
string
content;
public
Document(
string
title,
string
content)
{
this
.title = title;
this
.content = content;
}
}
class
DocumentManager
{
//定义队列集合
private
readonly
Queue<Document> docQueue =
new
Queue<Document>();
//添加文档
public
void
AddDocument(Document doc)
{
lock
(
this
)
{
//从队列一端插入内容
docQueue.Enqueue(doc);
Console.WriteLine(
"成功插入文档:"
+ doc.title);
}
}
//读取文档
public
Document GetDocument()
{
Document doc =
null
;
lock
(
this
)
{
doc = docQueue.Dequeue();
return
doc;
}
}
//只读属性,确定队列中是不是还有元素
public
bool
IsDocumentAvailable
{
get
{
return
docQueue.Count > 0;
}
}
}
// 处理文档
class
ProcessDocuments
{
private
DocumentManager dm;
public
ProcessDocuments( DocumentManager dm )
{
this
.dm = dm;
}
public
static
void
Start(DocumentManager manager)
{
// 参数 : public delegate void ThreadStart();
new
Thread(
new
ProcessDocuments(manager).DoThreadFunc).Start() ;
}
public
void
DoThreadFunc()
{
//无限循环读取,只要队列中有内容,这条线程就读出来
while
(
true
)
{
if
(
this
.dm.IsDocumentAvailable)
{
Document doc =
this
.dm.GetDocument() ;
Console.WriteLine(
"从队列中读到读取并删除文档 标题:{0} 内容: {1}"
, doc.title, doc.content );
}
}
}
}
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报
。
打开APP,阅读全文并永久保存
查看更多类似文章
猜你喜欢
类似文章
【热】
打开小程序,算一算2024你的财运
【C#高级编程(集合2)】 C#高级编程之实现队列读写操作
java并发(十九)阻塞队列
深入理解阻塞队列
批量获取图形视图中心坐标
C++实现两个栈实现一个队列和两个队列实现一个栈
无锁队列的实现
更多类似文章 >>
生活服务
热点新闻
留言交流
回顶部
联系我们
分享
收藏
点击这里,查看已保存的文章
导长图
关注
一键复制
下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!
联系客服
微信登录中...
请勿关闭此页面
先别划走!
送你5元优惠券,购买VIP限时立减!
5
元
优惠券
优惠券还有
10:00
过期
马上使用
×