提供者模式:提供者的实例化加锁

private static DataProvider _provider = null;

 private static readonly object SyncLock = new object();

private static void LoadProviders()
{
    // providers are loaded just once
    if (null == _providers)
    {
        // Synchronize the process of loading the providers
        lock (SyncLock)
        {
            // Double confirm that the _provider is still null.
            if (null == _provider)
            {
                try
                {
                    // Reads the webconfig file corresponding node.
                    DataProviderSection section = (DataProviderSection)
                                                  WebConfigurationManager.GetSection("system.web/dataProviderService");

                    _providers = new DataProviderCollection();

                    // Creates provider instance, and invokes ProviderBase's Initialize function.
                    ProvidersHelper.InstantiateProviders(section.Providers, Providers, typeof(DataProvider));

                    // Gets the default in the collection.
                    _provider = Providers[section.DefaultProvider];
                }
                catch
                {
                    throw new ProviderException("Can't create instance");
                }
            }
        }
    }
}
原文地址:https://www.cnblogs.com/Med1tator/p/6959302.html