单例模式的几种写法

单例模式的核心思想就是保证内存中存在一个实例

适用场景:多次及重复的调用类的实例,做重复的内容;全局管理及调用;提高效率的时候;


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

namespace ConsoleApplication5
{
    /// <summary>
    /// 饿汉模式,类装载中完成实例化,
    /// 1.避免出现多线程多次构造
    /// 2.不调用会造成资源浪费
    /// 
    /// </summary>
    class Singleton
    {
        public long Result { get; private set; }
        public Singleton()
        {
            for(int i = 0; i < 10000000; i++)
            {
                Result += i;
            }
            Console.WriteLine("构造函数 执行");
           
        }
        ~Singleton() {
            Console.WriteLine("析构函数");
        }

        private static Singleton _Instance = new Singleton();

        public static Singleton Instance
        {
            get
            {
                return _Instance;
            }
        }

        public void Dispose()
        {
            if (_Instance != null)
            {
                _Instance = null;
                Console.WriteLine("销毁");
            }
        }

    }
    /// <summary>
    /// 饿汉模式,静态构造里完成实例化
    /// 1.避免出现多线程多次构造
    /// 2.不调用会造成资源浪费
    ///
    /// </summary>
    class Singleton2
    {
        public long Result { get; private set; }
        public Singleton2()
        {
            for (int i = 0; i < 10000000; i++)
            {
                Result += i;
            }
            Console.WriteLine("构造函数 执行");

        }
        ~Singleton2()
        {
            Console.WriteLine("析构函数");
        }

        private static Singleton2 _Instance = null;

        static Singleton2()
        {
            _Instance = new Singleton2();
        }

        public static Singleton2 Instance
        {
            get
            {
                return _Instance;
            }
        }

        public void Dispose()
        {
            if (_Instance != null)
            {
                _Instance = null;
                Console.WriteLine("销毁");
            }
        }
    }
    /// <summary>
    /// 懒汉模式,调用的时候实例化
    /// 1.适用与单线程,不适合多线程,出现多次构造实力
    /// 180,254
    /// </summary>
    class Singleton3
    {
        public long Result { get; private set; }
        public Singleton3()
        {
            for (int i = 0; i < 10000000; i++)
            {
                Result += i;
            }
            Console.WriteLine("构造函数 执行");

        }

        ~Singleton3()
        {
            Console.WriteLine("析构函数");
        }

        private static Singleton3 _Instance = null;

        public static Singleton3 Instance
        {
            get
            {
                if(_Instance == null)
                {
                    _Instance = new Singleton3();
                }
                return _Instance;
            }
        }

        public void Dispose()
        {
            if(_Instance != null)
            {
                _Instance = null;
                Console.WriteLine("销毁");
            }
        }
    }

    /// <summary>
    /// 懒汉模式,双重检测,经典方法
    /// </summary>
    class Singleton4
    {
        public long Result { get; private set; }
        public Singleton4()
        {
            for (int i = 0; i < 10000000; i++)
            {
                Result += i;
            }
            Console.WriteLine("构造函数 执行");

        }

        ~Singleton4()
        {
            Console.WriteLine("析构函数");
        }

        private static Singleton4 _Instance = null;

        private static object _testLock = new object();
        public static Singleton4 Instance
        {
            get
            {
                if (_Instance == null)//所有的并发线程进入这里检测
                {
                    lock (_testLock)//锁住一个线程,其他线程等待
                    {
                        if (_Instance == null)//第一个进入的线程开始创建实例,其他线程进入后需要检测是否已经创建过
                        {
                            _Instance = new Singleton4();
                        }
                    }
                }
                return _Instance;
            }
        }

        public void Dispose()
        {
            if (_Instance != null)
            {
                _Instance = null;
                Console.WriteLine("销毁");
            }
        }
    }

}

客户端测试

  Console.WriteLine("-------------------------------------------------单线程分割线------------------------------------------------------");
            Stopwatch watch = new Stopwatch();
            watch.Start();

            for (int i = 0; i < 100; i++)
            {

                Console.WriteLine("ID{0} Result {1},Thread {2}", i, Singleton4.Instance.Result, Thread.CurrentThread.ManagedThreadId);
            }

            watch.Stop();
            Console.WriteLine("单线程中的单例耗时:{0}",watch.ElapsedMilliseconds);

            Singleton3.Instance.Dispose();
            GC.Collect();

            Console.WriteLine("-------------------------------------------------多线程分割线------------------------------------------------------");
            Stopwatch watch2 = new Stopwatch();
            watch2.Start();

            List<Task> taskList = new List<Task>();
            TaskFactory taskFactory = new TaskFactory();

            for (int i = 0; i < 100; i++)
            {
                taskList.Add(taskFactory.StartNew(() =>
                {
                    Console.WriteLine("ID{0} Result {1},Thread {2}", i, Singleton4.Instance.Result, Thread.CurrentThread.ManagedThreadId);
                }));



            }
            Task.WaitAll(taskList.ToArray());
            watch2.Stop();
            Console.WriteLine("多线程中的单例耗时:{0}", watch2.ElapsedMilliseconds);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值