zoukankan      html  css  js  c++  java
  • 黄聪:队列Queue笔记

    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));
    }
    }
    }
    }

  • 相关阅读:
    hdu 1176 免费馅饼
    算法竞赛入门经典训练指南-做题详细记录(更新中)
    poj-3250 Bad Hair Day
    poj-2823 Sliding Window
    2019.7.15-7.20暑假集训总结
    hdu 1754 I hate it
    poj 1611 The Suspects
    hdu 3038 How Many Answers Are Wrong
    hdu 1213 How Many Tables
    POJ 1182 食物链
  • 原文地址:https://www.cnblogs.com/huangcong/p/1734500.html
Copyright © 2011-2022 走看看