⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 producerconsumerqueue.cs

📁 HeyCacher 高性能缓存方案(带源码) 1. 文件的所有权益归上传用户所有 2. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途 3. CSDN下载频道仅提供交流平台
💻 CS
字号:
//===============================================================================
// CSDN HeyCache 
//===============================================================================
// 修改记录:[按最后修改时间倒排序]
// 2007.06.11 by tangwei
//
// 代码来源:参考于dotnet企业库3.0版
//===============================================================================
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Threading;

namespace HeyCacher.Components.Scavenger
{
    /// <summary>
    /// 回收队列
    /// </summary>	
    public class ProducerConsumerQueue
    {
        private object lockObject = new Object();
        private Queue queue = new Queue();

        /// <summary>
        /// Gets the number of items in the queue.
        /// </summary>
        /// <value>
        /// The number of items in the queue.
        /// </value>
        public int Count
        {
            get { return queue.Count; }
        }

        /// <summary>
        /// Removes and returns the object at the beginning of the queue. 
        /// </summary>
        /// <returns>
        /// The object at the beginning of the queue.
        /// </returns>
        public object Dequeue()
        {
            lock (lockObject)
            {
                while (queue.Count == 0)
                {
                    if (WaitUntilInterrupted())
                    {
                        return null;
                    }
                }

                return queue.Dequeue();
            }
        }

        /// <summary>
        /// Adds the object at the end of the queue. 
        /// </summary>		
        public void Enqueue(object queueItem)
        {
            lock (lockObject)
            {
                queue.Enqueue(queueItem);
                Monitor.Pulse(lockObject);
            }
        }

        private bool WaitUntilInterrupted()
        {
            try
            {
                Monitor.Wait(lockObject);
            }
            catch (ThreadInterruptedException)
            {
                return true;
            }

            return false;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -