using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Singletonhow
{
/// <summary>
/// 单例模式:适用于只有一个对象的场景,整个程序只有一个唯一对象
/// 要求:对象只被实例化一次
/// 比如说:打印机链接,数据库连接池,线程池,写日志对象
/// </summary>
class Program
{
static void Main(string[] args)
{
//生成任务工厂对象
TaskFactory taskfactory = new TaskFactory();
for (int i = 0; i < 10; i++)
{
//单线程
//SingletonOne.CreateInstanse();
taskfactory.StartNew(() => SingletonOne.CreateInstanse());
}
Thread.Sleep(15000);
Console.WriteLine("这里是第二次");
for (int i = 0; i < 10; i++)
{
//单线程
//SingletonOne.CreateInstanse();
taskfactory.StartNew(() => SingletonOne.CreateInstanse());
}
Console.ReadKey();
}
}
}