the wait queue

来源:http://www.cnblogs.com/12taotie21/archive/2016/01/16/5135159.html
-Advertisement-
Play Games

public class WaitQueue : IDisposable where T : class { /// /// The deal action. /// public Action DealActio...


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

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 話說蘋果在iOS7.0之後,很多系統界面都使用了毛玻璃效果,增加了界面的美觀性,比如下圖的通知中心界面;但是其iOS7.0的SDK並沒有提供給開發者實現毛玻璃效果的API,所以很多人都是通過一些別人封裝的框架來實現,後面我也會講到一個;其實在iOS7.0(包括)之前還是有系統的類可以實現毛玻璃效果的...
  • 1、判斷是否為快速點擊 /** 判斷是否是快速點擊 */ private static long lastClickTime; public static boolean isFastDoubleClick() { long time = System.current...
  • 原文鏈接地址:http://www.cnblogs.com/lyhabc/p/3888702.html 最近把大學時候的ORACLE教程書本翻出來看,真的是感觸良多 以前在學校的時候,每次ORACLE測驗和考試都是不合格的,期末的時候靠補考才勉強過關 大家看到下圖的封面應該知道大學教我們ORACLE...
  • 問題描述: 工作中使用kettle將原始庫中的數據抽取到標準庫中,在抽取過程中報錯:【ORA-14400: 插入的分區關鍵字未映射到任何分區】解決過程: 經過百度,發現出現ORA-14400是表分區出現問題。 1.確定該表是否已經添加了表分區。 select partition_na...
  • 本文主要含有一些AlwaysOn 配置方法及連接。本想展開詳細寫一下 無奈隔壁在年會排練節目,那歌唱得我只想趕緊回家!!!!!!!!!!!!!!!http://www.cnblogs.com/double-K/------------------------------只讀配置-----------...
  • 背景《SQL Server 2012實施與管理實戰指南》中指AlwaysON同步過程如下:任何一個SQL Server里都有個叫Log Writer的線程,當任何一個SQL用戶提交一個數據修改事務時,它會負責把記錄本次修改的日誌信息先記入一段記憶體中的日誌緩衝區,然後再寫入物理日誌文件(日誌固化)。所...
  • 今天在維護oracle資料庫時,查看監聽的狀態,執行#lsnrctl status報錯: -bash:lsnrctl:command not found。以前並不會這樣,仔細想了一下,問題找到了,我是以root遠程登錄到伺服器上,維護oracle時,直接 su oracle,這樣操作雖然用戶切換為o...
  • 相信很多博友在開發初次接觸學習C# winForm時,當窗體大小變化時,窗體內的控制項並沒有隨著窗體的變化而變化,最近因為一個項目工程的原因,也需要解決這個問題。通過查閱和學習,這個問題得到瞭解決,或許不是很好的處理方式,但是也值得借鑒。。。 下麵我將建立一個Demo來大概解釋下這個方法的實現:(註:...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...