public static Singleton1 Instance() { // Uses lazy initialization. // Note: this is not thread safe. if (_instance == null) { _instance = new Singleton1(); } return _instance; }
public static Singleton1 Instance() { if (_instance == null) { lock (typeof(Singleton1)) { if (_instance == null) { _instance = new Singleton1(); } } } return _instance; }
public static Singleton1 GetInstance() { if (_instance == null) { lock (syn) { if (_instance == null) { _instance = new Singleton1(); Console.WriteLine("创建成功" + DateTime.Now.ToString("yyyy-MM-dd:hhmmss")); } } } return(_instance); }
private static void RegisterStandard() { var singleton1 = new Singleton1(); var singleton2 = new Singleton2(); var singleton3 = new Singleton3(); Container.SetResolver <ISingleton1>(() => singleton1); Container.SetResolver <ISingleton2>(() => singleton2); Container.SetResolver <ISingleton3>(() => singleton3); Container.SetResolver <ITransient1, Transient1>(); Container.SetResolver <ITransient2, Transient2>(); Container.SetResolver <ITransient3, Transient3>(); Container.SetResolver <ICombined1, Combined1>(); Container.SetResolver <ICombined2, Combined2>(); Container.SetResolver <ICombined3, Combined3>(); }
public void GetInstanceTest() { for (int i = 0; i < 5; i++) { int threadCount = 10000; Task <int>[] tasks = new Task <int> [threadCount]; for (int j = 0; j < threadCount; j++) { tasks[j] = Task.Run <int>(() => { return(Singleton1.GetInstance().GetHashCode()); }); } Task.WaitAll(tasks); var codes = tasks.Select(t => t.Result).Distinct(); Assert.AreEqual(codes.Count(), 1, $"Singleton1非单例 {codes.Count()}"); } }
private void RegisterStandard() { ISingleton1 singleton1 = new Singleton1(); this.container[typeof(ISingleton1)] = () => singleton1; this.container[typeof(ITransient1)] = () => new Transient1(); this.container[typeof(ICombined1)] = () => new Combined1(singleton1, new Transient1()); ISingleton2 singleton2 = new Singleton2(); this.container[typeof(ISingleton2)] = () => singleton2; this.container[typeof(ITransient2)] = () => new Transient2(); this.container[typeof(ICombined2)] = () => new Combined2(singleton2, new Transient2()); ISingleton3 singleton3 = new Singleton3(); this.container[typeof(ISingleton3)] = () => singleton3; this.container[typeof(ITransient3)] = () => new Transient3(); this.container[typeof(ICombined3)] = () => new Combined3(singleton3, new Transient3()); }
public static void TestInstanceProfile() { int count = 100000; long es = 0; var watch = Stopwatch.StartNew(); for (int i = 0; i < count; i++) { var va = Singleton1.GetInstance(); } watch.Stop(); es = watch.ElapsedTicks; Console.WriteLine($"1 耗时 {es}"); watch.Restart(); for (int i = 0; i < count; i++) { var va = Singleton2.GetInstance(); } watch.Stop(); es = watch.ElapsedTicks; Console.WriteLine($"2 耗时 {es}"); watch.Restart(); for (int i = 0; i < count; i++) { var va = Singleton3.GetInstance(); } watch.Stop(); es = watch.ElapsedTicks; Console.WriteLine($"3 耗时 {es}"); watch.Restart(); for (int i = 0; i < count; i++) { var va = Singleton4.GetInstance(); } watch.Stop(); es = watch.ElapsedTicks; Console.WriteLine($"4 耗时 {es}"); }
public ISingleton1 ProvideSingleton(Singleton1 singleton) { return(singleton); }
static Singleton1() { _instance = new Singleton1(); }
public ISingleton1 ProvideSingleton(Singleton1 singleton) => singleton;