C#單例測試(懶漢式雙鎖保證線程安全)


單例模式的概念

單例模式的意思就是只有一個實例。單例模式確保某一個類只有一個實例,而且自行實例化並向整個系統提供這個實例。這個類稱為單例類。

關鍵點

  • 這個類只有一個實例,這是最基本的
  • 它必須自行創建這個實例,外部不能實例化
  • 進程內唯一

代碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WCF_Host_Service
{
    /// <summary>
    /// 服務程序唯一上下文對象(單例)
    /// </summary>
    public sealed  class ServiceContext
    {
        private static ServiceContext _ServiceContext = null;
        private readonly static object lockObj = new object();
        /// <summary>
        /// 禁止外部進行實例化
        /// </summary>
        private ServiceContext()
        {

        }
        /// <summary>
        /// 獲取唯一實例,雙鎖定防止多線程並發時重復創建實例
        /// </summary>
        /// <returns></returns>
        public static ServiceContext GetInstance()
        {
            if (_ServiceContext == null)
            {
                lock (lockObj)
                {
                    if (_ServiceContext == null)
                    {
                        _ServiceContext = new ServiceContext();
                    }
                }
            }
            return _ServiceContext;
        }
    }
}

關鍵點: 1)私有的構造函數 2)兩次進行唯一實例的內部成員變量是否為空的判斷。第二次判斷時是在lock的前提下進行的。所以是唯一的,這次判斷保證了是否為空的結論是線程安全的。

測試過程

ConcurrentDictionary<int, ServiceContext> dict = new ConcurrentDictionary<int, ServiceContext>();
                Action testTask = () =>
                {
                    ServiceContext sc = ServiceContext.GetInstance();
                    if (sc != null && !dict.ContainsKey(sc.GetHashCode()))
                    {
                        dict.TryAdd(sc.GetHashCode(), sc);
                    }
                    Thread.Sleep(1);
                }; 
                int index = 0;
                while (index < 1024)
                {                    
                    Parallel.Invoke(testTask,testTask);
                    index++;
                }
                Debugger.Log(0,"",string.Format("測試共生出{0}個實例。",dict.Count));

利用反射外部強制實例化測試:

var type = this.GetType().Assembly.GetType(typeof(ServiceContext).FullName);
                    var sc = Activator.CreateInstance(type);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM