黄聪

论SEO对人类的重要性,请看我的博客:hcsem.com

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
DocumentManager dm
= new DocumentManager();
ProcessDocument.Start(dm);

for (int i = 0; i < 1000; i++)
{
Document doc
= new Document("Doc " + i, "Content");
dm.AddDocument(doc);
Console.WriteLine(
"Added document Doc "+ i);
Thread.Sleep(
new Random().Next(20));
}
}
}


public class DocumentManager
{
private readonly Queue<Document> documentQueue = new Queue<Document>();

public void AddDocument(Document doc)
{
lock (this)
{
documentQueue.Enqueue(doc);
}
}

public Document GetDocument()
{
Document doc
= null;
lock (this)
{
doc
= documentQueue.Dequeue();
}
return doc;
}

public bool IsDocumentAvailable
{
get {
return documentQueue.Count > 0;
}
}
}

public class ProcessDocument
{
private DocumentManager documentManager;

protected ProcessDocument(DocumentManager dm)
{
this.documentManager = dm;
}

public static void Start(DocumentManager dm)
{
new Thread(new ProcessDocument(dm).Run).Start();
}

protected void Run()
{
while (true)
{
if (documentManager.IsDocumentAvailable)
{
Document doc
= documentManager.GetDocument();
Console.WriteLine(
"Processing document {0}",doc.Title);
}
Thread.Sleep(
new Random().Next(20));
}
}
}
}

posted on 2010-05-13 14:30  黄聪  阅读(364)  评论(0编辑  收藏  举报