当前位置:   article > 正文

C#中线程安全的单例模式_c#线程安全的单例

c#线程安全的单例

一、单例模式

①单例模式的定义:是用来保证这个类在运行期间只会被创建一个实例,并提供一个访问它的全局访问点。

②单例模式的作用:保证一个类只有一个访问实例,节省系统资源。

③单例模式的特点:私有构造方法;私有成员变量;公有获取实例方法。

二、单例模式实现

①懒汉式单例

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace SingleModel
  5. {
  6. //懒汉式单例模式
  7. public class Single
  8. {
  9. //私有构造
  10. private Single() { }
  11. //单例
  12. public static Single GetInstance()
  13. {
  14. return SingleHolder._Instance;
  15. }
  16. //内部类
  17. private static class SingleHolder
  18. {
  19. internal static readonly Single _Instance = new Single();
  20. }
  21. }//Class_end
  22. }

②饿汉式单例

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace SingleModel
  5. {
  6. //饿汉式单例模式
  7. public class Single2
  8. {
  9. private static readonly Single2 _Instance = new Single2();
  10. private Single2() { }
  11. public static Single2 GeInstance()
  12. {
  13. return _Instance;
  14. }
  15. }//Class_end
  16. }

③懒汉式单例

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace SingleModel
  5. {
  6. //懒汉式单例
  7. public class Single3
  8. {
  9. //定义一个保存实例的静态变量
  10. private static Single3 _Instance;
  11. //定义一个保证线程同步标识
  12. private static readonly object _Synchronized = new object();
  13. private Single3() { }
  14. public static Single3 GeInstance()
  15. {
  16. if (_Instance==null)
  17. {
  18. lock (_Synchronized)
  19. {
  20. //双重锁定
  21. if (_Instance==null)
  22. {
  23. _Instance = new Single3();
  24. }
  25. }
  26. }
  27. return _Instance;
  28. }
  29. }//Class_end
  30. }

 ④实现控制实例数目单例

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace SingleModel
  5. {
  6. //实现控制实例数目
  7. public class SingleExtand
  8. {
  9. //私有构造
  10. private SingleExtand() { }
  11. //定义一个缺省的key值前缀
  12. private static string _DefaultPreKey = "Cache";
  13. //记录当前实例
  14. public static int _SingleNum = 1;
  15. //定义实例的最大数目
  16. private static int _MaxSingleNum = 3;
  17. //定义一个保证线程同步的标识
  18. private static readonly object _Synchronized = new object();
  19. //缓存实例的容器
  20. private static Dictionary<String, SingleExtand> _SinleDic = new Dictionary<string, SingleExtand>();
  21. //本类实例
  22. public static SingleExtand GetInstance()
  23. {
  24. string key = _DefaultPreKey + _SingleNum;
  25. SingleExtand sinleExtand = null;
  26. if (_SinleDic.ContainsKey(key))
  27. {
  28. sinleExtand = _SinleDic[key];
  29. }
  30. if (sinleExtand==null)
  31. {
  32. lock (_Synchronized)
  33. {
  34. if (sinleExtand == null)
  35. {
  36. sinleExtand = new SingleExtand();
  37. _SinleDic.Add(key, sinleExtand);
  38. }
  39. }
  40. }
  41. _SingleNum++;
  42. if (_SingleNum>_MaxSingleNum)
  43. {
  44. _SingleNum = 1;
  45. }
  46. return sinleExtand;
  47. }
  48. }//Class_end
  49. }

三、单例测试

  1. using System;
  2. namespace SingleModel
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. Console.WriteLine("(线程安全)单例模式");
  9. Console.WriteLine("饿汉式单例1");
  10. Single single1 = Single.GetInstance();
  11. Single single2 = Single.GetInstance();
  12. Single single3 = Single.GetInstance();
  13. Console.WriteLine("single1:" + single1.GetHashCode());
  14. Console.WriteLine("single2:" + single2.GetHashCode());
  15. Console.WriteLine("single3:" + single3.GetHashCode());
  16. Console.WriteLine("\n");
  17. Console.WriteLine("懒汉式单例2");
  18. Single2 single21 = Single2.GeInstance();
  19. Single2 single22 = Single2.GeInstance();
  20. Single2 single23 = Single2.GeInstance();
  21. Console.WriteLine("single21:" + single21.GetHashCode());
  22. Console.WriteLine("single22:" + single22.GetHashCode());
  23. Console.WriteLine("single23:" + single23.GetHashCode());
  24. Console.WriteLine("\n");
  25. Console.WriteLine("懒汉式单例3");
  26. Single3 single31 = Single3.GeInstance();
  27. Single3 single32 = Single3.GeInstance();
  28. Single3 single33 = Single3.GeInstance();
  29. Console.WriteLine("single31:" + single31.GetHashCode());
  30. Console.WriteLine("single32:" + single32.GetHashCode());
  31. Console.WriteLine("single33:" + single33.GetHashCode());
  32. Console.WriteLine("\n");
  33. Console.WriteLine("控制实例数目的单例");
  34. SingleExtand singleExtand1 = SingleExtand.GetInstance();
  35. SingleExtand singleExtand2 = SingleExtand.GetInstance();
  36. SingleExtand singleExtand3 = SingleExtand.GetInstance();
  37. SingleExtand singleExtand4 = SingleExtand.GetInstance();
  38. SingleExtand singleExtand5 = SingleExtand.GetInstance();
  39. SingleExtand singleExtand6 = SingleExtand.GetInstance();
  40. Console.WriteLine("singleExtand1:" + singleExtand1.GetHashCode());
  41. Console.WriteLine("singleExtand2:" + singleExtand2.GetHashCode());
  42. Console.WriteLine("singleExtand3:" + singleExtand3.GetHashCode());
  43. Console.WriteLine("singleExtand4:" + singleExtand4.GetHashCode());
  44. Console.WriteLine("singleExtand5:" + singleExtand5.GetHashCode());
  45. Console.WriteLine("singleExtand6:" + singleExtand6.GetHashCode());
  46. Console.WriteLine("\n");
  47. Console.ReadLine();
  48. }
  49. }
  50. }

运行结果如下:

 

 

 

 

 

 

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号