the wait queue
using System;
using System.Collections.Concurrent;
using System.Threading; namespace Base
{
public class WaitQueue<T> : IDisposable where T : class
{
/// <summary>
/// The deal action.
/// </summary>
public Action<T> DealAction { get; set; } /// <summary>
/// The inner queue.
/// </summary>
private readonly ConcurrentQueue<T> _innerQueue; /// <summary>
/// The deal thread.
/// </summary>
private readonly Thread dealThread; /// <summary>
/// The flag for end thread.
/// </summary>
private bool endThreadFlag = false; /// <summary>
/// The auto reset event.
/// </summary>
private readonly AutoResetEvent autoResetEvent = new AutoResetEvent(true); /// <summary>
/// Initializes a new instance of the WaitQueue`1 class.
/// </summary>
public WaitQueue()
{
this._innerQueue = new ConcurrentQueue<T>();
this.dealThread = new Thread(this.DealQueue);
this.dealThread.Start();
} /// <summary>
/// Disposes current instance, end the deal thread and inner queue.
/// </summary>
public void Dispose()
{
this.endThreadFlag = true;
this._innerQueue.Enqueue(null);
this.autoResetEvent.Set();
this.dealThread.Join();
this.autoResetEvent.Close();
} /// <summary>
/// Save entity to Queue.
/// </summary>
/// <param name="entity">The entity what will be deal.</param>
public void SaveLog(T entity)
{
this._innerQueue.Enqueue(entity);
this.autoResetEvent.Set();
} /// <summary>
/// Out Queue.
/// </summary>
/// <param name="entity">The init entity.</param>
/// <returns>The entity what will be deal.</returns>
private bool Dequeue(out T entity)
{
return this._innerQueue.TryDequeue(out entity);
} /// <summary>
/// Deal entity in Queue.
/// </summary>
private void DealQueue()
{
while (true)
{
T entity;
if (this.Dequeue(out entity))
{
if (this.endThreadFlag && entity == null)
{
return; // Exit the deal thread.
} try
{
if (this.DealAction != null)
{
this.DealAction(entity);
}
}
catch
{
}
}
else
{
this.autoResetEvent.WaitOne();
}
}
}
}
}
the wait queue的更多相关文章
- [数据结构]——链表(list)、队列(queue)和栈(stack)
在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...
- Azure Queue Storage 基本用法 -- Azure Storage 之 Queue
Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob.Queue.File 和 Table. 笔者在<Azure File Storage 基 ...
- C++ std::queue
std::queue template <class T, class Container = deque<T> > class queue; FIFO queue queue ...
- 初识Message Queue之--基础篇
之前我在项目中要用到消息队列相关的技术时,一直让Redis兼职消息队列功能,一个偶然的机会接触到了MSMQ消息队列.秉着技术还是专业的好为原则,对MSMQ进行了学习,以下是我个人的学习笔记. 一.什么 ...
- 搭建高可用的rabbitmq集群 + Mirror Queue + 使用C#驱动连接
我们知道rabbitmq是一个专业的MQ产品,而且它也是一个严格遵守AMQP协议的玩意,但是要想骚,一定需要拿出高可用的东西出来,这不本篇就跟大家说 一下cluster的概念,rabbitmq是erl ...
- PriorityQueue和Queue的一种变体的实现
队列和优先队列是我们十分熟悉的数据结构.提供了所谓的“先进先出”功能,优先队列则按照某种规则“先进先出”.但是他们都没有提供:“固定大小的队列”和“固定大小的优先队列”的功能. 比如我们要实现:记录按 ...
- C#基础---Queue(队列)的应用
Queue队列,特性先进先出. 在一些项目中我们会遇到对一些数据的Check,如果数据不符合条件将会把不通过的信息返回到界面.但是对于有的数据可能会Check很多条件,如果一个数据一旦很多条件不 ...
- [LeetCode] Queue Reconstruction by Height 根据高度重建队列
Suppose you have a random list of people standing in a queue. Each person is described by a pair of ...
- [LeetCode] Implement Queue using Stacks 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- 源码之Queue
看源码可以把python看得更透,更懂,想必也是开发人员的必经之路. 现在有个任务,写个线程池.使用Queue就能写一个最简单的,下面就来学学Queue源码. 源码之Queue: class Queu ...
随机推荐
- Python 利用循环画散点图
import pandas as pd data = pd.read_csv('D:/suning/iris.csv') data = data.iloc[:,1:] ###2维散点图 import ...
- php二维数组的某一字段 做分组统计
$country=array_column($order,'country');$countryGP=array_count_values($country);对二维数组的某一字段 做分组统计
- Vue.js 的几点总结Watchers/router key/render
Vue.js 的几点总结,下面就是实战案例,一起来看一下. 第一招:化繁为简的Watchers 场景还原: 1 2 3 4 5 6 7 8 created(){ this.fetchPostLis ...
- fseek函数
函数名:fseek函数 头文件:#include<stdio.h> 功能:把与fp有关的文件位置指针放到一个指定位置. 格式: int fseek(FILE *stream, long ...
- 【bzoj3813】: 奇数国 数论-线段树-欧拉函数
[bzoj3813]: 奇数国 题意:给定一个序列,每个元素可以分解为最小的60个素数的形式.(x=p1^k1*p2^k2*......p60^k60)(p1=2,p2=3,…,p60=281) 支持 ...
- 【转】windows server 2012 R2搭建IIS服务器
源地址:http://blog.csdn.net/microsoft_wu/article/details/46521017
- Python列表删除的三种方法
1.使用del语句删除元素 >>> i1 = ["a",'b','c','d'] >>> del i1[0] >>> prin ...
- vue脚手架的搭建
另一博客地址:https://segmentfault.com/a/1190000016451376 一.基础的知识 1.html 2.js 3.css二.搭建项目过程--windows系统1.nod ...
- SP7258 SUBLEX - Lexicographical Substring Search
\(\color{#0066ff}{ 题目描述 }\) 给定一个字符串,求排名第k小的串 \(\color{#0066ff}{输入格式}\) 第一行给定主串(len<=90000) 第二行给定询 ...
- Qt 学习之路 2(11):布局管理器
Home / Qt 学习之路 2 / Qt 学习之路 2(11):布局管理器 Qt 学习之路 2(11):布局管理器 豆子 2012年9月4日 Qt 学习之路 2 70条评论 所谓 GUI 界 ...