转载至 https://www.codeproject.com/Articles/28785/Thread-synchronization-Wait-and-Pulse-demystified

/*
* 这是一个完整源代码示例,演示了此模式的多功能性。它实现了一个可以停止的阻塞队列。
* 阻塞队列是固定大小的队列。如果队列已满,则尝试添加项目将被阻止。
* 如果队列为空,则尝试删除项目将阻止。
* 当Quit()被调用时,队列停止。这意味着您无法再添加任何项目,但可以删除现有项目,直到队列为空。此时,队列已完成。
* 这是一组非常复杂的条件。您可以使用更高级别的结构组合来实现它,但它会更难。该模式使得该实现相对简单。
*
*/
using System;
using System.Threading;
using System.Collections.Generic; namespace TestBlockQueue
{
public class BlockingQueue<T>
{
readonly int _Size = ;
readonly Queue<T> _Queue = new Queue<T>();
readonly object _Key = new object();
bool _Quit = false; public BlockingQueue(int size)
{
_Size = size;
} public void Quit()
{
lock (_Key)
{
_Quit = true; Monitor.PulseAll(_Key);
}
} public bool Enqueue(T t)
{
lock (_Key)
{
while (!_Quit && _Queue.Count >= _Size) Monitor.Wait(_Key); if (_Quit) return false; _Queue.Enqueue(t); Monitor.PulseAll(_Key);
} return true;
} public bool Dequeue(out T t)
{
t = default(T); lock (_Key)
{
while (!_Quit && _Queue.Count == ) Monitor.Wait(_Key); if (_Queue.Count == ) return false; t = _Queue.Dequeue(); Monitor.PulseAll(_Key);
} return true;
}
} /// <summary>
/// 对于任意数量的生产者和消费者的并发访问,此实现是安全的。以下是一个快速生产者和两个慢速消费者的示例
/// </summary>
public class Program
{
public static void Main(string[] args)
{
var q = new BlockingQueue<int>(); // Producer
new Thread(() =>
{
for (int x = ; ; x++)
{
if (!q.Enqueue(x)) break;
Console.WriteLine(x.ToString("") + " >");
}
Console.WriteLine("Producer quitting");
}).Start(); // Consumers
for (int i = ; i < ; i++)
{
new Thread(() =>
{
for (; ; )
{
Thread.Sleep();
int x = ;
if (!q.Dequeue(out x)) break;
Console.WriteLine(" < " + x.ToString(""));
}
Console.WriteLine("Consumer quitting");
}).Start();
} Thread.Sleep(); Console.WriteLine("Quitting"); q.Quit();
}
}
}

最新文章

  1. Red Hat5下源码安装mysql5.6过程记录
  2. OpenStack 云计算基础知识
  3. UIGestureRecognizer ios手势识别温习
  4. I.MX6 ov5640 camera
  5. 【Knockout】五、创建自定义绑定
  6. [转]oracle的ANYDATA数据类型
  7. Linux下find与grep指令的相关用法
  8. c#设计模式-单例模式(面试题)
  9. 前段篇:HTML
  10. JavaScript原生实现观察者模式
  11. intoj
  12. Important persons in deep learning
  13. 1823: [JSOI2010]满汉全席 2-sat
  14. i++和++i的笔试题
  15. python设计模式-单例模式
  16. ContentType和@ResponseBody
  17. Redis事务介绍
  18. CSS之display样式
  19. Could not parse multipart servlet request; nested exception is java.io.IOException: The temporary upload location
  20. MyEclipse 对项目进行build path无效

热门文章

  1. Apache Kafka工作流程| Kafka Pub-Sub Messaging
  2. Tomcat是一个Servlet容器?
  3. 【C++札记】标准输入与输出
  4. bzoj 4500 矩阵 题解
  5. MySQL数据库-表操作-SQL语句(二)
  6. H5新特性 本地存储---cookie localStorage sessionStorage
  7. if的条件表达式
  8. 关于fastjson与jackson在反序列化bool型时的区别
  9. 同一个Tomcat部署多个springboot项目问题
  10. 监听EF执行的sql语句及状态