zoukankan      html  css  js  c++  java
  • 单例模式

    ============1单例模式==============

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading;

    namespace ConsoleApplication13

    {

        class Program

        {

            static void Main(string[] args)

            {

                for (int i = 0; i < 1000; i++)

                {

                    Thread t = new Thread(new ThreadStart(() =>

                    {

                        Singleton s = Singleton.GetInstance();

                    }));

                    t.Start();

                }

                Console.WriteLine("ok");

                Console.ReadKey();

            }

        }

        public class Singleton

        {

            private Singleton()

            {

                //只要输出一次.就证明创建了一个对象。

                Console.WriteLine(" . ");

            }

            private static Singleton _instance;

            private static readonly object _syn = new object();

            public static Singleton GetInstance()

            {

                //每次都锁定降低性能,所以只有为空值时才锁定。

                if (_instance == null)

                {

                    lock (_syn)

                    {

                        //为了防止在第一次判断_instance==null后,在锁定之前_instance又被赋值,所以锁定之后一定要再判断一次是否已经创建了对象。

                        if (_instance == null)

                        {

                            _instance = new Singleton();

                        }

                    }

                }

                return _instance;

            }

        }

    }

    =============================2单例模式(另一种写法)==========================

     public sealed class Singleton2

        {

            private Singleton2()

            {

                Console.WriteLine(".");

            }

            //静态成员初始化,只在第一次使用的时候初始化一次。

            private static readonly Singleton2 _instance = new Singleton2();

            public static Singleton2 GetInstance()

            {

                return _instance;

            }

        }

    =================测试====================

      for (int i = 0; i < 1000; i++)

                {

                    Thread t = new Thread(new ThreadStart(() =>

                    {

                        Singleton2 s = Singleton2.GetInstance();

                    }));

                    t.Start();

                }

                Console.WriteLine("ok");

                Console.ReadKey();

    //======================实现窗口类(Form)的单例模式===============

    public partial class Form2 : Form

        {

            private Form2()

            {

                InitializeComponent();

            }

            private static Form2 f2;

            private static readonly object syn = new object();

            public static Form2 GetForm2()

            {

                if (f2 == null || f2.IsDisposed)

                {

                    lock (syn)

                    {

                        if (f2 == null || f2.IsDisposed)

                        {

                            f2 = new Form2();

                        }

                    }

                   

                }

                return f2;

            }

        }

  • 相关阅读:
    Freemarker进行非空处理
    Freemarker导出带多个不重复图片的word
    Freemarker导出带图片的word
    怎样在Android本地视频播放器开发
    详细解说九宫图比较常用的多控件布局
    POJ 3189 Steady Cow Assignment【网络流】
    C++获取文件大小常用技巧
    operation is executing and cannot be enqueued
    Android 异步链式调用设计
    LINUX编程学习笔记(十三) 遍历目录的两种方法
  • 原文地址:https://www.cnblogs.com/luwei-s/p/4534342.html
Copyright © 2011-2022 走看看