Singleton

来源:互联网 发布:阿里云ssh连接不上 编辑:程序博客网 时间:2024/05/01 16:39

Singleton是我学的第一个模式,看了博客园上的解释,有点小问题。改了之后贴上。

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

namespace Singleton
{
    public class CountSigleton
    {
        /**////存储唯一的实例
        static CountSigleton uniCounter = new CountSigleton();  
   
        /**////存储计数值
        private int totNum = 0;  
   
        private CountSigleton() 
   
        { 
            /**////线程延迟2000毫秒
            Thread.Sleep(2000);
        } 
   
        static public CountSigleton Instance() 
   
        { 
   
            return uniCounter; 
   
        } 
        
        /**////计数加1
        public void Add()
        { 
            totNum ++;
        }  
        
        /**////获得当前计数值
        public int GetCounter()
        { 
            return totNum;
        } 
 
    }
}

 

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

namespace Singleton
{
    public class CountMutilThread
    {
        public CountMutilThread()
        {
           
        }

        /**//// <summary>
        /// 线程工作
        /// </summary>
        public static void DoSomeWork()
        {
            /**////构造显示字符串
            string results = "";

            /**////创建一个Sigleton实例
            CountSigleton MyCounter = CountSigleton.Instance();

            /**////循环调用四次
            for(int i=1;i<5;i++)
            {
                /**////开始计数
                lock (MyCounter)
                {

                    MyCounter.Add();

                    results += "线程";
                    results += Thread.CurrentThread.Name.ToString() + "——〉";
                    results += "当前的计数:";
                    results += MyCounter.GetCounter().ToString();
                    results += "/n";

                    Console.WriteLine(results);

                    /**/
                    ///清空显示字符串
                    results = "";
                }
            }
        }

        public void StartMain()
        {

           

            Thread thread0 = Thread.CurrentThread;
           
          

            thread0.Name = "Thread 0";
  
            Thread thread1 =new Thread(new ThreadStart(DoSomeWork));
  
            thread1.Name = "Thread 1";
  
            Thread thread2 =new Thread(new ThreadStart(DoSomeWork));
  
            thread2.Name = "Thread 2";
  
            Thread thread3 =new Thread(new ThreadStart(DoSomeWork));
  
            thread3.Name = "Thread 3";
  
            thread1.Start();
  
            thread2.Start();
  
            thread3.Start();
           
            /**////线程0也只执行和其他线程相同的工作
            DoSomeWork();
        }
    }

}

 

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

namespace Singleton
{
    public class CountClient
    {
        public static void Main(string[] args)
        {
       CountMutilThread cmt = new CountMutilThread();
 
            cmt.StartMain();
 
            Console.ReadLine();
        }
    }

}

 

原创粉丝点击