基础才是重中之重~LazyInitializer.EnsureInitialized对属性实现化的性能优化

本文介绍LazyInitializer.EnsureInitialized方法,该方法用于多线程环境下实现属性的延时初始化,避免多次不必要的实例化。通过三个代码示例对比展示了其优化效果。

回到目录

LazyInitializer.EnsureInitialized是frameworks4.0引入的新东西,实现对属性延时初始化的功能,它作用在System.Threading命名空间下,所以,它与多线程有着密切的关系,即当多人同步使用这个方法时,对存储的对象有着某种作用,这是msdn的相关说明:

这个方法可以用于多个执行者初始化Target目录对象。

在多个执行者同时存取这个方法的情况下,可能会建立多个T执行个体,但只有一个执行个体会存储至target。在些类情况下,这个方法将不会放置未储存的对象。如果这类对象必须被放置,则由呼叫端判断未使的对象,然后再对物件进行适当的放置。

对于概念不清楚的同步,没有关系,看下面的例子如完全明白了,呵呵

下面的实例介绍了对属性的两个初始化,并进行比较,延时初始化的好处,即在对象使用时再去初始化它当一个方法体中,如果一个对象初始化了一次,不要再进行重复的初始化工作。

代码1,展现了性能不好的情况

代码2,展现了优化的情况

代码3,微软为了我们进行了封装,在多线程中表现更好

代码1

  class TestLazyInitializer1
    {

        public People People
        {
            get
            {

                return new People();

            }
        }

        public void Print1()
        {
            Console.WriteLine(People.Name);
        }
        public void Print2()
        {
            Console.WriteLine(People.Name);
        }

    }

代码2:

 class TestLazyInitializer2
    {
        People _people;
        public People People
        {
            get
            {

                return _people == null
                    ? (_people = new People())
                    : _people;

            }
        }

        public void Print1()
        {
            Console.WriteLine(People.Name);
        }
        public void Print2()
        {
            Console.WriteLine(People.Name);
        }

    }

代码3

 class TestLazyInitializer
    {
        private People _people;
        /// <summary>
        /// 延时初始化指定属性
        /// </summary>
        public People People
        {
            get
            {
                return LazyInitializer.EnsureInitialized(
                    ref _people, () =>
                    {
                        return new People();
                    });
            }
        }

        public void Print1()
        {
            Console.WriteLine(People.Name);
        }
        public void Print2()
        {
            Console.WriteLine(People.Name);
        }

    }

而它们运行的结果,我们可想而知了,当一个类中多次使用同一个对象时,性能不好的,返回的Name(当前时间),肯定是不同的,而性能好的,只初始化一次的,返回的Name(当前时间)肯定是一个值,呵呵。

感谢阅读!

 回到目录

System.InvalidOperationException HResult=0x80131509 Message=A suitable constructor for type 'WebApi.Service.External.SplitterCommunicationService' could not be located. Ensure the type is concrete and all parameters of a public constructor are either registered as services or passed as arguments. Also ensure no extraneous arguments are provided. Source=Microsoft.Extensions.DependencyInjection.Abstractions StackTrace: 在 Microsoft.Extensions.DependencyInjection.ActivatorUtilities.FindApplicableConstructor(Type instanceType, Type[] argumentTypes, ConstructorInfo& matchingConstructor, Nullable`1[]& matchingParameterMap) 在 Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateFactory(Type instanceType, Type[] argumentTypes) 在 System.Threading.LazyInitializer.EnsureInitializedCore[T](T& target, Boolean& initialized, Object& syncLock, Func`1 valueFactory) 在 Microsoft.Extensions.Http.DefaultTypedHttpClientFactory`1.Cache.get_Activator() 在 Microsoft.Extensions.Http.DefaultTypedHttpClientFactory`1.CreateClient(HttpClient httpClient) 在 Microsoft.Extensions.DependencyInjection.HttpClientBuilderExtensions.AddTransientHelper[TClient,TImplementation](IServiceProvider s, IHttpClientBuilder builder) 在 Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitDisposeCache(ServiceCallSite transientCallSite, RuntimeResolverContext context) 在 Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument) 在 Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context) 在 Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context) 在 Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument) 在 Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitIEnumerable(IEnumerableCallSite enumerableCallSite, RuntimeResolverContext context) 在 Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context) 在 Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument) 在 Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope) 在 Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type serviceType) 在 System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) 在 Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope) 在 Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider) 在 Microsoft.Extensions.Hosting.Internal.Host.<StartAsync>d__12.MoveNext() 在 Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.<RunAsync>d__4.MoveNext() 在 Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.<RunAsync>d__4.MoveNext() 在 Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host) 在 Microsoft.AspNetCore.Builder.WebApplication.Run(String url) 在 Program.<Main>$(String[] args) 在 C:\Users\Administrator\Desktop\开发文件\WebApi.AgvServer - 冠佳\WebApi.AgvServer\Program.cs 中: 第 353 行
08-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值