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 ...
随机推荐
- U-Net: Convolutional Networks for Biomedical Image Segmentation(理解+github代码)
github代码:https://github.com/Chet1996/pytorch-UNet 0 - Abstract 这篇文章是生物学会议ICMICCAI2015的文章,主要针对的是生物影像进 ...
- js对象的深浅拷贝
JS数据类型可以分为(ES5,暂时不考虑ES6): 简单数据类型:Number.String.undefined.boolean 复杂数据类型:Object.Array 简单的数据类型,往往是赋值操作 ...
- Leveling Ground(数论,三分法,堆)
Leveling Ground(数论,三分法,堆) 给定n个数和a,b每次可以选择一段区间+a,-a,+b或-b,问最少操作几次能把他们都变成0.n<=1e5. 首先差分一下序列,问题就会变成了 ...
- mybatis 日期查询datetime
<select id="getHistoryDataByDate" parameterType="java.util.HashMap" resultTyp ...
- 二分+最小生成树【bzoj2654】: tree
2654: tree 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有need条白色边的生成树. 题目保证有解. 二分答案,然后跑最小生成树判断. 注意优先跑白色边. code: ...
- 关于在多个UItextield切换焦点
本人对于应用的完美用户体验是这样认为:当一个应用是迎合用户习惯 ,并且在人机交互之中降低用户的学习成本 ,由于应用和人的思维方向一致时,就会有共鸣,这对于程序设计是有益的,因为只要愿意去改变总有优雅的 ...
- jvm与tomcat启动优化配置
JVM 优化 Java 的内存模型分为: Young,年轻代(易被 GC).Young 区被划分为三部分,Eden 区和两个大小严格相同的 Survivor 区,其中 Survivor 区间中,某一时 ...
- Vue-Router路由Vue-CLI脚手架和模块化开发 之 路由的动态跳转
在上一篇的博文中,实现的跳转是在页面中进行实现的 利用vue-router实例方法,使用js对路由进行动态跳转: 1.router.push:参数为路由对象,跳转到指定路由,跳转后会产生历史记录: & ...
- linux 环境下tomcat中部署jfinal项目
tomcat中部署jfinal项目 问题现象如下图 问题描述: 我在自己的windows7系统上tomcat下面跑这个项目没有任何问题吗,但是当我把项目上传到linux服务器上的tomcatwebap ...
- 利用JPanel和JLabel设置背景图片
//创建面板1,放置背景图片1 JPanel jPanelTop=new JPanel(); jPanelTop.setBounds(,-,,); //x=0,y=-5用来设置面板距离窗体左上角的距离 ...